Ejemplo n.º 1
0
        public HttpResponseMessage Swagger()
        {
            var generator = new WebApiToSwaggerGenerator(Configuration.Routes.First(r => !string.IsNullOrEmpty(r.RouteTemplate)).RouteTemplate);
            var service   = generator.GenerateForController(GetType(), "Swagger");

            return(new HttpResponseMessage {
                Content = new StringContent(service.ToJson(), Encoding.UTF8)
            });
        }
            internal string GenerateForSingleController(string controllerClassName, string settingsData)
            {
                var settings = JsonConvert.DeserializeObject<WebApiAssemblyToSwaggerGeneratorSettings>(settingsData);

                var assembly = Assembly.LoadFrom(settings.AssemblyPath);
                var type = assembly.GetType(controllerClassName);

                var generator = new WebApiToSwaggerGenerator(settings);
                return generator.GenerateForController(type).ToJson();
            }
Ejemplo n.º 3
0
        public void When_operation_has_tags_attributes_then_they_are_processed()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiAssemblyToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController <TagsTest2Controller>();

            //// Assert
            Assert.AreEqual("[\"foo\",\"bar\"]", JsonConvert.SerializeObject(document.Operations.First().Operation.Tags));
        }
Ejemplo n.º 4
0
        public void When_route_is_on_inherited_parent_class_then_it_is_used_for_swagger_generation()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController <MyController2>();

            //// Assert
            Assert.AreEqual("/Foo", document.Operations.First().Path);
        }
Ejemplo n.º 5
0
        public void When_operation_has_tags_attributes_then_they_are_processed()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiAssemblyToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController<TagsTest2Controller>();

            //// Assert
            Assert.AreEqual("[\"foo\",\"bar\"]", JsonConvert.SerializeObject(document.Operations.First().Operation.Tags));
        }
Ejemplo n.º 6
0
        public void When_route_is_on_inherited_parent_class_then_it_is_used_for_swagger_generation()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController<MyController2>();

            //// Assert
            Assert.AreEqual("/Foo", document.Operations.First().Path);
        }
Ejemplo n.º 7
0
        public void When_accept_verbs_attribute_with_delete_is_used_then_http_method_is_correct()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController<TestController>();
            var operation = document.Operations.First(o => o.Operation.OperationId == "Test_AddDelete");

            //// Assert
            Assert.AreEqual(SwaggerOperationMethod.Delete, operation.Method);
        }
Ejemplo n.º 8
0
        public void When_enum_is_used_as_array_item_then_it_is_generated_only_once()
        {
            // Arrange
            var apiGenerator = new WebApiToSwaggerGenerator(new WebApiAssemblyToSwaggerGeneratorSettings());

            //// Act
            var document = apiGenerator.GenerateForController<MyController>();
            var json = document.ToJson();

            // Assert
            Assert.IsTrue(json.Split(new[] { "x-enumNames" }, StringSplitOptions.None).Length == 2); // enum is defined only once
        }
Ejemplo n.º 9
0
        public void When_generating_type_with_deep_inheritance_then_allOf_has_one_item()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var service = generator.GenerateForController<TestController>();
            var swaggerSpecification = service.ToJson();

            //// Assert
            Assert.AreEqual(2, Regex.Matches(Regex.Escape(swaggerSpecification), "allOf").Count); // must have an allOf in BB and CC, no more (rest are refs)
        }
Ejemplo n.º 10
0
        public void When_generating_type_with_deep_inheritance_then_allOf_has_one_item()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var document             = generator.GenerateForController <TestController>();
            var swaggerSpecification = document.ToJson();

            //// Assert
            Assert.AreEqual(2, Regex.Matches(Regex.Escape(swaggerSpecification), "allOf").Count); // must have an allOf in BB and CC, no more (rest are refs)
        }
