コード例 #1
0
        public void TestcodeModelWithStreamAndByteArray()
        {
            var input     = Path.Combine(CodeBaseDirectory, "Resource", "Swagger", "swagger-streaming.json");
            var modeler   = new SwaggerModeler();
            var codeModel = modeler.Build(SwaggerParser.Parse(File.ReadAllText(input)));

            Assert.NotNull(codeModel);
            Assert.Equal("GetWithStreamFormData", codeModel.Methods[0].Name);
            Assert.Equal("Stream", codeModel.Methods[0].Parameters[0].ModelType.Name);
            Assert.Equal("Stream", CreateCSharpResponseType(codeModel.Methods[0].ReturnType));
            Assert.Equal("Stream", CreateCSharpResponseType(codeModel.Methods[0].Responses[HttpStatusCode.OK]));

            Assert.Equal("PostWithByteArrayFormData", codeModel.Methods[1].Name);
            Assert.Equal("ByteArray", codeModel.Methods[1].Parameters[0].ModelType.Name);
            Assert.Equal("ByteArray", CreateCSharpResponseType(codeModel.Methods[1].ReturnType));
            Assert.Equal("ByteArray", CreateCSharpResponseType(codeModel.Methods[1].Responses[HttpStatusCode.OK]));

            Assert.Equal("GetWithStream", codeModel.Methods[2].Name);
            Assert.Equal("Stream", CreateCSharpResponseType(codeModel.Methods[2].ReturnType));
            Assert.Equal("Stream", CreateCSharpResponseType(codeModel.Methods[2].Responses[HttpStatusCode.OK]));

            Assert.Equal("PostWithByteArray", codeModel.Methods[3].Name);
            Assert.Equal("ByteArray", CreateCSharpResponseType(codeModel.Methods[3].ReturnType));
            Assert.Equal("ByteArray", CreateCSharpResponseType(codeModel.Methods[3].Responses[HttpStatusCode.OK]));
        }
コード例 #2
0
        public void TestcodeModelWithDifferentReturnsTypesBasedOnStatusCode()
        {
            using (NewContext)
            {
                new Settings
                {
                    Namespace = "Test",
                    Input     = Path.Combine("Swagger", "swagger-multiple-response-schemas.json")
                };
                var modeler   = new SwaggerModeler();
                var codeModel = modeler.Build();

                Assert.NotNull(codeModel);
                Assert.Equal("GetSameResponse", codeModel.Methods[0].Name);
                Assert.Equal("IList<Pet>", CreateCSharpResponseType(codeModel.Methods[0].ReturnType));
                Assert.Equal("IList<Pet>", CreateCSharpResponseType(codeModel.Methods[0].Responses[HttpStatusCode.OK]));
                Assert.Equal("IList<Pet>", CreateCSharpResponseType(codeModel.Methods[0].Responses[HttpStatusCode.Accepted]));

                Assert.Equal("PostInheretedTypes", codeModel.Methods[1].Name);
                Assert.Equal("Pet", CreateCSharpResponseType(codeModel.Methods[1].ReturnType));
                Assert.Equal("Dog", CreateCSharpResponseType(codeModel.Methods[1].Responses[HttpStatusCode.OK]));
                Assert.Equal("Cat", CreateCSharpResponseType(codeModel.Methods[1].Responses[HttpStatusCode.Accepted]));

                Assert.Equal("PatchDifferentStreamTypesNoContent", codeModel.Methods[6].Name);
                Assert.Equal("VirtualMachineGetRemoteDesktopFileResponse", CreateCSharpResponseType(codeModel.Methods[6].ReturnType));
                Assert.Equal("VirtualMachineGetRemoteDesktopFileResponse", CreateCSharpResponseType(codeModel.Methods[6].Responses[HttpStatusCode.OK]));
                Assert.Null(codeModel.Methods[6].Responses[HttpStatusCode.NoContent].Body);
            }
        }
コード例 #3
0
        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);
            }
        }
コード例 #4
0
        public void TestcodeModelPolymorhism()
        {
            using (NewContext)
            {
                new Settings
                {
                    Namespace = "Test",
                    Input     = Path.Combine("Swagger", "swagger-polymorphism.json")
                };
                var modeler   = new SwaggerModeler();
                var codeModel = modeler.Build();

                Assert.NotNull(codeModel);
                Assert.Equal("Pet", codeModel.ModelTypes.First(m => m.Name == "Pet").Name);
                Assert.Equal("dtype", codeModel.ModelTypes.First(m => m.Name == "Pet").PolymorphicDiscriminator);
                Assert.Equal(2, codeModel.ModelTypes.First(m => m.Name == "Pet").Properties.Count);
                Assert.Equal("Id", codeModel.ModelTypes.First(m => m.Name == "Pet").Properties[0].Name);
                Assert.Equal("Description", codeModel.ModelTypes.First(m => m.Name == "Pet").Properties[1].Name);
                Assert.Equal("Cat", codeModel.ModelTypes.First(m => m.Name == "Cat").Name);
                Assert.Equal("Pet", codeModel.ModelTypes.First(m => m.Name == "Cat").BaseModelType.Name);
                Assert.Equal(1, codeModel.ModelTypes.First(m => m.Name == "Cat").Properties.Count);
                Assert.Equal("Lizard", codeModel.ModelTypes.First(m => m.Name == "Lizard").Name);
                Assert.Equal("lzd", codeModel.ModelTypes.First(m => m.Name == "Lizard").SerializedName);
            }
        }
コード例 #5
0
        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);
            }
        }
コード例 #6
0
        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);
            }
        }
コード例 #7
0
        public void TestcodeModelWithResponseHeaders()
        {
            using (NewContext)
            {
                new Settings
                {
                    Namespace = "Test",
                    Input     = Path.Combine("Swagger", "swagger-response-headers.json")
                };
                var modeler   = new SwaggerModeler();
                var codeModel = modeler.Build();

                Assert.NotNull(codeModel);
                Assert.Equal(2, codeModel.Methods.Count);
                Assert.Equal(2, codeModel.Methods[0].Responses.Count);
                Assert.Equal("ListHeaders", codeModel.Methods[0].Responses[HttpStatusCode.OK].Headers.Name);
                Assert.Equal(3, ((CompositeType)codeModel.Methods[0].Responses[HttpStatusCode.OK].Headers).Properties.Count);
                Assert.Equal("ListHeaders", codeModel.Methods[0].Responses[HttpStatusCode.Created].Headers.Name);
                Assert.Equal(3, ((CompositeType)codeModel.Methods[0].Responses[HttpStatusCode.Created].Headers).Properties.Count);
                Assert.Equal("ListHeaders", codeModel.Methods[0].ReturnType.Headers.Name);
                Assert.Equal(3, ((CompositeType)codeModel.Methods[0].ReturnType.Headers).Properties.Count);

                Assert.Equal(1, codeModel.Methods[1].Responses.Count);
                Assert.Equal("CreateHeaders", codeModel.Methods[1].Responses[HttpStatusCode.OK].Headers.Name);
                Assert.Equal(3, ((CompositeType)codeModel.Methods[1].Responses[HttpStatusCode.OK].Headers).Properties.Count);
                Assert.Equal("CreateHeaders", codeModel.Methods[1].ReturnType.Headers.Name);
                Assert.Equal(3, ((CompositeType)codeModel.Methods[1].ReturnType.Headers).Properties.Count);
                Assert.True(codeModel.HeaderTypes.Any(c => c.Name == "ListHeaders"));
                Assert.True(codeModel.HeaderTypes.Any(c => c.Name == "CreateHeaders"));
            }
        }
コード例 #8
0
        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));
            }
        }
コード例 #9
0
        public void TestClientModelWithDifferentReturnsTypesBasedOnStatusCode()
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input     = Path.Combine("Swagger", "swagger-multiple-response-schemas.json")
            });
            var clientModel = modeler.Build();

            Assert.NotNull(clientModel);
            Assert.Equal("getSameResponse", clientModel.Methods[0].Name);
            Assert.Equal("IList<pet>", clientModel.Methods[0].ReturnType.ToString());
            Assert.Equal("IList<pet>", clientModel.Methods[0].Responses[HttpStatusCode.OK].ToString());
            Assert.Equal("IList<pet>", clientModel.Methods[0].Responses[HttpStatusCode.Accepted].ToString());

            Assert.Equal("postInheretedTypes", clientModel.Methods[1].Name);
            Assert.Equal("pet", clientModel.Methods[1].ReturnType.ToString());
            Assert.Equal("dog", clientModel.Methods[1].Responses[HttpStatusCode.OK].ToString());
            Assert.Equal("cat", clientModel.Methods[1].Responses[HttpStatusCode.Accepted].ToString());

            Assert.Equal("patchDifferentStreamTypesNoContent", clientModel.Methods[6].Name);
            Assert.Equal("VirtualMachineGetRemoteDesktopFileResponse", clientModel.Methods[6].ReturnType.ToString());
            Assert.Equal("VirtualMachineGetRemoteDesktopFileResponse",
                         clientModel.Methods[6].Responses[HttpStatusCode.OK].ToString());
            Assert.Null(clientModel.Methods[6].Responses[HttpStatusCode.NoContent].Body);
        }
