Esempio n. 1
0
        public void TestInputMapping()
        {
            using (NewContext)
            {
                var settings = new Settings
                {
                    Namespace = "Test",
                    Input = Path.Combine("Swagger", "swagger-payload-flatten.json"),
                    PayloadFlatteningThreshold = 3,
                    OutputDirectory = Path.GetTempPath()
                };
                settings.FileSystem = new MemoryFileSystem();
                settings.FileSystem.WriteFile("AutoRest.json", File.ReadAllText("AutoRest.json"));
                settings.FileSystem.CreateDirectory(Path.GetDirectoryName(settings.Input));
                settings.FileSystem.WriteFile(settings.Input, File.ReadAllText(settings.Input));

                var modeler = new SwaggerModeler();
                var clientModel = modeler.Build();
                CodeGeneratorCs generator = new CodeGeneratorCs();

                generator.Generate(clientModel).GetAwaiter().GetResult();
                string body = settings.FileSystem.ReadFileAsText(Path.Combine(settings.OutputDirectory, "Payload.cs"));
                Assert.True(body.ContainsMultiline(@"
                MinProduct minProduct = new MinProduct();
                if (baseProductId != null || baseProductDescription != null || maxProductReference != null)
                {
                    minProduct.BaseProductId = baseProductId;
                    minProduct.BaseProductDescription = baseProductDescription;
                    minProduct.MaxProductReference = maxProductReference;
                }"));
            }
        }
Esempio n. 2
0
 public OperationBuilder(Operation operation, SwaggerModeler swaggerModeler)
 {
     _operation         = operation ?? throw new ArgumentNullException("operation");
     _swaggerModeler    = swaggerModeler ?? throw new ArgumentNullException("swaggerModeler");
     _effectiveProduces = (operation.Produces.Any() ? operation.Produces : swaggerModeler.ServiceDefinition.Produces).ToList();
     _effectiveConsumes = (operation.Consumes.Any() ? operation.Consumes : swaggerModeler.ServiceDefinition.Consumes).ToList();
 }
        public void ConvertsPageResultsToPageTypeTest()
        {
            var settings = new Settings
            {
                Input = Path.Combine("Swagger", "azure-paging.json")
            };

            var modeler = new SwaggerModeler(settings);
            var serviceClient = modeler.Build();
            var codeGen = new AzureCSharpCodeGenerator(settings);
            codeGen.NormalizeClientModel(serviceClient);

            Assert.Equal(7, serviceClient.Methods.Count);
            Assert.Equal(1, serviceClient.Methods.Count(m => m.Name == "GetSinglePage"));
            Assert.Equal(0, serviceClient.Methods.Count(m => m.Name == "GetSinglePageNext"));
            Assert.Equal(1, serviceClient.Methods.Count(m => m.Name == "PutSinglePage"));
            Assert.Equal(1, serviceClient.Methods.Count(m => m.Name == "PutSinglePageSpecialNext"));

            Assert.Equal("Page<Product>", serviceClient.Methods[0].ReturnType.Body.Name);
            Assert.Equal("object", serviceClient.Methods[1].ReturnType.Body.Name.ToLowerInvariant());
            Assert.Equal("Page1<Product>", serviceClient.Methods[1].Responses.ElementAt(0).Value.Body.Name);
            Assert.Equal("string", serviceClient.Methods[1].Responses.ElementAt(1).Value.Body.Name.ToLowerInvariant());
            Assert.Equal("object", serviceClient.Methods[2].ReturnType.Body.Name.ToLowerInvariant());
            Assert.Equal("Page1<Product>", serviceClient.Methods[2].Responses.ElementAt(0).Value.Body.Name);
            Assert.Equal("Page1<Product>", serviceClient.Methods[2].Responses.ElementAt(1).Value.Body.Name);
            Assert.Equal("object", serviceClient.Methods[3].ReturnType.Body.Name.ToLowerInvariant());
            Assert.Equal("Page1<Product>", serviceClient.Methods[3].Responses.ElementAt(0).Value.Body.Name);
            Assert.Equal("Page1<ProductChild>", serviceClient.Methods[3].Responses.ElementAt(1).Value.Body.Name);
            Assert.Equal(5, serviceClient.ModelTypes.Count);
            Assert.False(serviceClient.ModelTypes.Any(t => t.Name.Equals("ProducResult", StringComparison.OrdinalIgnoreCase)));
            Assert.False(serviceClient.ModelTypes.Any(t => t.Name.Equals("ProducResult2", StringComparison.OrdinalIgnoreCase)));
            Assert.False(serviceClient.ModelTypes.Any(t => t.Name.Equals("ProducResult3", StringComparison.OrdinalIgnoreCase)));
        }
Esempio n. 4
0
        public void TestClientModelWithPayloadFlattening()
        {
            using (NewContext)
            {
                var setting = new Settings
                {
                    Namespace = "Test",
                    Input = Path.Combine("Swagger", "swagger-payload-flatten.json"),
                    PayloadFlatteningThreshold = 3
                };
                var modeler = new SwaggerModeler();
                var clientModel = modeler.Build();
                SwaggerExtensions.NormalizeClientModel(clientModel);

                Assert.NotNull(clientModel);
                Assert.Equal(4, clientModel.Methods[0].Parameters.Count);
                Assert.Equal("String subscriptionId", clientModel.Methods[0].Parameters[0].ToString());
                Assert.Equal("String resourceGroupName", clientModel.Methods[0].Parameters[1].ToString());
                Assert.Equal("String apiVersion", clientModel.Methods[0].Parameters[2].ToString());
                Assert.Equal("MaxProduct maxProduct", clientModel.Methods[0].Parameters[3].ToString());
                Assert.Equal(6, clientModel.Methods[1].Parameters.Count);
                Assert.Equal("String subscriptionId", clientModel.Methods[1].Parameters[0].ToString());
                Assert.Equal("String resourceGroupName", clientModel.Methods[1].Parameters[1].ToString());
                Assert.Equal("String apiVersion", clientModel.Methods[1].Parameters[2].ToString());
                Assert.Equal("String baseProductId", clientModel.Methods[1].Parameters[3].ToString());
                Assert.Equal(true, clientModel.Methods[1].Parameters[3].IsRequired);
                Assert.Equal("String baseProductDescription", clientModel.Methods[1].Parameters[4].ToString());
                Assert.Equal(false, clientModel.Methods[1].Parameters[4].IsRequired);
                Assert.Equal("MaxProduct maxProductReference", clientModel.Methods[1].Parameters[5].ToString());
                Assert.Equal(false, clientModel.Methods[1].Parameters[5].IsRequired);
                Assert.Equal(1, clientModel.Methods[1].InputParameterTransformation.Count);
                Assert.Equal(3, clientModel.Methods[1].InputParameterTransformation[0].ParameterMappings.Count);
            }
        }
Esempio n. 5
0
        public void TestParameterLocationExtension()
        {
            using (NewContext)
            {
                var setting = new Settings
                {
                    Namespace = "Test",
                    Input = Path.Combine("Swagger", "swagger-parameter-location.json"),
                    PayloadFlatteningThreshold = 3
                };
                var modeler = new SwaggerModeler();
                var clientModel = modeler.Build();
                SwaggerExtensions.NormalizeClientModel(clientModel);

                Assert.NotNull(clientModel);
                Assert.Equal(2, clientModel.Properties.Count);
                Assert.Equal(clientModel.Properties[0].Name, "SubscriptionId");
                Assert.Equal(clientModel.Properties[1].Name, "ApiVersion");
                Assert.False(
                    clientModel.Methods[0].Parameters.First(p => p.Name == "resourceGroupName").IsClientProperty);
                Assert.True(clientModel.Methods[0].Parameters.First(p => p.Name == "subscriptionId").IsClientProperty);
                Assert.True(clientModel.Methods[0].Parameters.First(p => p.Name == "apiVersion").IsClientProperty);
                Assert.False(
                    clientModel.Methods[1].Parameters.First(p => p.Name == "resourceGroupName").IsClientProperty);
                Assert.True(clientModel.Methods[1].Parameters.First(p => p.Name == "subscriptionId").IsClientProperty);
                Assert.True(clientModel.Methods[1].Parameters.First(p => p.Name == "apiVersion").IsClientProperty);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Create a new schema resolver in the context of the given swagger spec
        /// </summary>
        /// <param name="modeler">The swagger spec modeler</param>
        public SchemaResolver(SwaggerModeler modeler)
        {
            if (modeler == null)
            {
                throw new ArgumentNullException("modeler");
            }

            _serviceDefinition = modeler.ServiceDefinition;
        }
Esempio n. 7
0
        /// <summary>
        /// Create a new schema resolver in the context of the given swagger spec
        /// </summary>
        /// <param name="modeler">The swagger spec modeler</param>
        public SchemaResolver(SwaggerModeler modeler)
        {
            if (modeler == null)
            {
                throw new ArgumentNullException("modeler");
            }

            _serviceDefinition = modeler.ServiceDefinition;
        }
Esempio n. 8
0
        public void TestClientModelWithPayloadFlatteningViaXMSClientFlatten()
        {
            var setting = new Settings
            {
                Namespace = "Test",
                Input = Path.Combine("Swagger", "swagger-x-ms-client-flatten.json")
            };
            var modeler = new SwaggerModeler(setting);
            var clientModel = modeler.Build();
            SwaggerExtensions.NormalizeClientModel(clientModel, setting);

            Assert.NotNull(clientModel);
            Assert.Equal(8, clientModel.ModelTypes.Count);
            Assert.True(clientModel.ModelTypes.Any(m => m.Name == "BaseProduct"));
            Assert.True(clientModel.ModelTypes.Any(m => m.Name == "SimpleProduct"));
            Assert.True(clientModel.ModelTypes.Any(m => m.Name == "ConflictedProduct"));
            Assert.True(clientModel.ModelTypes.Any(m => m.Name == "ConflictedProductProperties")); // Since it's referenced in the response
            Assert.True(clientModel.ModelTypes.Any(m => m.Name == "RecursiveProduct"));
            Assert.True(clientModel.ModelTypes.Any(m => m.Name == "Error"));
            Assert.True(clientModel.ModelTypes.Any(m => m.Name == "ProductWithInheritance"));
            Assert.True(clientModel.ModelTypes.Any(m => m.Name == "BaseFlattenedProduct"));

            var simpleProduct = clientModel.ModelTypes.First(m => m.Name == "SimpleProduct");
            Assert.True(simpleProduct.Properties.Any(p => p.SerializedName == "details.max_product_display_name"
                                                       && p.Name == "max_product_display_name"));
            Assert.True(simpleProduct.Properties.Any(p => p.SerializedName == "details.max_product_capacity"
                                                       && p.Name == "max_product_capacity"));
            Assert.True(simpleProduct.Properties.Any(p => p.SerializedName == "details.max_product_image.@odata\\\\.value"
                                                       && p.Name == "@odata.value"));

            var conflictedProduct = clientModel.ModelTypes.First(m => m.Name == "ConflictedProduct");
            Assert.True(conflictedProduct.Properties.Any(p => p.SerializedName == "max_product_display_name"
                                                       && p.Name == "max_product_display_name"));
            Assert.True(conflictedProduct.Properties.Any(p => p.SerializedName == "details.max_product_display_name"
                                                       && p.Name == "ConflictedProductProperties_max_product_display_name"));
            Assert.True(conflictedProduct.Properties.Any(p => p.SerializedName == "simpleDetails.max_product_display_name"
                                                       && p.Name == "SimpleProductProperties_max_product_display_name"));
            Assert.True(conflictedProduct.Properties.Any(p => p.SerializedName == "details.base_product_description"
                                                       && p.Name == "ConflictedProduct_base_product_description"));

            var recursiveProduct = clientModel.ModelTypes.First(m => m.Name == "RecursiveProduct");
            Assert.True(recursiveProduct.Properties.Any(p => p.SerializedName == "properties.name"
                                                       && p.Name == "name"));
            Assert.True(recursiveProduct.Properties.Any(p => p.SerializedName == "properties.parent"
                                                       && p.Name == "parent"));

            var error = clientModel.ModelTypes.First(m => m.Name == "Error");
            Assert.Equal(3, error.Properties.Count);
            Assert.True(error.Properties.Any(p => p.SerializedName == "code" && p.Name == "code"));
            Assert.True(error.Properties.Any(p => p.SerializedName == "message" && p.Name == "message"));
            Assert.True(error.Properties.Any(p => p.SerializedName == "parentError" && p.Name == "parentError"));
            Assert.True(error.Properties.First(p => p.SerializedName == "parentError" && p.Name == "parentError").Type == error);
        }
Esempio n. 9
0
 public OperationBuilder(Operation operation, SwaggerModeler swaggerModeler)
 {
     if (operation == null)
     {
         throw new ArgumentNullException("operation");
     }
     if (swaggerModeler == null)
     {
         throw new ArgumentNullException("swaggerModeler");
     }
     this._operation         = operation;
     this._swaggerModeler    = swaggerModeler;
     this._effectiveProduces = operation.Produces.Any() ? operation.Produces : swaggerModeler.ServiceDefinition.Produces;
     this._effectiveConsumes = operation.Consumes.Any() ? operation.Consumes : swaggerModeler.ServiceDefinition.Consumes;
 }
        public OperationBuilder(Operation operation, SwaggerModeler swaggerModeler)
        {
            if (operation == null)
            {
                throw new ArgumentNullException("operation");
            }
            if (swaggerModeler == null)
            {
                throw new ArgumentNullException("swaggerModeler");
            }

            this._operation = operation;
            this._swaggerModeler = swaggerModeler;
            this._effectiveProduces = operation.Produces.Any() ? operation.Produces : swaggerModeler.ServiceDefinition.Produces;
            this._effectiveConsumes = operation.Consumes.Any() ? operation.Consumes : swaggerModeler.ServiceDefinition.Consumes;
        }
        public void ConvertsPageResultsToPageTypeTest()
        {
            using (NewContext)
            {
                var settings = new Settings
                {
                    Input = Path.Combine("Swagger", "azure-paging.json")
                };

                var modeler = new SwaggerModeler();
                var codeModel = modeler.Build();
                var plugin = new PluginCsa();
                using (plugin.Activate()) {
                    codeModel = plugin.Serializer.Load(codeModel);
                    codeModel = plugin.Transformer.TransformCodeModel(codeModel);

                    Assert.Equal(7, codeModel.Methods.Count);
                    Assert.Equal(1, codeModel.Methods.Count(m => m.Name == "GetSinglePage"));
                    Assert.Equal(0, codeModel.Methods.Count(m => m.Name == "GetSinglePageNext"));
                    Assert.Equal(1, codeModel.Methods.Count(m => m.Name == "PutSinglePage"));
                    Assert.Equal(1, codeModel.Methods.Count(m => m.Name == "PutSinglePageSpecialNext"));

                    Assert.Equal("Page<Product>", codeModel.Methods[0].ReturnType.Body.Name);
                    Assert.Equal("object", codeModel.Methods[1].ReturnType.Body.Name.ToLowerInvariant());
                    Assert.Equal("Page1<Product>", codeModel.Methods[1].Responses.ElementAt(0).Value.Body.Name);
                    Assert.Equal("string",
                        codeModel.Methods[1].Responses.ElementAt(1).Value.Body.Name.ToLowerInvariant());
                    Assert.Equal("object", codeModel.Methods[2].ReturnType.Body.Name.ToLowerInvariant());
                    Assert.Equal("Page1<Product>", codeModel.Methods[2].Responses.ElementAt(0).Value.Body.Name);
                    Assert.Equal("Page1<Product>", codeModel.Methods[2].Responses.ElementAt(1).Value.Body.Name);
                    Assert.Equal("object", codeModel.Methods[3].ReturnType.Body.Name.ToLowerInvariant());
                    Assert.Equal("Page1<Product>", codeModel.Methods[3].Responses.ElementAt(0).Value.Body.Name);
                    Assert.Equal("Page1<ProductChild>", codeModel.Methods[3].Responses.ElementAt(1).Value.Body.Name);
                    Assert.Equal(5, codeModel.ModelTypes.Count);
                    Assert.False(
                        codeModel.ModelTypes.Any(t => t.Name.EqualsIgnoreCase("ProducResult")));
                    Assert.False(
                        codeModel.ModelTypes.Any(t => t.Name.EqualsIgnoreCase("ProducResult2")));
                    Assert.False(
                        codeModel.ModelTypes.Any(t => t.Name.EqualsIgnoreCase("ProducResult3")));
                }
            }
        }
Esempio n. 12
0
        public void TestClientNameRubyNormalization()
        {
            var setting = new Settings
            {
                Namespace = "Test",
                Input = Path.Combine("Swagger", "swagger-x-ms-client-name.json")
            };

            var modeler = new SwaggerModeler(setting);
            var clientModel = modeler.Build();
            SwaggerExtensions.NormalizeClientModel(clientModel, setting);
            var namer = new RubyCodeNamer();
            namer.NormalizeClientModel(clientModel);

            Assert.NotNull(clientModel);
            Assert.Equal(2, clientModel.Methods.Count);

            Assert.Equal(2, clientModel.Methods[0].Parameters.Where(p => !p.IsClientProperty).Count());

            Assert.Equal("subscription", clientModel.Methods[0].Parameters[0].GetClientName());
            Assert.Equal("version", clientModel.Methods[0].Parameters[1].GetClientName());
            Assert.Equal("subscription", clientModel.Methods[0].Parameters[0].Name);
            Assert.Equal("version", clientModel.Methods[0].Parameters[1].Name);

            Assert.Equal(2, clientModel.Properties.Count);
            Assert.Equal(0, clientModel.Methods[1].Parameters.Where(p => !p.IsClientProperty).Count());

            Assert.Equal("subscription", clientModel.Properties[0].GetClientName());
            Assert.Equal("_version", clientModel.Properties[1].GetClientName());
            Assert.Equal("subscription", clientModel.Properties[0].Name);
            Assert.Equal("_version", clientModel.Properties[1].Name);

            var type = clientModel.ModelTypes.First();

            Assert.Equal("error_code", type.Properties[0].Name);
            Assert.Equal("error_message", type.Properties[1].Name);
            Assert.Equal("parent_error", type.Properties[2].Name);
        }
        public void PageableTest()
        {
            var settings = new Settings
            {
                Namespace = "Test",
                Input = @"Swagger\swagger-odata-spec.json"
            };


            var modeler = new SwaggerModeler(settings);
            var serviceClient = modeler.Build();
            var codeGen = new SampleAzureCodeGenerator(settings);
            codeGen.NormalizeClientModel(serviceClient);

            Assert.NotNull(serviceClient);
            Assert.Equal(3, serviceClient.Methods.Count);
            Assert.Equal("list", serviceClient.Methods[0].Name);
            Assert.Equal("listNext", serviceClient.Methods[2].Name);
            Assert.Equal(2, serviceClient.Methods[2].Parameters.Count);
            Assert.Equal("{nextLink}", serviceClient.Methods[2].Url);
            Assert.Equal("nextPageLink", serviceClient.Methods[2].Parameters[0].Name);
            Assert.Equal("accept-language", serviceClient.Methods[2].Parameters[1].Name);
            Assert.Equal(true, serviceClient.Methods[2].IsAbsoluteUrl);
            Assert.Equal(false, serviceClient.Methods[1].IsAbsoluteUrl);
        }
Esempio n. 14
0
        public void TestClientModelClientName()
        {
            using (NewContext)
            {
                var setting = new Settings
                {
                    Namespace = "Test",
                    Input = Path.Combine("Swagger", "swagger-x-ms-client-name.json")
                };

                var modeler = new SwaggerModeler();
                var clientModel = modeler.Build();
                SwaggerExtensions.NormalizeClientModel(clientModel);

                Assert.NotNull(clientModel);
                Assert.Equal(2, clientModel.Methods.Count);
                Assert.Equal(2, clientModel.Methods[0].Parameters.Where(p => !p.IsClientProperty).Count());
                Assert.Equal(0, clientModel.Methods[1].Parameters.Where(p => !p.IsClientProperty).Count());

                Assert.Equal("subscription", clientModel.Methods[0].Parameters[0].Name);
                Assert.Equal("version", clientModel.Methods[0].Parameters[1].Name);
                Assert.Equal("subscriptionId", clientModel.Methods[0].Parameters[0].Name.FixedValue);
                Assert.Equal("apiVersion", clientModel.Methods[0].Parameters[1].Name.FixedValue);

                Assert.Equal(2, clientModel.Properties.Count);
                Assert.Equal("Subscription", clientModel.Properties[0].Name);
                Assert.Equal("_version", clientModel.Properties[1].Name);
                Assert.Equal("subscriptionId", clientModel.Properties[0].Name.FixedValue);
                Assert.Equal("apiVersion", clientModel.Properties[1].Name.FixedValue);

                Assert.Equal(1, clientModel.ModelTypes.Count);

                var type = clientModel.ModelTypes.First();

                Assert.Equal("ErrorCode", type.Properties[0].Name);
                Assert.Equal("ErrorMessage", type.Properties[1].Name);
                Assert.Equal("ParentError", type.Properties[2].Name);

                Assert.Equal("code", type.Properties[0].Name.FixedValue);
                Assert.Equal("message", type.Properties[1].Name.FixedValue);
                Assert.Equal("parentError", type.Properties[2].Name.FixedValue);
            }
        }
Esempio n. 15
0
 public ObjectBuilder(SwaggerObject swaggerObject, SwaggerModeler modeler)
 {
     SwaggerObject = swaggerObject;
     Modeler = modeler;
 }
        public void PageableTest()
        {
            using (NewContext)
            {
                var settings = new Settings
                {
                    Namespace = "Test",
                    Input = @"Swagger\swagger-odata-spec.json"
                };


                var modeler = new SwaggerModeler();
                var codeModel = modeler.Build();
                var transformer = new SampleAzureTransformer();
                codeModel = transformer.TransformCodeModel(codeModel);

                Assert.NotNull(codeModel);
                Assert.Equal(3, codeModel.Methods.Count);
                Assert.Equal("List", codeModel.Methods[0].Name);
                Assert.Equal("ListNext", codeModel.Methods[2].Name);
                Assert.Equal(2, codeModel.Methods[2].Parameters.Count);
                Assert.Equal("{nextLink}", codeModel.Methods[2].Url);
                Assert.Equal("nextPageLink", codeModel.Methods[2].Parameters[0].Name);
                Assert.Equal("acceptLanguage", codeModel.Methods[2].Parameters[1].Name);
                Assert.Equal(true, codeModel.Methods[2].IsAbsoluteUrl);
                Assert.Equal(false, codeModel.Methods[1].IsAbsoluteUrl);
            }
        }
        public void FlatteningTest()
        {
            var settings = new Settings
            {
                Namespace = "Test",
                Input = @"Swagger\swagger-resource-flattening.json"
            };


            var modeler = new SwaggerModeler(settings);
            var serviceClient = modeler.Build();
            var codeGen = new SampleAzureCodeGenerator(settings);
            codeGen.NormalizeClientModel(serviceClient);

            Assert.NotNull(serviceClient);
            Assert.True(serviceClient.ModelTypes.Any(t => t.Name == "Product"));
            // ProductProperties type is not removed because it is referenced in response of one of the methods
            Assert.True(serviceClient.ModelTypes.Any(t => t.Name == "ProductProperties"));
            Assert.Equal(serviceClient.ModelTypes.First(t => t.Name == "ProductProperties").Properties.Count,
                serviceClient.ModelTypes.First(t => t.Name == "Product").Properties.Count);
            Assert.Equal("product_id", serviceClient.ModelTypes.First(t => t.Name == "ProductProperties").Properties[0].SerializedName);
            Assert.Equal("properties.product_id", serviceClient.ModelTypes.First(t => t.Name == "Product").Properties[0].SerializedName);
        }
Esempio n. 18
0
 public SchemaBuilder(Schema schema, SwaggerModeler modeler)
     : base(schema, modeler)
 {
     _schema = schema;
 }
Esempio n. 19
0
 public ParameterBuilder(SwaggerParameter swaggerParameter, SwaggerModeler modeler)
     : base(swaggerParameter, modeler)
 {
     _swaggerParameter = swaggerParameter;
 }
        public void SwaggerResourceExternalFalseTest()
        {
            var settings = new Settings
            {
                Namespace = "Test",
                Input = @"Swagger\resource-external-false.json"
            };


            var modeler = new SwaggerModeler(settings);
            var serviceClient = modeler.Build();
            var codeGen = new SampleAzureCodeGenerator(settings);
            codeGen.NormalizeClientModel(serviceClient);

            Assert.NotNull(serviceClient);
            var resource = serviceClient.ModelTypes.First(m =>
                m.Name.Equals("Resource", System.StringComparison.OrdinalIgnoreCase));
            Assert.True(resource.Extensions.ContainsKey(AzureExtensions.AzureResourceExtension));
            Assert.False((bool)resource.Extensions[AzureExtensions.AzureResourceExtension]);
            var flattenedProduct = serviceClient.ModelTypes.First(m =>
                m.Name.Equals("FlattenedProduct", System.StringComparison.OrdinalIgnoreCase));
            Assert.True(flattenedProduct.BaseModelType.Equals(resource));
        }
Esempio n. 21
0
 public ParameterBuilder(SwaggerParameter swaggerParameter, SwaggerModeler modeler)
     : base(swaggerParameter, modeler)
 {
     _swaggerParameter = swaggerParameter;
 }
        public void SwaggerODataSpecParsingTest()
        {
            using (NewContext)
            {
                var settings = new Settings
                {
                    Namespace = "Test",
                    Input = @"Swagger\swagger-odata-spec.json"
                };


                var modeler = new SwaggerModeler();
                var codeModel = modeler.Build();
                var transformer = new SampleAzureTransformer();
                codeModel = transformer.TransformCodeModel(codeModel);

                Assert.NotNull(codeModel);
                Assert.Equal(5, codeModel.Methods[0].Parameters.Count);
                Assert.Equal("$filter", codeModel.Methods[0].Parameters[2].Name.FixedValue);
                Assert.Equal("Product", codeModel.Methods[0].Parameters[2].ModelType.Name);
            }
        }
        public void SwaggerResourceExternalFalseTest()
        {
            using (NewContext)
            {
                var settings = new Settings
                {
                    Namespace = "Test",
                    Input = @"Swagger\resource-external-false.json"
                };


                var modeler = new SwaggerModeler();
                var codeModel = modeler.Build();
                var transformer = new SampleAzureTransformer();
                codeModel = transformer.TransformCodeModel(codeModel);
                Assert.NotNull(codeModel);
                var resource = codeModel.ModelTypes.First(m =>
                        m.Name.EqualsIgnoreCase("Resource"));
                Assert.True(resource.Extensions.ContainsKey(AzureExtensions.AzureResourceExtension));
                Assert.False((bool) resource.Extensions[AzureExtensions.AzureResourceExtension]);
                var flattenedProduct = codeModel.ModelTypes.First(m =>
                        m.Name.EqualsIgnoreCase("FlattenedProduct"));
                Assert.True(flattenedProduct.BaseModelType.Equals(resource));
            }
        }
        public void AzureParameterTest()
        {
            using (NewContext)
            {
                var settings = new Settings
                {
                    Namespace = "Test",
                    Input = @"Swagger\swagger-odata-spec.json"
                };


                var modeler = new SwaggerModeler();
                var codeModel = modeler.Build();
                var transformer = new SampleAzureTransformer();
                codeModel = transformer.TransformCodeModel(codeModel);


                Assert.NotNull(codeModel);
                Assert.Equal(3, codeModel.Methods.Count);
                Assert.Equal(5, codeModel.Methods[0].Parameters.Count);
                Assert.Equal("List", codeModel.Methods[0].Name);
                Assert.Equal(4, codeModel.Methods[1].Parameters.Count);
                Assert.Equal("Reset", codeModel.Methods[1].Name);
                Assert.Equal("subscriptionId", codeModel.Methods[0].Parameters[0].Name);
                Assert.Equal("resourceGroupName", codeModel.Methods[0].Parameters[1].Name);
                Assert.Equal("$filter", codeModel.Methods[0].Parameters[2].Name.FixedValue);
                Assert.Equal("accept-language", codeModel.Methods[0].Parameters[4].Name.FixedValue);
                Assert.Equal("resourceGroupName", codeModel.Methods[1].Parameters[1].Name);
                Assert.Equal("apiVersion", codeModel.Methods[1].Parameters[2].Name);
            }
        }
        public void AzureParameterTest()
        {
            var settings = new Settings
            {
                Namespace = "Test",
                Input = @"Swagger\swagger-odata-spec.json"
            };


            var modeler = new SwaggerModeler(settings);
            var serviceClient = modeler.Build();
            var codeGen = new SampleAzureCodeGenerator(settings);
            codeGen.NormalizeClientModel(serviceClient);

            Assert.NotNull(serviceClient);
            Assert.Equal(3, serviceClient.Methods.Count);
            Assert.Equal(5, serviceClient.Methods[0].Parameters.Count);
            Assert.Equal("list", serviceClient.Methods[0].Name);
            Assert.Equal(4, serviceClient.Methods[1].Parameters.Count);
            Assert.Equal("reset", serviceClient.Methods[1].Name);
            Assert.Equal("subscriptionId", serviceClient.Methods[0].Parameters[0].Name);
            Assert.Equal("resourceGroupName", serviceClient.Methods[0].Parameters[1].Name);
            Assert.Equal("$filter", serviceClient.Methods[0].Parameters[2].Name);
            Assert.Equal("accept-language", serviceClient.Methods[0].Parameters[4].Name);
            Assert.Equal("resourceGroupName", serviceClient.Methods[1].Parameters[1].Name);
            Assert.Equal("apiVersion", serviceClient.Methods[1].Parameters[2].Name);
        }
 public SchemaBuilder(Schema schema, SwaggerModeler modeler)
     : base(schema, modeler)
 {
     _schema = schema;
 }
Esempio n. 27
0
        public static void ProcessParameterizedHost(ServiceClient serviceClient, Settings settings)
        {
            if (serviceClient == null)
            {
                throw new ArgumentNullException("serviceClient");
            }

            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            if (serviceClient.Extensions.ContainsKey(ParameterizedHostExtension) && !hostChecked)
            { 
                SwaggerModeler modeler = new SwaggerModeler(settings);
                modeler.Build();
                var hostExtension = serviceClient.Extensions[ParameterizedHostExtension] as JObject;

                if (hostExtension != null)
                {
                    var hostTemplate = (string)hostExtension["hostTemplate"];
                    var parametersJson = hostExtension["parameters"].ToString();
                    var useSchemePrefix = true;
                    if (hostExtension[UseSchemePrefix] != null)
                    {
                        useSchemePrefix = bool.Parse(hostExtension[UseSchemePrefix].ToString());
                    }

                    var position = "first";
                    
                    if (hostExtension[PositionInOperation] != null)
                    {
                        var pat = "^(fir|la)st$";
                        Regex r = new Regex(pat, RegexOptions.IgnoreCase);
                        var text = hostExtension[PositionInOperation].ToString();
                        Match m = r.Match(text);
                        if (!m.Success)
                        {
                            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, 
                                Resources.InvalidExtensionProperty, text, PositionInOperation, ParameterizedHostExtension, "first, last"));
                        }
                        position = text;
                    }

                    if (!string.IsNullOrEmpty(parametersJson))
                    {
                        var jsonSettings = new JsonSerializerSettings
                        {
                            TypeNameHandling = TypeNameHandling.None,
                            MetadataPropertyHandling = MetadataPropertyHandling.Ignore
                        };

                        var swaggerParams = JsonConvert.DeserializeObject<List<SwaggerParameter>>(parametersJson, jsonSettings);
                        List<Parameter> hostParamList = new List<Parameter>();
                        foreach (var swaggerParameter in swaggerParams)
                        {
                            // Build parameter
                            var parameterBuilder = new ParameterBuilder(swaggerParameter, modeler);
                            var parameter = parameterBuilder.Build();

                            // check to see if the parameter exists in properties, and needs to have its name normalized
                            if (serviceClient.Properties.Any(p => p.SerializedName.Equals(parameter.SerializedName)))
                            {
                                parameter.ClientProperty = serviceClient.Properties.Single(p => p.SerializedName.Equals(parameter.SerializedName));
                            }
                            parameter.Extensions["hostParameter"] = true;
                            hostParamList.Add(parameter);
                        }

                        foreach (var method in serviceClient.Methods)
                        {
                            if (position.Equals("first", StringComparison.OrdinalIgnoreCase))
                            {
                                method.Parameters.InsertRange(0, hostParamList);
                            }
                            else
                            {
                                method.Parameters.AddRange(hostParamList);
                            }
                            
                        }
                        if (useSchemePrefix)
                        {
                            serviceClient.BaseUrl = string.Format(CultureInfo.InvariantCulture, "{0}://{1}{2}",
                                modeler.ServiceDefinition.Schemes[0].ToString().ToLowerInvariant(),
                                hostTemplate, modeler.ServiceDefinition.BasePath);
                        }
                        else
                        {
                            serviceClient.BaseUrl = string.Format(CultureInfo.InvariantCulture, "{0}{1}",
                                hostTemplate, modeler.ServiceDefinition.BasePath);
                        }
                        
                    }
                }
            }

            hostChecked = true;
        }
        public void FlatteningTest()
        {
            using (NewContext)
            {
                var settings = new Settings
                {
                    Namespace = "Test",
                    Input = @"Swagger\swagger-resource-flattening.json"
                };


                var modeler = new SwaggerModeler();
                var codeModel = modeler.Build();
                var transformer = new SampleAzureTransformer();
                codeModel = transformer.TransformCodeModel(codeModel);
                Assert.NotNull(codeModel);
                Assert.True(codeModel.ModelTypes.Any(t => t.Name == "Product"));
                // ProductProperties type is not removed because it is referenced in response of one of the methods
                Assert.True(codeModel.ModelTypes.Any(t => t.Name == "ProductProperties"));
                Assert.Equal(codeModel.ModelTypes.First(t => t.Name == "ProductProperties").Properties.Count,
                    codeModel.ModelTypes.First(t => t.Name == "Product").Properties.Count);
                Assert.Equal("product_id",
                    codeModel.ModelTypes.First(t => t.Name == "ProductProperties").Properties[0].SerializedName);
                Assert.Equal("properties.product_id",
                    codeModel.ModelTypes.First(t => t.Name == "Product").Properties[0].SerializedName);
            }
        }
Esempio n. 29
0
 public ObjectBuilder(SwaggerObject swaggerObject, SwaggerModeler modeler)
 {
     SwaggerObject = swaggerObject;
     Modeler       = modeler;
 }
        public void SwaggerODataSpecParsingTest()
        {
            var settings = new Settings
            {
                Namespace = "Test",
                Input = @"Swagger\swagger-odata-spec.json"
            };


            var modeler = new SwaggerModeler(settings);
            var serviceClient = modeler.Build();
            var codeGen = new SampleAzureCodeGenerator(settings);
            codeGen.NormalizeClientModel(serviceClient);

            Assert.NotNull(serviceClient);
            Assert.Equal(5, serviceClient.Methods[0].Parameters.Count);
            Assert.Equal("$filter", serviceClient.Methods[0].Parameters[2].Name);
            Assert.Equal("Product", serviceClient.Methods[0].Parameters[2].Type.Name);
        }