Ejemplo n.º 1
0
        public WebApiGeneratorModel BuildModel()
        {
            classesNames = new Collection <string>();
            warnings     = new Dictionary <string, string>();
            enums        = new Dictionary <string, ApiEnum>();

            //new RamlTypeParser(raml.Shapes, schemaObjects, ns, enums, warnings).Parse();

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

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

            HandleScalarTypes();

            if (raml.WebApi == null)
            {
                return(new WebApiGeneratorModel
                {
                    ControllersNamespace = controllersNamespace,
                    ModelsNamespace = modelsNamespace,
                    SchemaObjects = schemaObjects,
                    RequestObjects = schemaRequestObjects,
                    ResponseObjects = schemaResponseObjects,
                    Warnings = warnings,
                    Enums = Enums
                });
            }

            webApiMethodsGenerator = new WebApiMethodsGenerator(raml, schemaResponseObjects, schemaRequestObjects, linkKeysWithObjectNames, schemaObjects,
                                                                enums);
            var controllers = GetControllers().ToArray();

            apiObjectsCleaner      = new ApiObjectsCleaner(schemaRequestObjects, schemaResponseObjects, schemaObjects);
            uriParametersGenerator = new UriParametersGenerator(schemaObjects);

            CleanNotUsedObjects(controllers);

            return(new WebApiGeneratorModel
            {
                ControllersNamespace = controllersNamespace,
                ModelsNamespace = modelsNamespace,
                Controllers = controllers,
                SchemaObjects = schemaObjects,
                RequestObjects = schemaRequestObjects,
                ResponseObjects = schemaResponseObjects,
                Warnings = warnings,
                Enums = Enums,
                ApiVersion = raml.WebApi.Version
            });
        }
        public void should_clean_objects_not_used_as_return_types_client()
        {
            var property = new Property
            {
                Name         = "Prop1",
                Type         = CollectionTypeHelper.GetCollectionType("Object2"),
                OriginalName = "prop1"
            };

            var property2 = new Property
            {
                Name         = "Prop2",
                Type         = "int",
                OriginalName = "prop1"
            };

            var o1 = new ApiObject
            {
                Name       = "Object1",
                Properties = new[] { property }
            };

            var o2 = new ApiObject
            {
                Name       = "Object2",
                IsArray    = true,
                Properties = new[] { property2 }
            };

            var o3 = new ApiObject
            {
                Name       = "Object3",
                IsArray    = true,
                Properties = new[] { property }
            };

            var schemaResponseObjects = new Dictionary <string, ApiObject>();

            schemaResponseObjects.Add("obj1", o1);
            schemaResponseObjects.Add("obj2", o2);
            schemaResponseObjects.Add("obj3", o3);

            var schemaRequestObjects = new Dictionary <string, ApiObject>();
            var cleaner      = new ApiObjectsCleaner(schemaRequestObjects, schemaResponseObjects, new Dictionary <string, ApiObject>());
            var classObjects = new List <ClassObject>();

            var methods = new Collection <ClientGeneratorMethod>
            {
                new ClientGeneratorMethod
                {
                    ReturnType = CollectionTypeHelper.GetCollectionType("Object1")
                }
            };

            classObjects.Add(new ClassObject
            {
                Name    = "Class1",
                Methods = methods
            });

            cleaner.CleanObjects(classObjects, schemaResponseObjects, cleaner.IsUsedAsResponseInAnyMethod);

            Assert.AreEqual(2, schemaResponseObjects.Count);
            Assert.IsTrue(schemaResponseObjects.ContainsKey("obj1"));
            Assert.IsTrue(schemaResponseObjects.ContainsKey("obj2"));
            Assert.IsFalse(schemaResponseObjects.ContainsKey("obj3"));
        }
        public void should_clean_objects_not_used_as_return_types_client()
        {
            var property = new Property
            {
                Name = "Prop1",
                Type = CollectionTypeHelper.GetCollectionType("Object2"),
                OriginalName = "prop1"
            };

            var property2 = new Property
            {
                Name = "Prop2",
                Type = "int",
                OriginalName = "prop1"
            };

            var o1 = new ApiObject
            {
                Name = "Object1",
                Properties = new[] { property }
            };

            var o2 = new ApiObject
            {
                Name = "Object2",
                IsArray = true,
                Properties = new[] { property2 }
            };

            var o3 = new ApiObject
            {
                Name = "Object3",
                IsArray = true,
                Properties = new[] { property }
            };

            var schemaResponseObjects = new Dictionary<string, ApiObject>();
            schemaResponseObjects.Add("obj1", o1);
            schemaResponseObjects.Add("obj2", o2);
            schemaResponseObjects.Add("obj3", o3);

            var schemaRequestObjects = new Dictionary<string, ApiObject>();
            var cleaner = new ApiObjectsCleaner(schemaRequestObjects, schemaResponseObjects, new Dictionary<string, ApiObject>());
            var classObjects = new List<ClassObject>();

            var methods = new Collection<ClientGeneratorMethod>
            {
                new ClientGeneratorMethod
                {
                    ReturnType = CollectionTypeHelper.GetCollectionType("Object1")
                }
            };
            classObjects.Add(new ClassObject
            {
                Name = "Class1",
                Methods = methods
            });

            cleaner.CleanObjects(classObjects, schemaResponseObjects, cleaner.IsUsedAsResponseInAnyMethod);

            Assert.AreEqual(2, schemaResponseObjects.Count);
            Assert.IsTrue(schemaResponseObjects.ContainsKey("obj1"));
            Assert.IsTrue(schemaResponseObjects.ContainsKey("obj2"));
            Assert.IsFalse(schemaResponseObjects.ContainsKey("obj3"));
        }
        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>();

            //new RamlTypeParser(raml.Shapes, schemaObjects, ns, enums, warnings).Parse();

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

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

            HandleScalarTypes();

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

            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.WebApi.EndPoints, null, parentClass, null, new Dictionary <string, Parameter>());

            SetClassesProperties(classesObjectsRegistry[rootClassName]);

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


            apiObjectsCleaner      = new ApiObjectsCleaner(schemaRequestObjects, schemaResponseObjects, schemaObjects);
            uriParametersGenerator = new UriParametersGenerator(schemaObjects);

            CleanNotUsedObjects(classObjects);



            return(new ClientGeneratorModel
            {
                BaseNamespace = baseNamespace,
                ModelsNamespace = modelsNamespace,
                SchemaObjects = schemaObjects,
                RequestObjects = schemaRequestObjects,
                ResponseObjects = schemaResponseObjects,
                QueryObjects = queryObjects,
                HeaderObjects = headerObjects,

                ApiRequestObjects = apiRequestObjects.ToArray(),
                ApiResponseObjects = apiResponseObjects.ToArray(),
                ResponseHeaderObjects = responseHeadersObjects,

                BaseUriParameters = ParametersMapper.Map(raml.WebApi.BaseUriParameters).ToArray(),
                BaseUri = raml.WebApi.Servers.Any() ? raml.WebApi.Servers.First() : null,
                Security = SecurityParser.GetSecurity(raml.WebApi),
                Version = raml.WebApi.Version,
                Warnings = warnings,
                Classes = classObjects.Where(c => c.Name != rootClassName).ToArray(),
                Root = classObjects.First(c => c.Name == rootClassName),
                UriParameterObjects = uriParameterObjects,
                Enums = Enums.ToArray()
            });
        }