コード例 #10
0
        public void TestcodeModelWithStreamAndByteArray()
        {
            using (NewContext)
            {
                new Settings
                {
                    Namespace = "Test",
                    Input     = Path.Combine("Swagger", "swagger-streaming.json")
                };
                var modeler   = new SwaggerModeler();
                var codeModel = modeler.Build();

                Assert.NotNull(codeModel);
                Assert.Equal("GetWithStreamFormData", codeModel.Methods[0].Name);
                Assert.Equal("Stream", codeModel.Methods[0].Parameters[0].ModelType.Name);
                Assert.Equal("Stream", CreateCSharpResponseType(codeModel.Methods[0].ReturnType));
                Assert.Equal("Stream", CreateCSharpResponseType(codeModel.Methods[0].Responses[HttpStatusCode.OK]));

                Assert.Equal("PostWithByteArrayFormData", codeModel.Methods[1].Name);
                Assert.Equal("ByteArray", codeModel.Methods[1].Parameters[0].ModelType.Name);
                Assert.Equal("ByteArray", CreateCSharpResponseType(codeModel.Methods[1].ReturnType));
                Assert.Equal("ByteArray", CreateCSharpResponseType(codeModel.Methods[1].Responses[HttpStatusCode.OK]));

                Assert.Equal("GetWithStream", codeModel.Methods[2].Name);
                Assert.Equal("Stream", CreateCSharpResponseType(codeModel.Methods[2].ReturnType));
                Assert.Equal("Stream", CreateCSharpResponseType(codeModel.Methods[2].Responses[HttpStatusCode.OK]));

                Assert.Equal("PostWithByteArray", codeModel.Methods[3].Name);
                Assert.Equal("ByteArray", CreateCSharpResponseType(codeModel.Methods[3].ReturnType));
                Assert.Equal("ByteArray", CreateCSharpResponseType(codeModel.Methods[3].Responses[HttpStatusCode.OK]));
            }
        }
コード例 #11
0
        public void TestClientModelWithStreamAndByteArray()
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input     = Path.Combine("Swagger", "swagger-streaming.json")
            });
            var clientModel = modeler.Build();

            Assert.NotNull(clientModel);
            Assert.Equal("GetWithStreamFormData", clientModel.Methods[0].Name);
            Assert.Equal("Stream", clientModel.Methods[0].Parameters[0].Type.Name);
            Assert.Equal("Stream", clientModel.Methods[0].ReturnType.ToString());
            Assert.Equal("Stream", clientModel.Methods[0].Responses[HttpStatusCode.OK].ToString());

            Assert.Equal("PostWithByteArrayFormData", clientModel.Methods[1].Name);
            Assert.Equal("ByteArray", clientModel.Methods[1].Parameters[0].Type.Name);
            Assert.Equal("ByteArray", clientModel.Methods[1].ReturnType.ToString());
            Assert.Equal("ByteArray", clientModel.Methods[1].Responses[HttpStatusCode.OK].ToString());

            Assert.Equal("GetWithStream", clientModel.Methods[2].Name);
            Assert.Equal("Stream", clientModel.Methods[2].ReturnType.ToString());
            Assert.Equal("Stream", clientModel.Methods[2].Responses[HttpStatusCode.OK].ToString());

            Assert.Equal("PostWithByteArray", clientModel.Methods[3].Name);
            Assert.Equal("ByteArray", clientModel.Methods[3].ReturnType.ToString());
            Assert.Equal("ByteArray", clientModel.Methods[3].Responses[HttpStatusCode.OK].ToString());
        }
コード例 #12
0
        public void TestConstants()
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input     = @"Swagger\swagger-validation.json"
            });
            var clientModel = modeler.Build();

            Assert.Equal("myintconst", clientModel.Methods[0].Parameters[4].Name);
            Assert.Equal(true, clientModel.Methods[0].Parameters[4].Type.IsPrimaryType(KnownPrimaryType.Int));
            Assert.Equal(true, clientModel.Methods[0].Parameters[4].IsConstant);
            Assert.Equal("0", clientModel.Methods[0].Parameters[4].DefaultValue);

            Assert.Equal("mystrconst", clientModel.Methods[0].Parameters[5].Name);
            Assert.Equal(true, clientModel.Methods[0].Parameters[5].Type.IsPrimaryType(KnownPrimaryType.String));
            Assert.Equal(true, clientModel.Methods[0].Parameters[5].IsConstant);
            Assert.Equal("constant", clientModel.Methods[0].Parameters[5].DefaultValue);

            Assert.Equal("myintconst", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[5].Name);
            Assert.Equal(true, clientModel.ModelTypes.First(m => m.Name == "Product").Properties[5].Type.IsPrimaryType(KnownPrimaryType.Int));
            Assert.Equal(true, clientModel.ModelTypes.First(m => m.Name == "Product").Properties[5].IsConstant);
            Assert.Equal("0", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[5].DefaultValue);

            Assert.Equal("mystrconst", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[6].Name);
            Assert.Equal(true, clientModel.ModelTypes.First(m => m.Name == "Product").Properties[6].Type.IsPrimaryType(KnownPrimaryType.String));
            Assert.Equal(true, clientModel.ModelTypes.First(m => m.Name == "Product").Properties[6].IsConstant);
            Assert.Equal("constant", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[6].DefaultValue);

            Assert.Equal(true, clientModel.ModelTypes.First(m => m.Name == "Product").ContainsConstantProperties);
            Assert.Equal(false, clientModel.ModelTypes.First(m => m.Name == "Error").ContainsConstantProperties);
        }
コード例 #13
0
        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);
        }
コード例 #14
0
        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);
        }
コード例 #15
0
        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));
        }
コード例 #16
0
        public void TestYamlParsing()
        {
            var input     = Path.Combine(CodeBaseDirectory, "Resource", "Swagger", "swagger-simple-spec.yaml");
            var modeler   = new SwaggerModeler();
            var codeModel = modeler.Build(SwaggerParser.Parse(File.ReadAllText(input)));

            Assert.NotNull(codeModel);
        }
コード例 #17
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();

            Extensions.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);
        }
