private void SetFluentApiProperties(Resource resource, ClassObject classObject, string url)
        {
            var propertiesNames = new List <string>();

            foreach (var childResource in resource.Resources)
            {
                if (childResource.RelativeUri.StartsWith("/{") && childResource.RelativeUri.EndsWith("}"))
                {
                    continue;
                }

                var property = new FluentProperty
                {
                    Name = GetExistingObjectName(GetUrl(url, childResource.RelativeUri)),
                };

                if (propertiesNames.Contains(property.Name))
                {
                    continue;
                }

                classObject.Properties.Add(property);
                propertiesNames.Add(property.Name);
            }
        }
Beispiel #2
0
        private void SetClassesProperties(ClassObject rootClassObject)
        {
            var propertiesNames = new List<string>();
            foreach (var parentResource in raml.Resources)
            {
                var fullUrl = GetUrl(null, parentResource.RelativeUri);

                if (!parentResource.RelativeUri.StartsWith("/{") || !parentResource.RelativeUri.EndsWith("}"))
                {
                    var property = new FluentProperty
                                   {
                                       Name = GetExistingObjectName(fullUrl),
                                   };

                    if (propertiesNames.Contains(property.Name))
                        continue;

                    rootClassObject.Properties.Add(property);
                    propertiesNames.Add(property.Name);

                    var classObj = GetClassObject(fullUrl);
                    if (classObj == null)
                        throw new InvalidOperationException("Null class object for resource " + fullUrl);

                    SetFluentApiProperties(parentResource, classObj, fullUrl);
                    SetClassesProperties(parentResource.Resources, classObj, fullUrl);
                }
                else
                {
                    SetFluentApiProperties(parentResource, rootClassObject, fullUrl);
                    SetClassesProperties(parentResource.Resources, rootClassObject, fullUrl);
                }
            }
        }