Ejemplo n.º 11
0
        public void When_method_has_overload_then_operation_ids_are_still_unique()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiAssemblyToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController<OperationIdController>();

            //// Assert
            var allIds = document.Operations.Select(o => o.Operation.OperationId).ToArray();
            Assert.AreEqual(4, allIds.Distinct().Count());
        }
Ejemplo n.º 12
0
        public void When_accept_verbs_attribute_with_put_is_used_then_http_method_is_correct()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var document  = generator.GenerateForController <TestController>();
            var operation = document.Operations.First(o => o.Operation.OperationId == "Test_AddPut");

            //// Assert
            Assert.AreEqual(SwaggerOperationMethod.Put, operation.Method);
        }
Ejemplo n.º 13
0
        public void When_generating_swagger_from_controller_than_all_required_operations_are_available()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var service = generator.GenerateForController <PersonsController>();
            var swaggerSpecification = service.ToJson();

            //// Assert
            Assert.AreEqual(10, service.Operations.Count());
        }
        public void When_generating_swagger_from_controller_than_all_required_operations_are_available()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator("api/{controller}/{action}/{id}");

            //// Act
            var service = generator.GenerateForController<PersonsController>();
            var swaggerSpecification = service.ToJson();

            //// Assert
            Assert.AreEqual(9, service.Operations.Count());
        }
Ejemplo n.º 15
0
        public void When_SwaggerOperation_attribute_is_available_then_operation_id_is_correct()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiAssemblyToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController<OperationIdController>();

            //// Assert
            Assert.AreEqual("MyFoo", document.Operations.First(o => o.Path == "/Foo").Operation.OperationId);
            Assert.AreEqual("OperationId_Bar", document.Operations.First(o => o.Path == "/Bar").Operation.OperationId);
        }
Ejemplo n.º 16
0
        public HttpResponseMessage Swagger()
        {
            var generator = new WebApiToSwaggerGenerator(new WebApiAssemblyToSwaggerGeneratorSettings
            {
                DefaultUrlTemplate = Configuration.Routes.First(r => !string.IsNullOrEmpty(r.RouteTemplate)).RouteTemplate
            });
            var document = generator.GenerateForController(GetType());

            return(new HttpResponseMessage {
                Content = new StringContent(document.ToJson(), Encoding.UTF8)
            });
        }
Ejemplo n.º 17
0
        public void When_route_contains_complex_path_parameter_then_it_is_correctly_parsed()
        {
            //// Arrange
            var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var swagger = swaggerGen.GenerateForController <PersonsController>();
            var json    = swagger.ToJson();

            //// Assert
            Assert.IsTrue(swagger.Paths.Contains("/api/Persons/RegexPathParameter/{deviceType}/{deviceId}/energyConsumed"));
        }
Ejemplo n.º 18
0
        // todo: Fix this
        //[TestMethod]
        public void When_route_attribute_is_on_controller_class_then_it_applies_for_actions()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var service   = generator.GenerateForController <MyController>();
            var operation = service.Operations.Single(o => o.Operation.OperationId == "Foo");

            //// Assert
            Assert.AreEqual("myRoot/My/Foo", operation.Path);
        }
Ejemplo n.º 19
0
        public void When_enum_is_used_as_array_item_then_it_is_generated_only_once()
        {
            // Arrange
            var apiGenerator = new WebApiToSwaggerGenerator(new WebApiAssemblyToSwaggerGeneratorSettings());

            //// Act
            var document = apiGenerator.GenerateForController <MyController>();
            var json     = document.ToJson();

            // Assert
            Assert.IsTrue(json.Split(new[] { "x-enumNames" }, StringSplitOptions.None).Length == 2); // enum is defined only once
        }
Ejemplo n.º 20
0
        public void When_SwaggerOperation_attribute_is_available_then_operation_id_is_correct()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiAssemblyToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController <OperationIdController>();

            //// Assert
            Assert.AreEqual("MyFoo", document.Operations.First(o => o.Path == "/Foo").Operation.OperationId);
            Assert.AreEqual("OperationId_Bar", document.Operations.First(o => o.Path == "/Bar").Operation.OperationId);
        }