コード例 #18
0
        public void TestcodeModelFromSimpleSwagger()
        {
            var input     = Path.Combine(CodeBaseDirectory, "Resource", "Swagger", "swagger-simple-spec.json");
            var modeler   = new SwaggerModeler();
            var codeModel = modeler.Build(SwaggerParser.Parse(File.ReadAllText(input)));

            var description =
                "The Products endpoint returns information about the Uber products offered at a given location. The response includes the display name and other details about each product, and lists the products in the proper display order.";
            var summary = "Product Types";

            Assert.NotNull(codeModel);
            Assert.Equal(2, codeModel.Properties.Count);
            Assert.True(
                codeModel.Properties.Any(p => p.Name.EqualsIgnoreCase("subscriptionId")));
            Assert.True(
                codeModel.Properties.Any(p => p.Name.EqualsIgnoreCase("apiVersion")));
            Assert.Equal("2014-04-01-preview", codeModel.ApiVersion);
            Assert.Equal("https://management.azure.com/", codeModel.BaseUrl);
            Assert.Equal("Some cool documentation.", codeModel.Documentation);
            //var allMethods = codeModel.Operations.SelectMany(each => each.Methods);
            Assert.Equal(2, codeModel.Methods.Count);
            Assert.Equal("List", codeModel.Methods[0].Name);
            Assert.NotEmpty(codeModel.Methods[0].Description);
            Assert.Equal(description, codeModel.Methods[0].Description);
            Assert.NotEmpty(codeModel.Methods[0].Summary);
            Assert.Equal(summary, codeModel.Methods[0].Summary);
            Assert.Equal(HttpMethod.Get, codeModel.Methods[0].HttpMethod);
            Assert.Equal(3, codeModel.Methods[0].Parameters.Count);
            Assert.Equal("subscriptionId", codeModel.Methods[0].Parameters[0].Name);
            Assert.NotNull(codeModel.Methods[0].Parameters[0].ClientProperty);
            Assert.Equal("resourceGroupName", codeModel.Methods[0].Parameters[1].Name);
            Assert.Equal("resourceGroupName", codeModel.Methods[0].Parameters[1].SerializedName);
            Assert.Equal("Resource Group ID.", codeModel.Methods[0].Parameters[1].Documentation);
            Assert.Equal(true, codeModel.Methods[0].Parameters[0].IsRequired);
            Assert.Equal(ParameterLocation.Path, codeModel.Methods[0].Parameters[0].Location);
            Assert.Equal("String", codeModel.Methods[0].Parameters[0].ModelType.Name);
            Assert.Equal("Reset", codeModel.Methods[1].Name);
            Assert.Equal("Product", codeModel.ModelTypes.First(m => m.Name == "Product").Name);
            Assert.Equal("Product", codeModel.ModelTypes.First(m => m.Name == "Product").SerializedName);
            Assert.Equal("The product title.", codeModel.ModelTypes.First(m => m.Name == "Product").Summary);
            Assert.Equal("The product documentation.",
                         codeModel.ModelTypes.First(m => m.Name == "Product").Documentation);
            Assert.Equal("A product id.",
                         codeModel.ModelTypes.First(m => m.Name == "Product").Properties[0].Summary);
            Assert.Equal("ProductId", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[0].Name);
            Assert.Equal("product_id",
                         codeModel.ModelTypes.First(m => m.Name == "Product").Properties[0].SerializedName);
            Assert.Null(codeModel.Methods[1].ReturnType.Body);
            Assert.Null(codeModel.Methods[1].Responses[HttpStatusCode.NoContent].Body);
            Assert.Equal(3, codeModel.Methods[1].Parameters.Count);
            Assert.Equal("subscriptionId", codeModel.Methods[1].Parameters[0].Name);
            Assert.Null(codeModel.Methods[1].Parameters[0].ClientProperty);
            Assert.Equal("resourceGroupName", codeModel.Methods[1].Parameters[1].Name);
            Assert.Equal("apiVersion", codeModel.Methods[1].Parameters[2].Name);

            Assert.Equal("Capacity", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[3].Name);
            Assert.Equal("100", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[3].DefaultValue);
        }
コード例 #19
0
        // [Fact] skipping - test runner dies.
        public void codeModelWithCircularDependencyThrowsError()
        {
            var input   = Path.Combine(CodeBaseDirectory, "Resource", "Swagger", "swagger-allOf-circular.json");
            var modeler = new SwaggerModeler();
            var ex      = Assert.Throws <InvalidOperationException>(() => modeler.Build(SwaggerParser.Parse(File.ReadAllText(input))));

            Assert.Contains("circular", ex.Message, StringComparison.OrdinalIgnoreCase);
            Assert.Contains("siamese", ex.Message, StringComparison.OrdinalIgnoreCase);
        }
コード例 #20
0
 public void ClientModelWithCircularDependencyThrowsError()
 {
     var modeler = new SwaggerModeler(new Settings
     {
         Namespace = "Test",
         Input = Path.Combine("Swagger", "swagger-allOf-circular.json")
     });
     Assert.Throws<ArgumentException>(() => modeler.Build());
 }
コード例 #21
0
        public void GlobalResponsesReference()
        {
            var input     = Path.Combine(CodeBaseDirectory, "Resource", "Swagger", "swagger-global-responses.json");
            var modeler   = new SwaggerModeler();
            var codeModel = modeler.Build(SwaggerParser.Parse(File.ReadAllText(input)));

            Assert.Equal(1, codeModel.Methods[0].Responses.Count);
            Assert.NotNull(codeModel.Methods[0].Responses[HttpStatusCode.OK]);
        }
コード例 #22
0
        public void TestCompositeConstants()
        {
            var input     = Path.Combine(CodeBaseDirectory, "Resource", "Swagger", "swagger-composite-constants.json");
            var modeler   = new SwaggerModeler();
            var codeModel = modeler.Build(SwaggerParser.Parse(File.ReadAllText(input)));

            Assert.Equal(false, codeModel.ModelTypes.First(m => m.Name == "NetworkInterfaceIPConfigurationPropertiesFormat").ContainsConstantProperties);
            Assert.Equal(false, codeModel.ModelTypes.First(m => m.Name == "IPConfigurationPropertiesFormat").ContainsConstantProperties);
        }
コード例 #23
0
        public void TestConstants()
        {
            using (NewContext)
            {
                new Settings
                {
                    Namespace = "Test",
                    Input     = @"Swagger\swagger-validation.json"
                };
                var modeler   = new SwaggerModeler();
                var codeModel = modeler.Build();

                Assert.Equal("myintconst", codeModel.Methods[0].Parameters[4].Name);
                Assert.Equal(true, codeModel.Methods[0].Parameters[4].ModelType.IsPrimaryType(KnownPrimaryType.Int));
                Assert.Equal(true, codeModel.Methods[0].Parameters[4].IsConstant);
                Assert.Equal("0", codeModel.Methods[0].Parameters[4].DefaultValue);

                Assert.Equal("mystrconst", codeModel.Methods[0].Parameters[5].Name);
                Assert.Equal(true, codeModel.Methods[0].Parameters[5].ModelType.IsPrimaryType(KnownPrimaryType.String));
                Assert.Equal(true, codeModel.Methods[0].Parameters[5].IsConstant);
                Assert.Equal("constant", codeModel.Methods[0].Parameters[5].DefaultValue);

                Assert.Equal("Myintconst", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[5].Name);
                Assert.Equal(true, codeModel.ModelTypes.First(m => m.Name == "Product").Properties[5].ModelType.IsPrimaryType(KnownPrimaryType.Int));
                Assert.Equal(true, codeModel.ModelTypes.First(m => m.Name == "Product").Properties[5].IsConstant);
                Assert.Equal("0", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[5].DefaultValue);

                Assert.Equal("Mystrconst", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[6].Name);
                Assert.Equal(true, codeModel.ModelTypes.First(m => m.Name == "Product").Properties[6].ModelType.IsPrimaryType(KnownPrimaryType.String));
                Assert.Equal(true, codeModel.ModelTypes.First(m => m.Name == "Product").Properties[6].IsConstant);
                Assert.Equal("constant", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[6].DefaultValue);

                Assert.Equal("RefStrEnumRequiredConstant", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[7].Name);
                Assert.Equal(true, codeModel.ModelTypes.First(m => m.Name == "Product").Properties[7].ModelType.IsPrimaryType(KnownPrimaryType.String));
                Assert.Equal(true, codeModel.ModelTypes.First(m => m.Name == "Product").Properties[7].IsConstant);
                Assert.Equal("ReferenceEnum1", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[7].DefaultValue);

                Assert.Equal("RefIntEnumRequiredConstant", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[8].Name);
                Assert.Equal(true, codeModel.ModelTypes.First(m => m.Name == "Product").Properties[8].ModelType.IsPrimaryType(KnownPrimaryType.Int));
                Assert.Equal(true, codeModel.ModelTypes.First(m => m.Name == "Product").Properties[8].IsConstant);
                Assert.Equal("0", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[8].DefaultValue);

                Assert.Equal("RefStrEnum", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[9].Name);
                Assert.Equal("enum", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[9].ModelType.Name);
                Assert.Equal(false, codeModel.ModelTypes.First(m => m.Name == "Product").Properties[9].IsConstant);
                Assert.True(codeModel.ModelTypes.First(m => m.Name == "Product").Properties[9].DefaultValue.IsNullOrEmpty());

                Assert.Equal("RefIntEnum", codeModel.ModelTypes.First(m => m.Name == "Product").Properties[10].Name);
                Assert.Equal(true, codeModel.ModelTypes.First(m => m.Name == "Product").Properties[10].ModelType.IsPrimaryType(KnownPrimaryType.Int));
                Assert.Equal(false, codeModel.ModelTypes.First(m => m.Name == "Product").Properties[10].IsConstant);
                Assert.True(codeModel.ModelTypes.First(m => m.Name == "Product").Properties[10].DefaultValue.IsNullOrEmpty());

                Assert.Equal(true, codeModel.ModelTypes.First(m => m.Name == "Product").ContainsConstantProperties);
                Assert.Equal(false, codeModel.ModelTypes.First(m => m.Name == "Error").ContainsConstantProperties);
            }
        }