Beispiel #3
0
        private void SetClassesProperties(IEnumerable<Resource> resources, ClassObject parentClass, string url)
        {
            if (resources == null)
                return;

            foreach (var resource in resources)
            {
                if (resource == null)
                    continue;

                var fullUrl = GetUrl(url, resource.RelativeUri);
                if (!resource.RelativeUri.StartsWith("/{") || !resource.RelativeUri.EndsWith("}"))
                {
                    var classObj = GetClassObject(fullUrl);
                    if (classObj == null)
                        throw new InvalidOperationException("Null class object for resource " + fullUrl);

                    SetFluentApiProperties(resource, classObj, fullUrl);
                    SetClassesProperties(resource.Resources, classObj, fullUrl);
                }
                else
                {
                    SetFluentApiProperties(resource, parentClass, fullUrl);
                    SetClassesProperties(resource.Resources, parentClass, fullUrl);
                }
            }
        }
        public ClientGeneratorModel BuildModel()
        {
            warnings = new Dictionary<string, string>();
            classesNames = new Collection<string>();
            classes = new Collection<ClassObject>();
            classesObjectsRegistry = new Dictionary<string, ClassObject>();
            uriParameterObjects = new Dictionary<string, ApiObject>();
            enums = new Dictionary<string, ApiEnum>();

            ParseSchemas();
            schemaRequestObjects = GetRequestObjects();
            schemaResponseObjects = GetResponseObjects();

            CleanProperties(schemaObjects);
            CleanProperties(schemaRequestObjects);
            CleanProperties(schemaResponseObjects);

            clientMethodsGenerator = new ClientMethodsGenerator(raml, schemaResponseObjects, uriParameterObjects,
                queryObjects, headerObjects, responseHeadersObjects, schemaRequestObjects, linkKeysWithObjectNames,
                schemaObjects);

            var parentClass = new ClassObject { Name = rootClassName, Description = "Main class for grouping root resources. Nested resources are defined as properties. The constructor can optionally receive an URL and HttpClient instance to override the default ones." };
            classesNames.Add(parentClass.Name);
            classes.Add(parentClass);
            classesObjectsRegistry.Add(rootClassName, parentClass);

            var classObjects = GetClasses(raml.Resources, null, parentClass, null);
            SetClassesProperties(classesObjectsRegistry[rootClassName]);

            var apiRequestObjects = apiRequestGenerator.Generate(classObjects);
            var apiResponseObjects = apiResponseGenerator.Generate(classObjects);

            CleanNotUsedObjects(classObjects);

            return new ClientGeneratorModel
                   {
                       Namespace = NetNamingMapper.GetNamespace(raml.Title),
                       SchemaObjects = schemaObjects,
                       RequestObjects = schemaRequestObjects,
                       ResponseObjects = schemaResponseObjects,
                       QueryObjects = queryObjects,
                       HeaderObjects = headerObjects,
                       ApiRequestObjects = apiRequestObjects,
                       ApiResponseObjects = apiResponseObjects,
                       ResponseHeaderObjects = responseHeadersObjects,
                       BaseUriParameters = ParametersMapper.Map(raml.BaseUriParameters),
                       BaseUri = raml.BaseUri,
                       Security = SecurityParser.GetSecurity(raml),
                       Version = raml.Version,
                       Warnings = warnings,
                       Classes = classObjects.Where(c => c.Name != rootClassName).ToArray(),
                       Root = classObjects.First(c => c.Name == rootClassName),
                       UriParameterObjects = uriParameterObjects,
                       Enums = Enums
                   };
        }
        private ICollection <ClassObject> GetClasses(IEnumerable <Resource> resources, Resource parent, ClassObject parentClass, string url, IDictionary <string, Parameter> parentUriParameters)
        {
            if (resources == null)
            {
                return(classes);
            }

            foreach (var resource in resources)
            {
                if (resource == null)
                {
                    continue;
                }

                var fullUrl = GetUrl(url, resource.RelativeUri);
                // when the resource is a parameter dont generate a class but add it's methods and children to the parent
                if (resource.RelativeUri.StartsWith("/{") && resource.RelativeUri.EndsWith("}"))
                {
                    var generatedMethods = clientMethodsGenerator.GetMethods(resource, fullUrl, parentClass, parentClass.Name, parentUriParameters);

                    foreach (var method in generatedMethods)
                    {
                        parentClass.Methods.Add(method);
                    }

                    GetInheritedUriParams(parentUriParameters, resource);

                    var children = GetClasses(resource.Resources, resource, parentClass, fullUrl, parentUriParameters);
                    foreach (var child in children)
                    {
                        parentClass.Children.Add(child);
                    }
                    continue;
                }

                var classObj = new ClassObject
                {
                    Name        = GetUniqueObjectName(resource, parent),
                    Description = resource.Description
                };
                classObj.Methods = clientMethodsGenerator.GetMethods(resource, fullUrl, null, classObj.Name, parentUriParameters);

                GetInheritedUriParams(parentUriParameters, resource);

                classObj.Children = GetClasses(resource.Resources, resource, classObj, fullUrl, parentUriParameters);

                //TODO: check
                parentClass.Children.Add(classObj);

                classesNames.Add(classObj.Name);
                classes.Add(classObj);
                classesObjectsRegistry.Add(CalculateClassKey(fullUrl), classObj);
            }
            return(classes);
        }
        public ClientGeneratorModel BuildModel()
        {
            warnings               = new Dictionary <string, string>();
            classesNames           = new Collection <string>();
            classes                = new Collection <ClassObject>();
            classesObjectsRegistry = new Dictionary <string, ClassObject>();
            uriParameterObjects    = new Dictionary <string, ApiObject>();

            schemaRequestObjects  = GetRequestObjects();
            schemaResponseObjects = GetResponseObjects();

            CleanProperties(schemaRequestObjects);
            CleanProperties(schemaResponseObjects);

            var parentClass = new ClassObject {
                Name = rootClassName, Description = "Main class for grouping root resources. Nested resources are defined as properties. The constructor can optionally receive an URL and HttpClient instance to override the default ones."
            };

            classesNames.Add(parentClass.Name);
            classes.Add(parentClass);
            classesObjectsRegistry.Add(rootClassName, parentClass);

            var classObjects = GetClasses(raml.Resources, null, parentClass, null);

            SetClassesProperties(classesObjectsRegistry[rootClassName]);

            var apiRequestObjects  = apiRequestGenerator.Generate(classObjects);
            var apiResponseObjects = apiResponseGenerator.Generate(classObjects);

            return(new ClientGeneratorModel
            {
                Namespace = NetNamingMapper.GetNamespace(raml.Title),
                RequestObjects = schemaRequestObjects,
                ResponseObjects = schemaResponseObjects,
                QueryObjects = queryObjects,
                HeaderObjects = headerObjects,
                ApiRequestObjects = apiRequestObjects,
                ApiResponseObjects = apiResponseObjects,
                ResponseHeaderObjects = responseHeadersObjects,
                BaseUriParameters = ParametersMapper.Map(raml.BaseUriParameters),
                BaseUri = raml.BaseUri,
                Security = SecurityParser.GetSecurity(raml),
                Version = raml.Version,
                Warnings = warnings,
                Classes = classObjects.Where(c => c.Name != rootClassName).ToArray(),
                Root = classObjects.First(c => c.Name == rootClassName),
                UriParameterObjects = uriParameterObjects
            });
        }
	    public ICollection<ClientGeneratorMethod> GetMethods(Resource resource, string url, ClassObject parent, string objectName)
		{
			var methodsNames = new List<string>();
			if (parent != null)
				methodsNames = parent.Methods.Select(m => m.Name).ToList();

			var generatorMethods = new Collection<ClientGeneratorMethod>();
			if (resource.Methods == null)
				return generatorMethods;

			foreach (var method in resource.Methods)
			{
				AddGeneratedMethod(resource, url, objectName, method, methodsNames, generatorMethods);
			}

			return generatorMethods;
		}
 private void BuildRequest(ClientGeneratorMethod method, ClassObject classObject, ICollection<ApiObject> objects)
 {
     var requestProperties = BuildRequestProperties(method).ToList();
     if (requestProperties.Any())
     {
         var reqObject = new ApiObject
         {
             Name = classObject.Name + method.Name + "Request",
             Description = "Request object for method " + method.Name + " of class " + classObject.Name,
             Properties = requestProperties
         };
         objects.Add(reqObject);
         method.RequestType = ClientGeneratorMethod.ModelsNamespacePrefix + reqObject.Name;
     }
     else
     {
         method.RequestType = "ApiRequest";
     }
 }
        private void BuildRequest(ClientGeneratorMethod method, ClassObject classObject, ICollection <ApiObject> objects)
        {
            var requestProperties = BuildRequestProperties(method).ToList();

            if (requestProperties.Any())
            {
                var reqObject = new ApiObject
                {
                    Name        = classObject.Name + method.Name + "Request",
                    Description = "Request object for method " + method.Name + " of class " + classObject.Name,
                    Properties  = requestProperties
                };
                objects.Add(reqObject);
                method.RequestType = ClientGeneratorMethod.ModelsNamespacePrefix + reqObject.Name;
            }
            else
            {
                method.RequestType = "ApiRequest";
            }
        }