Ejemplo n.º 21
0
        public void When_route_contains_complex_path_parameter_then_it_is_correctly_parsed()
        {
            //// Arrange
            var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var swagger = swaggerGen.GenerateForController<PersonsController>();
            var json = swagger.ToJson(); 

            //// Assert
            Assert.IsTrue(swagger.Paths.Contains("/api/Persons/RegexPathParameter/{deviceType}/{deviceId}/energyConsumed"));
        }
        // todo: Fix this
        //[TestMethod]
        public void When_route_attribte_is_on_controller_class_then_it_applies_for_actions()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var service = generator.GenerateForController<MyController>();
            var operation = service.Operations.Single(o => o.Operation.OperationId == "Foo");

            //// Assert
            Assert.AreEqual("myRoot/My/Foo", operation.Path);
        }
Ejemplo n.º 23
0
        public void When_operation_has_SwaggerResponseAttribute_then_it_is_processed()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiAssemblyToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController<ResponseAttributeTestController>();

            //// Assert
            var barOperation = document.Operations.Single(o => o.Operation.OperationId == "ResponseAttributeTest_Bar");
            Assert.AreEqual("201", barOperation.Operation.Responses.First().Key);
            Assert.AreEqual(JsonObjectType.Integer, barOperation.Operation.Responses.First().Value.Schema.Type);
        }
Ejemplo n.º 24
0
        public void When_web_api_path_has_constraints_then_they_are_removed_in_the_swagger_spec()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController(typeof(ConstrainedRoutePathController));

            //// Assert
            var path = document.Paths.First().Key;

            Assert.AreEqual("/{id}", path);
        }
Ejemplo n.º 25
0
        public void When_method_has_overload_then_operation_ids_are_still_unique()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiAssemblyToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController <OperationIdController>();

            //// Assert
            var allIds = document.Operations.Select(o => o.Operation.OperationId).ToArray();

            Assert.AreEqual(4, allIds.Distinct().Count());
        }
Ejemplo n.º 26
0
        public void When_there_is_a_ResultType_attribute_on_an_action_method_then_the_response_is_taken_from_the_given_type()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var service   = generator.GenerateForController <PersonsController>();
            var operation = service.Operations.Single(o => o.Path == "api/Persons/Get/{id}");

            //// Assert
            Assert.AreEqual(2, operation.Operation.Responses.Count);
            Assert.AreEqual("Person", operation.Operation.Responses["200"].ActualResponseSchema.TypeName);
        }
        public void When_there_is_a_ResultType_attribute_on_an_action_method_then_the_response_is_taken_from_the_given_type()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator("api/{controller}/{action}/{id}");

            //// Act
            var service = generator.GenerateForController<PersonsController>();
            var operation = service.Operations.Single(o => o.Path == "api/Persons/Get/{id}");

            //// Assert
            Assert.AreEqual(2, operation.Operation.Responses.Count);
            Assert.AreEqual("Person", operation.Operation.Responses["200"].Schema.ActualSchema.TypeName);
        }
Ejemplo n.º 28
0
        public void When_parameter_name_is_reserved_keyword_then_it_is_appended_with_at()
        {
            //// Arrange
            var swaggerGenerator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
            var document         = swaggerGenerator.GenerateForController <FooController>();

            var generator = new SwaggerToCSharpClientGenerator(document, new SwaggerToCSharpClientGeneratorSettings());

            //// Act
            var code = generator.GenerateFile();

            //// Assert
            Assert.IsTrue(code.Contains("Task<object> GetPersonAsync(bool? @override, "));
        }