コード例 #24
0
        public void DefaultReturnsCorrectType()
        {
            var input     = Path.Combine(CodeBaseDirectory, "Resource", "Swagger", "swagger-multiple-response-schemas.json");
            var modeler   = new SwaggerModeler();
            var codeModel = modeler.Build(SwaggerParser.Parse(File.ReadAllText(input)));

            var retType = codeModel.Methods.First(m => m.Name == "PatchDefaultResponse");

            Assert.Equal("Pet", CreateCSharpResponseType(retType.ReturnType));
        }
コード例 #25
0
        public void TestCustomPaths()
        {
            var input     = Path.Combine(CodeBaseDirectory, "Resource", "Swagger", "swagger-x-ms-paths.json");
            var modeler   = new SwaggerModeler();
            var codeModel = modeler.Build(SwaggerParser.Parse(File.ReadAllText(input)));

            Assert.NotNull(codeModel);
            Assert.Equal(3, codeModel.Methods.Count());
            Assert.True(codeModel.Methods.All(m => m.Url == "/values/foo"));
        }
コード例 #26
0
        public void ClientModelWithCircularDependencyThrowsError()
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input     = Path.Combine("Swagger", "swagger-allOf-circular.json")
            });

            Assert.Throws <ArgumentException>(() => modeler.Build());
        }
コード例 #27
0
        /// <summary>
        /// Helper method -- load two Swagger documents and invoke the comparison logic.
        /// </summary>
        /// <param name="input">The name of the swagger document file. The file name must be the same in both the 'modified' and 'original' folder.</param>
        /// <returns>A list of messages from the comparison logic.</returns>
        private IEnumerable <ComparisonMessage> CompareSwagger(string input)
        {
            var modeler = new SwaggerModeler();
            var baseDir = Directory.GetParent(typeof(SwaggerModelerCompareTests).GetTypeInfo().Assembly.Location.ToString()).ToString();

            return(modeler.Compare(
                       File.ReadAllText(Path.Combine(baseDir, "Resource", "Swagger", "old", input)),
                       File.ReadAllText(Path.Combine(baseDir, "Resource", "Swagger", "new", input))
                       ));
        }
コード例 #28
0
        public void TestClientModelFromSimpleSwagger()
        {
            Modeler modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input     = Path.Combine("Swagger", "swagger-simple-spec.json")
            });
            var clientModel = modeler.Build();

            var description = "The Products endpoint returns information about the Uber products offered at a given location. The response includes the display name and other details about each product, and lists the products in the proper display order.";
            var summary     = "Product Types";

            Assert.NotNull(clientModel);
            Assert.Equal(2, clientModel.Properties.Count);
            Assert.True(clientModel.Properties.Any(p => p.Name.Equals("subscriptionId", StringComparison.OrdinalIgnoreCase)));
            Assert.True(clientModel.Properties.Any(p => p.Name.Equals("apiVersion", StringComparison.OrdinalIgnoreCase)));
            Assert.Equal("2014-04-01-preview", clientModel.ApiVersion);
            Assert.Equal("https://management.azure.com/", clientModel.BaseUrl);
            Assert.Equal("Some cool documentation.", clientModel.Documentation);
            Assert.Equal(0, clientModel.Methods.Count(m => m.Group != null));
            Assert.Equal(2, clientModel.Methods.Count);
            Assert.Equal("list", clientModel.Methods[0].Name);
            Assert.NotEmpty(clientModel.Methods[0].Description);
            Assert.Equal(description, clientModel.Methods[0].Description);
            Assert.NotEmpty(clientModel.Methods[0].Summary);
            Assert.Equal(summary, clientModel.Methods[0].Summary);
            Assert.Equal(HttpMethod.Get, clientModel.Methods[0].HttpMethod);
            Assert.Equal(3, clientModel.Methods[0].Parameters.Count);
            Assert.Equal("subscriptionId", clientModel.Methods[0].Parameters[0].Name);
            Assert.NotNull(clientModel.Methods[0].Parameters[0].ClientProperty);
            Assert.Equal("resourceGroupName", clientModel.Methods[0].Parameters[1].Name);
            Assert.Equal("resourceGroupName", clientModel.Methods[0].Parameters[1].SerializedName);
            Assert.Equal("Resource Group ID.", clientModel.Methods[0].Parameters[1].Documentation);
            Assert.Equal(true, clientModel.Methods[0].Parameters[0].IsRequired);
            Assert.Equal(ParameterLocation.Path, clientModel.Methods[0].Parameters[0].Location);
            Assert.Equal("String", clientModel.Methods[0].Parameters[0].Type.ToString());
            Assert.Equal("reset", clientModel.Methods[1].Name);
            Assert.Equal("Product", clientModel.ModelTypes.First(m => m.Name == "Product").Name);
            Assert.Equal("Product", clientModel.ModelTypes.First(m => m.Name == "Product").SerializedName);
            Assert.Equal("The product title.", clientModel.ModelTypes.First(m => m.Name == "Product").Summary);
            Assert.Equal("The product documentation.", clientModel.ModelTypes.First(m => m.Name == "Product").Documentation);
            Assert.Equal("A product id.", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[0].Summary);
            Assert.Equal("product_id", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[0].Name);
            Assert.Equal("product_id", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[0].SerializedName);
            Assert.Null(clientModel.Methods[1].ReturnType.Body);
            Assert.Null(clientModel.Methods[1].Responses[HttpStatusCode.NoContent].Body);
            Assert.Equal(3, clientModel.Methods[1].Parameters.Count);
            Assert.Equal("subscriptionId", clientModel.Methods[1].Parameters[0].Name);
            Assert.Null(clientModel.Methods[1].Parameters[0].ClientProperty);
            Assert.Equal("resourceGroupName", clientModel.Methods[1].Parameters[1].Name);
            Assert.Equal("apiVersion", clientModel.Methods[1].Parameters[2].Name);

            Assert.Equal("capacity", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[3].Name);
            Assert.Equal("100", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[3].DefaultValue);
        }
コード例 #29
0
        public void TestClientModelFromSimpleSwagger()
        {
            Generator.Modeler modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = Path.Combine("Swagger", "swagger-simple-spec.json")
            });
            var clientModel = modeler.Build();

            var description = "The Products endpoint returns information about the Uber products offered at a given location. The response includes the display name and other details about each product, and lists the products in the proper display order.";
            var summary = "Product Types";

            Assert.NotNull(clientModel);
            Assert.Equal(2, clientModel.Properties.Count);
            Assert.True(clientModel.Properties.Any(p => p.Name.Equals("subscriptionId", StringComparison.OrdinalIgnoreCase)));
            Assert.True(clientModel.Properties.Any(p => p.Name.Equals("apiVersion", StringComparison.OrdinalIgnoreCase)));
            Assert.Equal("2014-04-01-preview", clientModel.ApiVersion);
            Assert.Equal("https://management.azure.com/", clientModel.BaseUrl);
            Assert.Equal("Some cool documentation.", clientModel.Documentation);
            Assert.Equal(0, clientModel.Methods.Count(m => m.Group != null));
            Assert.Equal(2, clientModel.Methods.Count);
            Assert.Equal("list", clientModel.Methods[0].Name);
            Assert.NotEmpty(clientModel.Methods[0].Description);
            Assert.Equal(description, clientModel.Methods[0].Description);
            Assert.NotEmpty(clientModel.Methods[0].Summary);
            Assert.Equal(summary, clientModel.Methods[0].Summary);
            Assert.Equal(HttpMethod.Get, clientModel.Methods[0].HttpMethod);
            Assert.Equal(3, clientModel.Methods[0].Parameters.Count);
            Assert.Equal("subscriptionId", clientModel.Methods[0].Parameters[0].Name);
            Assert.NotNull(clientModel.Methods[0].Parameters[0].ClientProperty);
            Assert.Equal("resourceGroupName", clientModel.Methods[0].Parameters[1].Name);
            Assert.Equal("resourceGroupName", clientModel.Methods[0].Parameters[1].SerializedName);
            Assert.Equal("Resource Group ID.", clientModel.Methods[0].Parameters[1].Documentation);
            Assert.Equal(true, clientModel.Methods[0].Parameters[0].IsRequired);
            Assert.Equal(ParameterLocation.Path, clientModel.Methods[0].Parameters[0].Location);
            Assert.Equal("String", clientModel.Methods[0].Parameters[0].Type.ToString());
            Assert.Equal("reset", clientModel.Methods[1].Name);
            Assert.Equal("Product", clientModel.ModelTypes.First(m=>m.Name == "Product").Name);
            Assert.Equal("Product", clientModel.ModelTypes.First(m => m.Name == "Product").SerializedName);
            Assert.Equal("The product title.", clientModel.ModelTypes.First(m => m.Name == "Product").Summary);
            Assert.Equal("The product documentation.", clientModel.ModelTypes.First(m => m.Name == "Product").Documentation);
            Assert.Equal("A product id.", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[0].Summary);
            Assert.Equal("product_id", clientModel.ModelTypes.First(m=>m.Name == "Product").Properties[0].Name);
            Assert.Equal("product_id", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[0].SerializedName);
            Assert.Null(clientModel.Methods[1].ReturnType.Body);
            Assert.Null(clientModel.Methods[1].Responses[HttpStatusCode.NoContent].Body);
            Assert.Equal(3, clientModel.Methods[1].Parameters.Count);
            Assert.Equal("subscriptionId", clientModel.Methods[1].Parameters[0].Name);
            Assert.Null(clientModel.Methods[1].Parameters[0].ClientProperty);
            Assert.Equal("resourceGroupName", clientModel.Methods[1].Parameters[1].Name);
            Assert.Equal("apiVersion", clientModel.Methods[1].Parameters[2].Name);

            Assert.Equal("capacity", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[3].Name);
            Assert.Equal("100", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[3].DefaultValue);
        }