Beispiel #10
0
        private void BuildResponse(ClientGeneratorMethod method, ClassObject classObject, ICollection <ApiObject> objects)
        {
            var responseProperties = BuildResponseProperties(method).ToList();

            if (responseProperties.Any())
            {
                var respObject = new ApiObject
                {
                    Name        = classObject.Name + method.Name + "Response",
                    Description = "Response object for method " + method.Name + " of class " + classObject.Name,
                    Properties  = responseProperties,
                    IsUnionType = method.ReturnTypeObject != null && method.ReturnTypeObject.IsUnionType
                };
                objects.Add(respObject);
                method.ResponseType = ClientGeneratorMethod.ModelsNamespacePrefix + respObject.Name;
            }
            else
            {
                method.ResponseType = "ApiResponse";
            }
        }
        public void Should_Generate_One_Method_Per_Verb()
        {
            var methods = new List<Method>
                          {
                              new Method
                              {
                                  Verb = "get"
                              },
                              new Method
                              {
                                  Verb = "post"
                              }
                          };

            var resource = new Resource
            {
                RelativeUri = "/abc{token}{code}",
                Methods = methods,
            };

            var schemaResponseObjects = new Dictionary<string, ApiObject>();
            var schemaRequestObjects = new Dictionary<string, ApiObject>();
            var ramlDocument = new RamlDocument();
            var uriParameterObjects = new Dictionary<string, ApiObject>();
            var queryObjects = new Dictionary<string, ApiObject>();
            var headerObjects = new Dictionary<string, ApiObject>();
            var responseHeadersObjects = new Dictionary<string, ApiObject>();
            var linkedKeyWithObjectNames = new Dictionary<string, string>();
            var classObject = new ClassObject();
            var schemaObjects = new Dictionary<string, ApiObject>();

            var generator = new ClientMethodsGenerator(ramlDocument, schemaResponseObjects,
                uriParameterObjects, queryObjects, headerObjects, responseHeadersObjects, schemaRequestObjects, linkedKeyWithObjectNames,
                schemaObjects);

            var generatorMethods = generator.GetMethods(resource, "/", classObject, "Test", new Dictionary<string, Parameter>());

            Assert.AreEqual(2, generatorMethods.Count);
        }
        private void SetClassesProperties(IEnumerable<Resource> resources, ClassObject parentClass, string url)
        {
            if (resources == null)
                return;

            foreach (var resource in resources)
            {
                if (resource == null)
                    continue;

                var fullUrl = GetUrl(url, resource.RelativeUri);
                if (!resource.RelativeUri.StartsWith("/{") || !resource.RelativeUri.EndsWith("}"))
                {
                    var classObj = GetClassObject(fullUrl);
                    if (classObj == null)
                        throw new InvalidOperationException("Null class object for resource " + fullUrl);

                    SetFluentApiProperties(resource, classObj, fullUrl);
                    SetClassesProperties(resource.Resources, classObj, fullUrl);
                }
                else
                {
                    SetFluentApiProperties(resource, parentClass, fullUrl);
                    SetClassesProperties(resource.Resources, parentClass, fullUrl);
                }
            }
        }