Ejemplo n.º 29
0
        public void When_operation_has_SwaggerResponseAttribute_then_it_is_processed()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiAssemblyToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController <ResponseAttributeTestController>();

            //// Assert
            var barOperation = document.Operations.Single(o => o.Operation.OperationId == "ResponseAttributeTest_Bar");

            Assert.AreEqual("201", barOperation.Operation.Responses.First().Key);
            Assert.AreEqual(JsonObjectType.Integer, barOperation.Operation.Responses.First().Value.Schema.Type);
        }
Ejemplo n.º 30
0
        public void When_controller_has_RoutePrefix_then_paths_are_correct()
        {
            //// Arrange
            var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var swagger = swaggerGen.GenerateForController<PersonsController>();

            //// Assert
            Assert.IsNotNull(swagger.Paths["/api/Persons"][SwaggerOperationMethod.Get]);
            Assert.IsNotNull(swagger.Paths["/api/Persons/{id}"][SwaggerOperationMethod.Get]);
            Assert.IsNotNull(swagger.Paths["/api/Persons"][SwaggerOperationMethod.Post]);
            Assert.IsNotNull(swagger.Paths["/api/Persons/{id}"][SwaggerOperationMethod.Put]);
            Assert.IsNotNull(swagger.Paths["/api/Persons/{id}"][SwaggerOperationMethod.Delete]);
        }
Ejemplo n.º 31
0
        public void When_action_has_file_parameter_then_Stream_is_generated_in_CSharp_code()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
            var document = generator.GenerateForController<FileUploadController>();

            //// Act
            var codeGen = new SwaggerToCSharpClientGenerator(document, new SwaggerToCSharpClientGeneratorSettings());
            var code = codeGen.GenerateFile();

            //// Assert
            Assert.IsTrue(code.Contains("FileParameter file"));
            Assert.IsTrue(code.Contains("var content_ = new System.Net.Http.MultipartFormDataContent();"));
            Assert.IsTrue(code.Contains("content_.Add(new System.Net.Http.StreamContent(file.Data), \"file\""));
        }
Ejemplo n.º 32
0
        public void When_generating_swagger_from_controller_than_all_required_operations_are_available()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings
            {
                DefaultUrlTemplate = "api/{controller}/{action}/{id}"
            });

            //// Act
            var document = generator.GenerateForController<PersonsController>();
            var swaggerSpecification = document.ToJson();

            //// Assert
            Assert.AreEqual(10, document.Operations.Count());
        }
Ejemplo n.º 33
0
        public void When_class_has_RouteAttribute_with_placeholders_then_they_are_correctly_replaced()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController(typeof(AccountController));

            //// Assert
            Assert.IsTrue(document.Paths.ContainsKey("/account/Get"));
            Assert.IsTrue(document.Paths.ContainsKey("/account/GetAll"));
            Assert.IsTrue(document.Paths.ContainsKey("/account/Post"));
            Assert.IsTrue(document.Paths.ContainsKey("/account/Verify"));
            Assert.IsTrue(document.Paths.ContainsKey("/account/Confirm"));
        }
        public void When_generating_swagger_from_controller_than_all_required_operations_are_available()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings
            {
                DefaultUrlTemplate = "api/{controller}/{action}/{id}"
            });

            //// Act
            var document             = generator.GenerateForController <PersonsController>();
            var swaggerSpecification = document.ToJson();

            //// Assert
            Assert.AreEqual(10, document.Operations.Count());
        }
Ejemplo n.º 35
0
        public void When_controller_has_RoutePrefix_then_paths_are_correct()
        {
            //// Arrange
            var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var swagger = swaggerGen.GenerateForController <PersonsController>();

            //// Assert
            Assert.IsNotNull(swagger.Paths["/api/Persons"][SwaggerOperationMethod.Get]);
            Assert.IsNotNull(swagger.Paths["/api/Persons/{id}"][SwaggerOperationMethod.Get]);
            Assert.IsNotNull(swagger.Paths["/api/Persons"][SwaggerOperationMethod.Post]);
            Assert.IsNotNull(swagger.Paths["/api/Persons/{id}"][SwaggerOperationMethod.Put]);
            Assert.IsNotNull(swagger.Paths["/api/Persons/{id}"][SwaggerOperationMethod.Delete]);
        }