コード例 #30
0
 public void ClientModelWithCircularDependencyThrowsError()
 {
     var modeler = new SwaggerModeler(new Settings
     {
         Namespace = "Test",
         Input = Path.Combine("Swagger", "swagger-allOf-circular.json")
     });
     var ex = Assert.Throws<InvalidOperationException>(() => modeler.Build());
     Assert.Contains("circular", ex.Message, StringComparison.OrdinalIgnoreCase);
     Assert.Contains("siamese", ex.Message, StringComparison.OrdinalIgnoreCase);
 }
コード例 #31
0
        public void GenerateCodeModel()
        {
            var input     = Path.Combine(CodeBaseDirectory, "..", "..", "..", "Resource", "Swagger", "deprecated.yaml");
            var modeler   = new SwaggerModeler();
            var codeModel = modeler.Build(SwaggerParser.Parse(File.ReadAllText(input)));

            var output = Path.Combine(CodeBaseDirectory, "..", "..", "..", "Expected", "deprecated", "deprecated.json");

            Directory.CreateDirectory(Path.GetDirectoryName(output));
            File.WriteAllText(output, JsonConvert.SerializeObject(codeModel, Formatting.Indented));
        }
コード例 #32
0
        public void TestYamlParsing()
        {
            Generator.Modeler modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input     = Path.Combine("Swagger", "swagger-simple-spec.yaml")
            });
            var clientModel = modeler.Build();

            Assert.NotNull(clientModel);
        }
コード例 #33
0
 public void ClientModelWithCircularDependencyThrowsError()
 {
     var modeler = new SwaggerModeler(new Settings
     {
         Namespace = "Test",
         Input = Path.Combine("Swagger", "swagger-allOf-circular.json")
     });
     var ex = Assert.Throws<InvalidOperationException>(() => modeler.Build());
     Assert.Contains("circular", ex.Message, StringComparison.OrdinalIgnoreCase);
     Assert.Contains("siamese", ex.Message, StringComparison.OrdinalIgnoreCase);
 }
コード例 #34
0
        public void GlobalResponsesReference()
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = Path.Combine("Swagger", "swagger-global-responses.json")
            });
            var clientModel = modeler.Build();

            Assert.Equal(1, clientModel.Methods[0].Responses.Count);
            Assert.NotNull(clientModel.Methods[0].Responses[HttpStatusCode.OK]);
        }
コード例 #35
0
ファイル: SwaggerObject.cs プロジェクト: Ranjana1996/autorest
 public ObjectBuilder GetBuilder(SwaggerModeler swaggerSpecBuilder)
 {
     if (this is SwaggerParameter)
     {
         return new ParameterBuilder(this as SwaggerParameter, swaggerSpecBuilder);
     }
     if (this is Schema)
     {
         return new SchemaBuilder(this as Schema, swaggerSpecBuilder);
     }
     return new ObjectBuilder(this, swaggerSpecBuilder);
 }
コード例 #36
0
        public void TestExternalReferences()
        {
            Generator.Modeler modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = Path.Combine("Swagger", "swagger-external-ref-no-definitions.json")
            });
            var clientModel = modeler.Build();

            Assert.NotNull(clientModel);
            Assert.Equal(2, clientModel.ModelTypes.Count);
        }
コード例 #37
0
        public void DefaultReturnsCorrectType()
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = Path.Combine("Swagger", "swagger-multiple-response-schemas.json")
            });
            var clientModel = modeler.Build();

            var retType = clientModel.Methods.First(m => m.Name == "patchDefaultResponse");

            Assert.Equal("pet", retType.ReturnType.ToString());
        }
コード例 #38
0
        public void TestExternalReferencesWithAllOf()
        {
            Generator.Modeler modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = @"Swagger\swagger-external-ref.json"
            });
            var clientModel = modeler.Build();

            Assert.NotNull(clientModel);
            Assert.Equal(3, clientModel.ModelTypes.Count);
            Assert.Equal("ChildProduct", clientModel.ModelTypes[0].Name);
            Assert.Equal("Product", clientModel.ModelTypes[0].BaseModelType.Name);
        }
コード例 #39
0
        private IEnumerable<ValidationMessage> ValidateSwagger(string input)
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = input
            });
            IEnumerable<ValidationMessage> messages;
            modeler.Build(out messages);

            // remove debug-level messages
            messages = messages.Where(each => each.Severity > LogEntrySeverity.Debug);

            return messages;
        }
コード例 #40
0
        public void AllowVendorExtensionInPath()
        {
            SwaggerModeler modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = Path.Combine("Swagger", "vendor-extension-in-path.json")
            });
            var clientModel = modeler.Build();

            // should return a valid model.
            Assert.NotNull(clientModel);

            // there should be one method in this generated api.
            Assert.Equal(1, modeler.ServiceClient.Methods.Count);
        }
コード例 #41
0
        public void TestClientModelFromSimpleSwagger()
        {
            Generator.Modeler modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = Path.Combine("Swagger", "swagger-simple-spec.json")
            });
            var clientModel = modeler.Build();

            Assert.NotNull(clientModel);
            Assert.Equal(2, clientModel.Properties.Count);
            Assert.True(clientModel.Properties.Any(p => p.Name.Equals("subscriptionId", StringComparison.OrdinalIgnoreCase)));
            Assert.True(clientModel.Properties.Any(p => p.Name.Equals("apiVersion", StringComparison.OrdinalIgnoreCase)));
            Assert.Equal("2014-04-01-preview", clientModel.ApiVersion);
            Assert.Equal("https://management.azure.com/", clientModel.BaseUrl);
            Assert.Equal("Some cool documentation.", clientModel.Documentation);
            Assert.Equal(0, clientModel.Methods.Count(m => m.Group != null));
            Assert.Equal(2, clientModel.Methods.Count);
            Assert.Equal("list", clientModel.Methods[0].Name);
            Assert.NotEmpty(clientModel.Methods[0].Documentation);
            Assert.Equal(HttpMethod.Get, clientModel.Methods[0].HttpMethod);
            Assert.Equal(3, clientModel.Methods[0].Parameters.Count);
            Assert.Equal("subscriptionId", clientModel.Methods[0].Parameters[0].Name);
            Assert.NotNull(clientModel.Methods[0].Parameters[0].ClientProperty);
            Assert.Equal("resourceGroupName", clientModel.Methods[0].Parameters[1].Name);
            Assert.Equal("resourceGroupName", clientModel.Methods[0].Parameters[1].SerializedName);
            Assert.Equal("Resource Group ID.", clientModel.Methods[0].Parameters[1].Documentation);
            Assert.Equal(true, clientModel.Methods[0].Parameters[0].IsRequired);
            Assert.Equal(ParameterLocation.Path, clientModel.Methods[0].Parameters[0].Location);
            Assert.Equal("String", clientModel.Methods[0].Parameters[0].Type.ToString());
            Assert.Equal("reset", clientModel.Methods[1].Name);
            Assert.Equal("Product", clientModel.ModelTypes[0].Name);
            Assert.Equal("Product", clientModel.ModelTypes[0].SerializedName);
            Assert.Equal("The product documentation.", clientModel.ModelTypes[0].Documentation);
            Assert.Equal("product_id", clientModel.ModelTypes[0].Properties[0].Name);
            Assert.Equal("product_id", clientModel.ModelTypes[0].Properties[0].SerializedName);
            Assert.Null(clientModel.Methods[1].ReturnType);
            Assert.Null(clientModel.Methods[1].Responses[HttpStatusCode.NoContent]);
            Assert.Equal(3, clientModel.Methods[1].Parameters.Count);
            Assert.Equal("subscriptionId", clientModel.Methods[1].Parameters[0].Name);
            Assert.Null(clientModel.Methods[1].Parameters[0].ClientProperty);
            Assert.Equal("resourceGroupName", clientModel.Methods[1].Parameters[1].Name);
            Assert.Equal("apiVersion", clientModel.Methods[1].Parameters[2].Name);
        }