Beispiel #13
0
        public ICollection <ClientGeneratorMethod> GetMethods(Resource resource, string url, ClassObject parent, string objectName, IDictionary <string, ApiObject> schemaResponseObjects, IDictionary <string, ApiObject> uriParameterObjects, IDictionary <string, ApiObject> queryObjects, IDictionary <string, ApiObject> headerObjects, IDictionary <string, ApiObject> responseHeadersObjects, IDictionary <string, ApiObject> schemaRequestObjects)
        {
            var methodsNames = new List <string>();

            if (parent != null)
            {
                methodsNames = parent.Methods.Select(m => m.Name).ToList();
            }

            var generatorMethods = new Collection <ClientGeneratorMethod>();

            if (resource.Methods == null)
            {
                return(generatorMethods);
            }

            foreach (var method in resource.Methods)
            {
                AddGeneratedMethod(resource, url, objectName, schemaResponseObjects, uriParameterObjects, queryObjects,
                                   headerObjects, responseHeadersObjects, method, methodsNames, generatorMethods, schemaRequestObjects);
            }

            return(generatorMethods);
        }
Beispiel #14
0
        public ICollection <ClientGeneratorMethod> GetMethods(Resource resource, string url, ClassObject parent, string objectName, IDictionary <string, Parameter> parentUriParameters)
        {
            var methodsNames = new List <string>();

            if (parent != null)
            {
                methodsNames = parent.Methods.Select(m => m.Name).ToList();
            }

            var generatorMethods = new Collection <ClientGeneratorMethod>();

            if (resource.Methods == null)
            {
                return(generatorMethods);
            }

            foreach (var method in resource.Methods)
            {
                AddGeneratedMethod(resource, url, objectName, method, methodsNames, generatorMethods, parentUriParameters);
            }

            return(generatorMethods);
        }
        private void SetFluentApiProperties(Resource resource, ClassObject classObject, string url)
        {
            var propertiesNames = new List<string>();

            foreach (var childResource in resource.Resources)
            {
                if (childResource.RelativeUri.StartsWith("/{") && childResource.RelativeUri.EndsWith("}"))
                    continue;

                var property = new FluentProperty
                {
                    Name = GetExistingObjectName(GetUrl(url, childResource.RelativeUri)),
                };

                if (propertiesNames.Contains(property.Name))
                    continue;

                classObject.Properties.Add(property);
                propertiesNames.Add(property.Name);
            }
        }
        private void SetClassesProperties(ClassObject rootClassObject)
        {
            var propertiesNames = new List<string>();
            foreach (var parentResource in raml.Resources)
            {
                var fullUrl = GetUrl(null, parentResource.RelativeUri);

                if (!parentResource.RelativeUri.StartsWith("/{") || !parentResource.RelativeUri.EndsWith("}"))
                {
                    var property = new FluentProperty
                                   {
                                       Name = GetExistingObjectName(fullUrl),
                                   };

                    if (propertiesNames.Contains(property.Name))
                        continue;

                    rootClassObject.Properties.Add(property);
                    propertiesNames.Add(property.Name);

                    var classObj = GetClassObject(fullUrl);
                    if (classObj == null)
                        throw new InvalidOperationException("Null class object for resource " + fullUrl);

                    SetFluentApiProperties(parentResource, classObj, fullUrl);
                    SetClassesProperties(parentResource.Resources, classObj, fullUrl);
                }
                else
                {
                    SetFluentApiProperties(parentResource, rootClassObject, fullUrl);
                    SetClassesProperties(parentResource.Resources, rootClassObject, fullUrl);
                }
            }
        }
        private ICollection<ClassObject> GetClasses(IEnumerable<Resource> resources, Resource parent, ClassObject parentClass, string url)
        {
            if (resources == null)
                return classes;

            foreach (var resource in resources)
            {
                if (resource == null)
                    continue;

                var fullUrl = GetUrl(url, resource.RelativeUri);
                // when the resource is a parameter dont generate a class but add it's methods and children to the parent
                if (resource.RelativeUri.StartsWith("/{") && resource.RelativeUri.EndsWith("}"))
                {
                    var generatedMethods = clientMethodsGenerator.GetMethods(resource, fullUrl, parentClass, parentClass.Name);

                    foreach (var method in generatedMethods)
                    {
                        parentClass.Methods.Add(method);
                    }

                    var children = GetClasses(resource.Resources, resource, parentClass, fullUrl);
                    foreach (var child in children)
                    {
                        parentClass.Children.Add(child);
                    }
                    continue;
                }

                var classObj = new ClassObject
                               {
                                   Name = GetUniqueObjectName(resource, parent),
                                   Description = resource.Description
                               };
                classObj.Methods = clientMethodsGenerator.GetMethods(resource, fullUrl, null, classObj.Name);

                classObj.Children = GetClasses(resource.Resources, resource, classObj, fullUrl);
                
                //TODO: check
                parentClass.Children.Add(classObj);

                classesNames.Add(classObj.Name);
                classes.Add(classObj);
                classesObjectsRegistry.Add(CalculateClassKey(fullUrl), classObj);
            }
            return classes;
        }