Ejemplo n.º 36
0
        public void When_parameter_is_primitive_and_has_FromBody_then_it_is_a_path_parameter()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings
            {
                DefaultUrlTemplate = "api/{controller}/{action}/{id}"
            });

            //// Act
            var document = generator.GenerateForController<TestController>();
            var operation = document.Operations.Single(o => o.Operation.OperationId == "Test_WithFromBodyAttribute").Operation;

            //// Assert
            Assert.AreEqual(SwaggerParameterKind.Path, operation.ActualParameters[0].Kind); // TODO: What is correct?
        }
Ejemplo n.º 37
0
        public void When_operation_has_tag_attribute_with_AddToDocument_then_it_is_added_to_document()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiAssemblyToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController <TagsTest3Controller>();

            //// Assert
            Assert.AreEqual(1, document.Tags.Count);
            Assert.AreEqual("foo", document.Tags[0].Name);

            Assert.AreEqual(1, document.Operations.First().Operation.Tags.Count);
            Assert.AreEqual("foo", document.Operations.First().Operation.Tags[0]);
        }
Ejemplo n.º 38
0
        public void When_parameter_is_primitive_and_has_FromBody_then_it_is_a_path_parameter()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings
            {
                DefaultUrlTemplate = "api/{controller}/{action}/{id}"
            });

            //// Act
            var document  = generator.GenerateForController <TestController>();
            var operation = document.Operations.Single(o => o.Operation.OperationId == "Test_WithFromBodyAttribute").Operation;

            //// Assert
            Assert.AreEqual(SwaggerParameterKind.Path, operation.ActualParameters[0].Kind); // TODO: What is correct?
        }
Ejemplo n.º 39
0
        public void When_action_has_file_parameter_then_Stream_is_generated_in_CSharp_code()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
            var document  = generator.GenerateForController <FileUploadController>();

            //// Act
            var codeGen = new SwaggerToCSharpClientGenerator(document, new SwaggerToCSharpClientGeneratorSettings());
            var code    = codeGen.GenerateFile();

            //// Assert
            Assert.IsTrue(code.Contains("FileParameter file"));
            Assert.IsTrue(code.Contains("var content_ = new System.Net.Http.MultipartFormDataContent();"));
            Assert.IsTrue(code.Contains("content_.Add(new System.Net.Http.StreamContent(file.Data), \"file\""));
        }
Ejemplo n.º 40
0
        public void When_parameter_is_from_uri_and_has_file_then_two_params_and_consumes_is_correct()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController(typeof(FromUriFileParameterController));

            //// Assert
            var operation = document.Paths["/upload"][SwaggerOperationMethod.Post];

            Assert.AreEqual(JsonObjectType.File, operation.ActualParameters.Single(p => p.Name == "formFile").Type);
            Assert.IsTrue(operation.ActualParameters.Any(p => p.Name == "formFile"));
            Assert.IsTrue(operation.ActualParameters.Any(p => p.Name == "CustomLocationToSave"));
            Assert.AreEqual("multipart/form-data", operation.Consumes[0]);
        }
        public void When_there_is_a_ResultType_attribute_on_an_action_method_then_the_response_is_taken_from_the_given_type()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings
            {
                DefaultUrlTemplate = "api/{controller}/{action}/{id}"
            });

            //// Act
            var document  = generator.GenerateForController <PersonsController>();
            var operation = document.Operations.Single(o => o.Path == "/api/Persons/Get/{id}");

            //// Assert
            Assert.AreEqual(2, operation.Operation.Responses.Count);
            Assert.AreEqual("Person", operation.Operation.Responses["200"].ActualResponseSchema.GetTypeName(new DefaultTypeNameGenerator(), string.Empty));
        }