コード例 #42
0
        /// <summary>
        /// Helper method -- load two Swagger documents and invoke the comparison logic.
        /// </summary>
        /// <param name="input">The name of the swagger document file. The file name must be the same in both the 'modified' and 'original' folder.</param>
        /// <returns>A list of messages from the comparison logic.</returns>
        private IEnumerable<ComparisonMessage> CompareSwagger(string input)
        {
            using (NewContext)
            {
                var settings = new Settings
                {
                    Namespace = "Test",
                    Input = Path.Combine("Swagger", "Comparison", "Modified", input),
                    Previous = Path.Combine("Swagger", "Comparison", "Original", input)
                };

                var modeler = new SwaggerModeler();
                IEnumerable<ComparisonMessage> messages = modeler.Compare();

                // remove debug-level messages
                messages = messages.Where(each => each.Severity > LogEntrySeverity.Debug);

                return messages;
            }
        }
コード例 #43
0
        public void TestConstants()
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = @"Swagger\swagger-validation.json"
            });
            var clientModel = modeler.Build();

            Assert.Equal("myintconst", clientModel.Methods[0].Parameters[4].Name);
            Assert.Equal(PrimaryType.Int, clientModel.Methods[0].Parameters[4].Type);
            Assert.Equal(true, clientModel.Methods[0].Parameters[4].IsConstant);
            Assert.Equal("0", clientModel.Methods[0].Parameters[4].DefaultValue);

            Assert.Equal("mystrconst", clientModel.Methods[0].Parameters[5].Name);
            Assert.Equal(PrimaryType.String, clientModel.Methods[0].Parameters[5].Type);
            Assert.Equal(true, clientModel.Methods[0].Parameters[5].IsConstant);
            Assert.Equal("constant", clientModel.Methods[0].Parameters[5].DefaultValue);

            Assert.Equal("myintconst", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[5].Name);
            Assert.Equal(PrimaryType.Int, clientModel.ModelTypes.First(m => m.Name == "Product").Properties[5].Type);
            Assert.Equal(true, clientModel.ModelTypes.First(m => m.Name == "Product").Properties[5].IsConstant);
            Assert.Equal("0", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[5].DefaultValue);

            Assert.Equal("mystrconst", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[6].Name);
            Assert.Equal(PrimaryType.String, clientModel.ModelTypes.First(m => m.Name == "Product").Properties[6].Type);
            Assert.Equal(true, clientModel.ModelTypes.First(m => m.Name == "Product").Properties[6].IsConstant);
            Assert.Equal("constant", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[6].DefaultValue);
        }
コード例 #44
0
        public void TestAdditionalProperties()
        {
            using (NewContext)
            {
                new Settings
                {
                    Namespace = "Test",
                    Input = Path.Combine("Swagger", "swagger-additional-properties.yaml")
                };

                Modeler modeler = new SwaggerModeler();
                var codeModel = modeler.Build();

                Assert.NotNull(codeModel);
                Assert.Equal(5, codeModel.ModelTypes.Count);

                // did we find the type?
                var wtd = codeModel.ModelTypes.FirstOrDefault(each => each.Name == "WithTypedDictionary");
                Assert.NotNull(wtd);

                // did we find the member called 'additionalProperties'
                var prop = wtd.Properties.FirstOrDefault(each => each.Name == "AdditionalProperties");
                Assert.NotNull(prop);

                // is it a DictionaryType?
                var dictionaryProperty = prop.ModelType as DictionaryType;
                Assert.NotNull(dictionaryProperty);

                // is a string,string dictionary?
                Assert.Equal("Dictionary<string,Feature>", dictionaryProperty.Name);
                Assert.Equal("Feature", dictionaryProperty.ValueType.Name);

                // is it marked as an 'additionalProperties' bucket?
                Assert.True(dictionaryProperty.SupportsAdditionalProperties);

                // did we find the type?
                var wud = codeModel.ModelTypes.FirstOrDefault(each => each.Name == "WithUntypedDictionary");
                Assert.NotNull(wud);

                // did we find the member called 'additionalProperties'
                prop = wud.Properties.FirstOrDefault(each => each.Name == "AdditionalProperties");
                Assert.NotNull(prop);

                // is it a DictionaryType?
                dictionaryProperty = prop.ModelType as DictionaryType;
                Assert.NotNull(dictionaryProperty);

                // is a string,string dictionary?
                Assert.Equal("Dictionary<string,Object>", dictionaryProperty.Name);
                Assert.Equal("Object", dictionaryProperty.ValueType.Name);

                // is it marked as an 'additionalProperties' bucket?
                Assert.True(dictionaryProperty.SupportsAdditionalProperties);

                var wsd = codeModel.ModelTypes.FirstOrDefault(each => each.Name == "WithStringDictionary");
                Assert.NotNull(wsd);

                // did we find the member called 'additionalProperties'
                prop = wsd.Properties.FirstOrDefault(each => each.Name == "AdditionalProperties");
                Assert.NotNull(prop);

                // is it a DictionaryType?
                dictionaryProperty = prop.ModelType as DictionaryType;
                Assert.NotNull(dictionaryProperty);

                // is a string,string dictionary?
                Assert.Equal("Dictionary<string,String>", dictionaryProperty.Name);
                Assert.Equal("String", dictionaryProperty.ValueType.Name);

                // is it marked as an 'additionalProperties' bucket?
                Assert.True(dictionaryProperty.SupportsAdditionalProperties);
            }
        }
コード例 #45
0
        public void TestYamlParsing()
        {
            using (NewContext)
            {
                var settings = new Settings
                {
                    Namespace = "Test",
                    Input = Path.Combine("Swagger", "swagger-simple-spec.yaml")
                };

                Modeler modeler = new SwaggerModeler();
                var codeModel = modeler.Build();

                Assert.NotNull(codeModel);
            }
        }
コード例 #46
0
        public void TestClientModelPolymorhism()
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = Path.Combine("Swagger", "swagger-polymorphism.json")
            });
            var clientModel = modeler.Build();

            Assert.NotNull(clientModel);
            Assert.Equal("Pet", clientModel.ModelTypes.First(m => m.Name == "Pet").Name);
            Assert.Equal("dtype", clientModel.ModelTypes.First(m => m.Name == "Pet").PolymorphicDiscriminator);
            Assert.Equal(2, clientModel.ModelTypes.First(m => m.Name == "Pet").Properties.Count);
            Assert.Equal("id", clientModel.ModelTypes.First(m => m.Name == "Pet").Properties[0].Name);
            Assert.Equal("description", clientModel.ModelTypes.First(m => m.Name == "Pet").Properties[1].Name);
            Assert.Equal("Cat", clientModel.ModelTypes.First(m => m.Name == "Cat").Name);
            Assert.Equal("Pet", clientModel.ModelTypes.First(m => m.Name == "Cat").BaseModelType.Name);
            Assert.Equal(1, clientModel.ModelTypes.First(m => m.Name == "Cat").Properties.Count);
            Assert.Equal("Lizard", clientModel.ModelTypes.First(m => m.Name == "Lizard").Name);
            Assert.Equal("lzd", clientModel.ModelTypes.First(m => m.Name == "Lizard").SerializedName);
        }
コード例 #47
0
        public void TestExternalReferences()
        {
            using (NewContext)
            {
                new Settings
                {
                    Namespace = "Test",
                    Input = Path.Combine("Swagger", "swagger-external-ref-no-definitions.json")
                };
                Modeler modeler = new SwaggerModeler();
                var codeModel = modeler.Build();

                Assert.NotNull(codeModel);
                Assert.Equal(2, codeModel.ModelTypes.Count);
            }
        }
コード例 #48
0
        public void TestClientModelWithRecursiveTypes()
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = Path.Combine("Swagger", "swagger-recursive-type.json")
            });
            var clientModel = modeler.Build();

            Assert.NotNull(clientModel);
            Assert.Equal("Product", clientModel.ModelTypes.First(m => m.Name == "Product").Name);
            Assert.Equal("product_id", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[0].Name);
            Assert.Equal("String", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[0].Type.ToString());
        }
コード例 #49
0
        public void TestExternalReferencesWithAllOf()
        {
            Generator.Modeler modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = Path.Combine("Swagger", "swagger-external-ref.json")
            });
            var clientModel = modeler.Build();

            Assert.NotNull(clientModel);
            Assert.Equal(3, clientModel.ModelTypes.Count);
            Assert.Equal("ChildProduct", clientModel.ModelTypes.First(m => m.Name == "ChildProduct").Name);
            Assert.Equal("Product", clientModel.ModelTypes.First(m => m.Name == "ChildProduct").BaseModelType.Name);
        }
コード例 #50
0
        public void TestClientModelWithNoContent()
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = @"Swagger\swagger-no-content.json"
            });
            var clientModel = modeler.Build();

            Assert.Equal("DeleteBlob", clientModel.Methods[4].Name);
            Assert.Equal(PrimaryType.Object, clientModel.Methods[4].ReturnType.Body);
            Assert.Equal(PrimaryType.Object, clientModel.Methods[4].Responses[HttpStatusCode.OK].Body);
            Assert.Null(clientModel.Methods[4].Responses[HttpStatusCode.BadRequest].Body);
        }
コード例 #51
0
        public void TestClientModelWithInheritance()
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = Path.Combine("Swagger", "swagger-allOf.json")
            });
            var clientModel = modeler.Build();

            Assert.NotNull(clientModel);
            Assert.Equal("pet", clientModel.ModelTypes.First(m => m.Name == "pet").Name);
            Assert.Equal("cat", clientModel.ModelTypes.First(m => m.Name == "cat").Name);
            Assert.Equal("pet", clientModel.ModelTypes.First(m => m.Name == "cat").BaseModelType.Name);
            Assert.Equal("breed", clientModel.ModelTypes.First(m => m.Name == "cat").Properties[0].Name);
            Assert.Equal(true, clientModel.ModelTypes.First(m => m.Name == "cat").Properties[0].IsRequired);
            Assert.Equal("color", clientModel.ModelTypes.First(m => m.Name == "cat").Properties[1].Name);
            Assert.Equal("siamese", clientModel.ModelTypes.First(m => m.Name == "siamese").Name);
            Assert.Equal("cat", clientModel.ModelTypes.First(m => m.Name == "siamese").BaseModelType.Name);
        }
コード例 #52
0
        public void TestClientModelWithDifferentReturnsTypesBasedOnStatusCode()
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = Path.Combine("Swagger", "swagger-multiple-response-schemas.json")
            });
            var clientModel = modeler.Build();

            Assert.NotNull(clientModel);
            Assert.Equal("getSameResponse", clientModel.Methods[0].Name);
            Assert.Equal("IList<pet>", clientModel.Methods[0].ReturnType.ToString());
            Assert.Equal("IList<pet>", clientModel.Methods[0].Responses[HttpStatusCode.OK].ToString());
            Assert.Equal("IList<pet>", clientModel.Methods[0].Responses[HttpStatusCode.Accepted].ToString());

            Assert.Equal("postInheretedTypes", clientModel.Methods[1].Name);
            Assert.Equal("pet", clientModel.Methods[1].ReturnType.ToString());
            Assert.Equal("dog", clientModel.Methods[1].Responses[HttpStatusCode.OK].ToString());
            Assert.Equal("cat", clientModel.Methods[1].Responses[HttpStatusCode.Accepted].ToString());

            Assert.Equal("patchDifferentStreamTypesNoContent", clientModel.Methods[6].Name);
            Assert.Equal("VirtualMachineGetRemoteDesktopFileResponse", clientModel.Methods[6].ReturnType.ToString());
            Assert.Equal("VirtualMachineGetRemoteDesktopFileResponse",
                clientModel.Methods[6].Responses[HttpStatusCode.OK].ToString());
            Assert.Null(clientModel.Methods[6].Responses[HttpStatusCode.NoContent].Body);
        }
コード例 #53
0
        public void TestClientWithValidation()
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = @"Swagger\swagger-validation.json"
            });
            var clientModel = modeler.Build();

            Assert.Equal("resourceGroupName", clientModel.Methods[0].Parameters[1].Name);
            Assert.Equal(true, clientModel.Methods[0].Parameters[1].IsRequired);
            Assert.Equal(3, clientModel.Methods[0].Parameters[1].Constraints.Count);
            Assert.Equal("10", clientModel.Methods[0].Parameters[1].Constraints[Constraint.MaxLength]);
            Assert.Equal("3", clientModel.Methods[0].Parameters[1].Constraints[Constraint.MinLength]);
            Assert.Equal("[a-zA-Z0-9]+", clientModel.Methods[0].Parameters[1].Constraints[Constraint.Pattern]);

            Assert.Equal("id", clientModel.Methods[0].Parameters[2].Name);
            Assert.Equal(3, clientModel.Methods[0].Parameters[2].Constraints.Count);
            Assert.Equal("10", clientModel.Methods[0].Parameters[2].Constraints[Constraint.MultipleOf]);
            Assert.Equal("100", clientModel.Methods[0].Parameters[2].Constraints[Constraint.InclusiveMinimum]);
            Assert.Equal("1000", clientModel.Methods[0].Parameters[2].Constraints[Constraint.InclusiveMaximum]);

            Assert.Equal("apiVersion", clientModel.Methods[0].Parameters[3].Name);
            Assert.NotNull(clientModel.Methods[0].Parameters[3].ClientProperty);
            Assert.Equal(1, clientModel.Methods[0].Parameters[3].Constraints.Count);
            Assert.Equal("\\d{2}-\\d{2}-\\d{4}", clientModel.Methods[0].Parameters[3].Constraints[Constraint.Pattern]);

            Assert.Equal("Product", clientModel.ModelTypes.First(m => m.Name == "Product").Name);
            Assert.Equal("display_names", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[2].Name);
            Assert.Equal(3, clientModel.ModelTypes.First(m => m.Name == "Product").Properties[2].Constraints.Count);
            Assert.Equal("6", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[2].Constraints[Constraint.MaxItems]);
            Assert.Equal("0", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[2].Constraints[Constraint.MinItems]);
            Assert.Equal("true", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[2].Constraints[Constraint.UniqueItems]);

            Assert.Equal("capacity", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[3].Name);
            Assert.Equal(2, clientModel.ModelTypes.First(m => m.Name == "Product").Properties[3].Constraints.Count);
            Assert.Equal("100", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[3].Constraints[Constraint.ExclusiveMaximum]);
            Assert.Equal("0", clientModel.ModelTypes.First(m => m.Name == "Product").Properties[3].Constraints[Constraint.ExclusiveMinimum]);
	    }