Ejemplo n.º 42
0
        public void When_parameter_is_from_uri_and_has_file_then_two_params_and_consumes_is_correct()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController(typeof(FromUriFileParameterController));

            //// Assert
            var operation = document.Paths["/upload"][SwaggerOperationMethod.Post];

            Assert.AreEqual(JsonObjectType.File, operation.ActualParameters.Single(p => p.Name == "formFile").Type);
            Assert.IsTrue(operation.ActualParameters.Any(p => p.Name == "formFile"));
            Assert.IsTrue(operation.ActualParameters.Any(p => p.Name == "CustomLocationToSave"));
            Assert.AreEqual("multipart/form-data", operation.Consumes[0]);
        }
Ejemplo n.º 43
0
        public void When_there_is_a_ResultType_attribute_on_an_action_method_then_the_response_is_taken_from_the_given_type()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings
            {
                DefaultUrlTemplate = "api/{controller}/{action}/{id}"
            });

            //// Act
            var document = generator.GenerateForController<PersonsController>();
            var operation = document.Operations.Single(o => o.Path == "/api/Persons/Get/{id}");
            var json = document.ToJson();

            //// Assert
            Assert.AreEqual(2, operation.Operation.Responses.Count);
            Assert.IsTrue(document.Definitions.Any(d => d.Key == "Person"));
        }
Ejemplo n.º 44
0
        public void When_parameter_is_file_collection_then_type_is_correct_and_collection_format_is_multi()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController(typeof(FileCollectionController));

            //// Assert
            var operation = document.Paths["/upload"][SwaggerOperationMethod.Post];
            var parameter = operation.ActualParameters.Single(p => p.Name == "files");

            Assert.AreEqual(JsonObjectType.File, parameter.Type);
            Assert.AreEqual(SwaggerParameterCollectionFormat.Multi, parameter.CollectionFormat);

            Assert.AreEqual("multipart/form-data", operation.Consumes[0]);
        }
Ejemplo n.º 45
0
        public void When_there_is_a_ResultType_attribute_on_an_action_method_then_the_response_is_taken_from_the_given_type()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings
            {
                DefaultUrlTemplate = "api/{controller}/{action}/{id}"
            });

            //// Act
            var document  = generator.GenerateForController <PersonsController>();
            var operation = document.Operations.Single(o => o.Path == "/api/Persons/Get/{id}");
            var json      = document.ToJson();

            //// Assert
            Assert.AreEqual(2, operation.Operation.Responses.Count);
            Assert.IsTrue(document.Definitions.Any(d => d.Key == "Person"));
        }
Ejemplo n.º 46
0
        public void When_ConfigurationClass_is_set_then_correct_ctor_is_generated()
        {
            //// Arrange
            var swaggerGenerator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
            var document         = swaggerGenerator.GenerateForController <FooController>();

            var generator = new SwaggerToCSharpClientGenerator(document, new SwaggerToCSharpClientGeneratorSettings
            {
                ConfigurationClass = "MyConfig",
                ClientBaseClass    = "MyBaseClass"
            });

            //// Act
            var code = generator.GenerateFile();

            //// Assert
            Assert.IsTrue(code.Contains("public FooClient(MyConfig configuration) : base(configuration)"));
        }
Ejemplo n.º 47
0
        public void When_parameter_is_from_uri_then_two_params_are_generated()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController(typeof(FromUriParameterController));

            //// Assert
            var operation = document.Paths["/upload"][SwaggerOperationMethod.Post];

            Assert.AreEqual(JsonObjectType.String, operation.ActualParameters.Single(p => p.Name == "Foo").Type);
            Assert.AreEqual(JsonObjectType.String, operation.ActualParameters.Single(p => p.Name == "Bar").Type);

            Assert.IsTrue(operation.ActualParameters.Any(p => p.Name == "Foo"));
            Assert.IsTrue(operation.ActualParameters.Any(p => p.Name == "Bar"));

            Assert.IsNull(operation.Consumes);
        }
Ejemplo n.º 48
0
        public void When_UseHttpRequestMessageCreationMethod_is_set_then_CreateRequestMessage_is_generated()
        {
            //// Arrange
            var swaggerGenerator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
            var document         = swaggerGenerator.GenerateForController <FooController>();

            var generator = new SwaggerToCSharpClientGenerator(document, new SwaggerToCSharpClientGeneratorSettings
            {
                ConfigurationClass = "MyConfig",
                ClientBaseClass    = "MyBaseClass",
                UseHttpRequestMessageCreationMethod = true
            });

            //// Act
            var code = generator.GenerateFile();

            //// Assert
            Assert.IsTrue(code.Contains("var request_ = await CreateHttpRequestMessageAsync(cancellationToken).ConfigureAwait(false);"));
        }
Ejemplo n.º 49
0
        public void When_route_has_path_parameter_which_is_not_an_action_parameter_then_it_is_still_added_as_path_parameter()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings
            {
                DefaultUrlTemplate       = "api/{controller}/{action}/{id}",
                AddMissingPathParameters = true
            });

            //// Act
            var document = generator.GenerateForController <RoutePrefixWithPathsController>();

            //// Assert
            var operation = document.Operations.First().Operation;
            var parameter = operation.Parameters.Single(p => p.Name == "companyIdentifier");

            Assert.AreEqual(2, operation.ActualParameters.Count);
            Assert.AreEqual(SwaggerParameterKind.Path, parameter.Kind);
            Assert.AreEqual(JsonObjectType.String, parameter.Type);
            Assert.AreEqual(JsonFormatStrings.Guid, parameter.Format);
        }
Ejemplo n.º 50
0
        public void When_controller_has_tag_attributes_then_they_are_processed()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiAssemblyToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController<TagsTest1Controller>();

            //// Assert
            Assert.AreEqual(4, document.Tags.Count);

            Assert.AreEqual("x", document.Tags[0].Name);
            Assert.AreEqual("y", document.Tags[1].Name);

            Assert.AreEqual("a1", document.Tags[2].Name);
            Assert.AreEqual("a2", document.Tags[2].Description);
            Assert.AreEqual(null, document.Tags[2].ExternalDocumentation);

            Assert.AreEqual("b1", document.Tags[3].Name);
            Assert.AreEqual("b2", document.Tags[3].Description);
            Assert.AreEqual("b3", document.Tags[3].ExternalDocumentation.Description);
            Assert.AreEqual("b4", document.Tags[3].ExternalDocumentation.Url);
        }
Ejemplo n.º 51
0
 public HttpResponseMessage Swagger()
 {
     var generator = new WebApiToSwaggerGenerator(Configuration.Routes.First(r => !string.IsNullOrEmpty(r.RouteTemplate)).RouteTemplate);
     var service = generator.GenerateForController(GetType(), "Swagger");
     return new HttpResponseMessage { Content = new StringContent(service.ToJson(), Encoding.UTF8) };
 }
Ejemplo n.º 52
0
 public HttpResponseMessage Swagger()
 {
     var generator = new WebApiToSwaggerGenerator(new WebApiAssemblyToSwaggerGeneratorSettings
     {
         DefaultUrlTemplate = Configuration.Routes.First(r => !string.IsNullOrEmpty(r.RouteTemplate)).RouteTemplate
     });
     var document = generator.GenerateForController(GetType());
     return new HttpResponseMessage { Content = new StringContent(document.ToJson(), Encoding.UTF8) };
 }
Ejemplo n.º 53
0
        public void When_parameter_is_file_collection_then_type_is_correct_and_collection_format_is_multi()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController(typeof(FileCollectionController));

            //// Assert
            var operation = document.Paths["/upload"][SwaggerOperationMethod.Post];
            var parameter = operation.ActualParameters.Single(p => p.Name == "files");

            Assert.AreEqual(JsonObjectType.File, parameter.Type);
            Assert.AreEqual(SwaggerParameterCollectionFormat.Multi, parameter.CollectionFormat);

            Assert.AreEqual("multipart/form-data", operation.Consumes[0]);
        }