コード例 #54
0
        public void TestDataTypes()
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = Path.Combine("Swagger", "swagger-data-types.json")
            });
            var clientModel = modeler.Build();

            Assert.NotNull(clientModel);
            Assert.Equal("Int integer", clientModel.Methods[0].Parameters[0].ToString());
            Assert.Equal("Int int", clientModel.Methods[0].Parameters[1].ToString());
            Assert.Equal("Long long", clientModel.Methods[0].Parameters[2].ToString());
            Assert.Equal("Double number", clientModel.Methods[0].Parameters[3].ToString());
            Assert.Equal("Double float", clientModel.Methods[0].Parameters[4].ToString());
            Assert.Equal("Double double", clientModel.Methods[0].Parameters[5].ToString());
            Assert.Equal("Decimal decimal", clientModel.Methods[0].Parameters[6].ToString());
            Assert.Equal("String string", clientModel.Methods[0].Parameters[7].ToString());
            Assert.Equal("enum color", clientModel.Methods[0].Parameters[8].ToString());
            Assert.Equal("ByteArray byte", clientModel.Methods[0].Parameters[9].ToString());
            Assert.Equal("Boolean boolean", clientModel.Methods[0].Parameters[10].ToString());
            Assert.Equal("Date date", clientModel.Methods[0].Parameters[11].ToString());
            Assert.Equal("DateTime dateTime", clientModel.Methods[0].Parameters[12].ToString());
            Assert.Equal("IList<String> array", clientModel.Methods[0].Parameters[13].ToString());

            var variableEnumInPath =
                clientModel.Methods.First(m => m.Name == "list" && m.Group == null).Parameters.First(p => p.Name == "color" && p.Location == ParameterLocation.Path).Type as EnumType;
            Assert.NotNull(variableEnumInPath);
            Assert.Equal(variableEnumInPath.Values,
                new[] { new EnumValue { Name = "red" }, new EnumValue { Name = "blue" }, new EnumValue { Name = "green" } }.ToList());
            Assert.True(variableEnumInPath.ModelAsString);
            Assert.Empty(variableEnumInPath.Name);

            var variableEnumInQuery =
                clientModel.Methods.First(m => m.Name == "list" && m.Group == null).Parameters.First(p => p.Name == "color" && p.Location == ParameterLocation.Query).Type as EnumType;
            Assert.NotNull(variableEnumInQuery);
            Assert.Equal(variableEnumInQuery.Values,
                new[] { new EnumValue { Name = "red" }, new EnumValue { Name = "blue" }, new EnumValue { Name = "green" }, new EnumValue { Name = "purple" } }.ToList());
            Assert.True(variableEnumInQuery.ModelAsString);
            Assert.Empty(variableEnumInQuery.Name);

            var differentEnum =
                clientModel.Methods.First(m => m.Name == "list" && m.Group == "DiffEnums").Parameters.First(p => p.Name == "color" && p.Location == ParameterLocation.Query).Type as EnumType;
            Assert.NotNull(differentEnum);
            Assert.Equal(differentEnum.Values,
                new[] { new EnumValue { Name = "cyan" }, new EnumValue { Name = "yellow" } }.ToList());
            Assert.True(differentEnum.ModelAsString);
            Assert.Empty(differentEnum.Name);

            var sameEnum =
                clientModel.Methods.First(m => m.Name == "get" && m.Group == "SameEnums").Parameters.First(p => p.Name == "color2" && p.Location == ParameterLocation.Query).Type as EnumType;
            Assert.NotNull(sameEnum);
            Assert.Equal(sameEnum.Values,
                new[] { new EnumValue { Name = "blue" }, new EnumValue { Name = "green" }, new EnumValue { Name = "red" } }.ToList());
            Assert.True(sameEnum.ModelAsString);
            Assert.Empty(sameEnum.Name);

            var modelEnum =
                clientModel.ModelTypes.First(m => m.Name == "Product").Properties.First(p => p.Name == "color2").Type as EnumType;
            Assert.NotNull(modelEnum);
            Assert.Equal(modelEnum.Values,
                new[] { new EnumValue { Name = "red" }, new EnumValue { Name = "blue" }, new EnumValue { Name = "green" } }.ToList());
            Assert.True(modelEnum.ModelAsString);
            Assert.Empty(modelEnum.Name);

            var fixedEnum =
                clientModel.ModelTypes.First(m => m.Name == "Product").Properties.First(p => p.Name == "color").Type as EnumType;
            Assert.NotNull(fixedEnum);
            Assert.Equal(fixedEnum.Values,
                new[] { new EnumValue { Name = "red" }, new EnumValue { Name = "blue" }, new EnumValue { Name = "green" } }.ToList());
            Assert.False(fixedEnum.ModelAsString);
            Assert.Equal("Colors", fixedEnum.Name);

            var fixedEnum2 =
                clientModel.ModelTypes.First(m => m.Name == "Product").Properties.First(p => p.Name == "color3").Type as EnumType;
            Assert.Equal(fixedEnum2, fixedEnum);

            Assert.Equal(1, clientModel.EnumTypes.Count);
            Assert.Equal("Colors", clientModel.EnumTypes.First().Name);
        }
コード例 #55
0
        public void TestClientModelWithMethodGroups()
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = @"Swagger\swagger-optional-params.json"
            });
            var clientModel = modeler.Build();

            Assert.NotNull(clientModel);
            Assert.Equal(0, clientModel.Methods.Count(m => m.Group == null));
            Assert.Equal(2, clientModel.Methods.Count(m => m.Group == "Widgets"));
            Assert.Equal("List", clientModel.Methods[0].Name);
        }
コード例 #56
0
        public void TestClientModelWithStreamAndByteArray()
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = Path.Combine("Swagger", "swagger-streaming.json")
            });
            var clientModel = modeler.Build();

            Assert.NotNull(clientModel);
            Assert.Equal("GetWithStreamFormData", clientModel.Methods[0].Name);
            Assert.Equal("Stream", clientModel.Methods[0].Parameters[0].Type.Name);
            Assert.Equal("Stream", clientModel.Methods[0].ReturnType.ToString());
            Assert.Equal("Stream", clientModel.Methods[0].Responses[HttpStatusCode.OK].ToString());

            Assert.Equal("PostWithByteArrayFormData", clientModel.Methods[1].Name);
            Assert.Equal("ByteArray", clientModel.Methods[1].Parameters[0].Type.Name);
            Assert.Equal("ByteArray", clientModel.Methods[1].ReturnType.ToString());
            Assert.Equal("ByteArray", clientModel.Methods[1].Responses[HttpStatusCode.OK].ToString());

            Assert.Equal("GetWithStream", clientModel.Methods[2].Name);
            Assert.Equal("Stream", clientModel.Methods[2].ReturnType.ToString());
            Assert.Equal("Stream", clientModel.Methods[2].Responses[HttpStatusCode.OK].ToString());

            Assert.Equal("PostWithByteArray", clientModel.Methods[3].Name);
            Assert.Equal("ByteArray", clientModel.Methods[3].ReturnType.ToString());
            Assert.Equal("ByteArray", clientModel.Methods[3].Responses[HttpStatusCode.OK].ToString());
        }
コード例 #57
0
        public void TestClientModelWithResponseHeaders()
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = Path.Combine("Swagger", "swagger-response-headers.json")
            });
            var clientModel = modeler.Build();

            Assert.NotNull(clientModel);
            Assert.Equal(2, clientModel.Methods.Count);
            Assert.Equal(2, clientModel.Methods[0].Responses.Count);
            Assert.Equal("list-Headers", clientModel.Methods[0].Responses[HttpStatusCode.OK].Headers.Name);
            Assert.Equal(3, ((CompositeType)clientModel.Methods[0].Responses[HttpStatusCode.OK].Headers).Properties.Count);
            Assert.Equal("list-Headers", clientModel.Methods[0].Responses[HttpStatusCode.Created].Headers.Name);
            Assert.Equal(3, ((CompositeType)clientModel.Methods[0].Responses[HttpStatusCode.Created].Headers).Properties.Count);
            Assert.Equal("list-Headers", clientModel.Methods[0].ReturnType.Headers.Name);
            Assert.Equal(3, ((CompositeType)clientModel.Methods[0].ReturnType.Headers).Properties.Count);

            Assert.Equal(1, clientModel.Methods[1].Responses.Count);
            Assert.Equal("create-Headers", clientModel.Methods[1].Responses[HttpStatusCode.OK].Headers.Name);
            Assert.Equal(3, ((CompositeType)clientModel.Methods[1].Responses[HttpStatusCode.OK].Headers).Properties.Count);
            Assert.Equal("create-Headers", clientModel.Methods[1].ReturnType.Headers.Name);
            Assert.Equal(3, ((CompositeType)clientModel.Methods[1].ReturnType.Headers).Properties.Count);
            Assert.True(clientModel.HeaderTypes.Any(c => c.Name == "list-Headers"));
            Assert.True(clientModel.HeaderTypes.Any(c => c.Name == "create-Headers"));
        }
コード例 #58
0
        public void TestCustomPaths()
        {
            var modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = Path.Combine("Swagger", "swagger-x-ms-paths.json")
            });
            var clientModel = modeler.Build();

            Assert.NotNull(clientModel);
            Assert.Equal(3, clientModel.Methods.Count);
            Assert.True(clientModel.Methods.All(m => m.Url == "/values/foo"));

        }
コード例 #59
0
        public void TestCompositeConstants()
        {
            using (NewContext)
            {
                new Settings
                {
                    Namespace = "Test",
                    Input = Path.Combine("Swagger", "swagger-composite-constants.json")
                };
                var modeler = new SwaggerModeler();

                var codeModel = modeler.Build();
                Assert.Equal(false, codeModel.ModelTypes.First(m => m.Name == "NetworkInterfaceIPConfigurationPropertiesFormat").ContainsConstantProperties);
                Assert.Equal(false, codeModel.ModelTypes.First(m => m.Name == "IPConfigurationPropertiesFormat").ContainsConstantProperties);
            }
        }
コード例 #60
0
        public void TestExternalReferencesWithExtension()
        {
            Generator.Modeler modeler = new SwaggerModeler(new Settings
            {
                Namespace = "Test",
                Input = Path.Combine("Swagger", "swagger-external-ref-no-definitions.json")
            });
            var clientModel = modeler.Build();

            Assert.NotNull(clientModel);
            Assert.True(clientModel.ModelTypes.First().Extensions.ContainsKey("x-ms-external"));
        }