Ejemplo n.º 54
0
        public void When_class_has_RouteAttribute_with_placeholders_then_they_are_correctly_replaced()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController(typeof(AccountController));

            //// Assert
            Assert.IsTrue(document.Paths.ContainsKey("/account/Get"));
            Assert.IsTrue(document.Paths.ContainsKey("/account/GetAll"));
            Assert.IsTrue(document.Paths.ContainsKey("/account/Post"));
            Assert.IsTrue(document.Paths.ContainsKey("/account/Verify"));
            Assert.IsTrue(document.Paths.ContainsKey("/account/Confirm"));
        }
Ejemplo n.º 55
0
        public void When_web_api_path_has_constraints_then_they_are_removed_in_the_swagger_spec()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController(typeof(ConstrainedRoutePathController));

            //// Assert
            var path = document.Paths.First().Key;

            Assert.AreEqual("/{id}", path);
        }
Ejemplo n.º 56
0
        public void When_route_has_path_parameter_which_is_not_an_action_parameter_then_it_is_still_added_as_path_parameter()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings
            {
                DefaultUrlTemplate = "api/{controller}/{action}/{id}", 
                AddMissingPathParameters = true
            });

            //// Act
            var document = generator.GenerateForController<RoutePrefixWithPathsController>();
            
            //// Assert
            var operation = document.Operations.First().Operation;
            var parameter = operation.Parameters.Single(p => p.Name == "companyIdentifier");

            Assert.AreEqual(2, operation.ActualParameters.Count);
            Assert.AreEqual(SwaggerParameterKind.Path, parameter.Kind);
            Assert.AreEqual(JsonObjectType.String, parameter.Type);
            Assert.AreEqual(JsonFormatStrings.Guid, parameter.Format);
        }
Ejemplo n.º 57
0
        public void When_operation_has_tag_attribute_with_AddToDocument_then_it_is_added_to_document()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiAssemblyToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController<TagsTest3Controller>();

            //// Assert
            Assert.AreEqual(1, document.Tags.Count);
            Assert.AreEqual("foo", document.Tags[0].Name);

            Assert.AreEqual(1, document.Operations.First().Operation.Tags.Count);
            Assert.AreEqual("foo", document.Operations.First().Operation.Tags[0]);
        }
Ejemplo n.º 58
0
        public void When_parameter_is_from_uri_then_two_params_are_generated()
        {
            //// Arrange
            var generator = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

            //// Act
            var document = generator.GenerateForController(typeof(FromUriParameterController));

            //// Assert
            var operation = document.Paths["/upload"][SwaggerOperationMethod.Post];

            Assert.AreEqual(JsonObjectType.String, operation.ActualParameters.Single(p => p.Name == "Foo").Type);
            Assert.AreEqual(JsonObjectType.String, operation.ActualParameters.Single(p => p.Name == "Bar").Type);

            Assert.IsTrue(operation.ActualParameters.Any(p => p.Name == "Foo"));
            Assert.IsTrue(operation.ActualParameters.Any(p => p.Name == "Bar"));

            Assert.IsNull(operation.Consumes);
        }
            internal string GenerateForSingleController(string assemblyPath, string controllerClassName, string urlTemplate, string jsonSchemaGeneratorSettingsData)
            {
                var jsonSchemaGeneratorSettings = JsonConvert.DeserializeObject<JsonSchemaGeneratorSettings>(jsonSchemaGeneratorSettingsData);

                var assembly = Assembly.LoadFrom(assemblyPath);
                var type = assembly.GetType(controllerClassName);

                var generator = new WebApiToSwaggerGenerator(urlTemplate, jsonSchemaGeneratorSettings);
                return generator.GenerateForController(type).ToJson();
            }