コード例 #1
0
        public async Task When_controllertarget_aspnet_and_multiple_controllers_then_only_single_custom_fromheader_generated()
        {
            //// Arrange
            var document = await GetSwaggerDocument();

            var settings = new SwaggerToCSharpControllerGeneratorSettings
            {
                ControllerTarget       = CSharpControllerTarget.AspNet,
                ControllerStyle        = CSharpControllerStyle.Abstract,
                OperationNameGenerator = new MultipleClientsFromFirstTagAndPathSegmentsOperationNameGenerator()
            };

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, settings);
            var code    = codeGen.GenerateFile();

            //// Assert
            var fromHeaderCustomAttributeCount = Regex.Matches(code, "public class FromHeaderAttribute :").Count;

            Assert.Equal(1, fromHeaderCustomAttributeCount);
            var fromHeaderCustomBindingCount = Regex.Matches(code, "public class FromHeaderBinding :").Count;

            Assert.Equal(1, fromHeaderCustomBindingCount);
            Assert.Contains("[FromHeader", code);
            Assert.DoesNotContain("[Microsoft.AspNetCore.Mvc.FromHeader", code);
        }
コード例 #2
0
 public async Task <string> RunAsync()
 {
     return(await Task.Run(async() =>
     {
         var document = await GetInputSwaggerDocument().ConfigureAwait(false);
         var clientGenerator = new SwaggerToCSharpControllerGenerator(document, Settings);
         return clientGenerator.GenerateFile();
     }));
 }
コード例 #3
0
 public new async Task <string> RunAsync()
 {
     return(await Task.Run(async() =>
     {
         var document = await GetInputSwaggerDocument().ConfigureAwait(false);
         _preProcessor?.ApplyProcessing(document);
         var clientGenerator = new SwaggerToCSharpControllerGenerator(document, Settings);
         return _sourceCodePrefixer.AddPrefixTemplate(clientGenerator.GenerateFile());
     }));
 }
コード例 #4
0
        public async Task When_controllergenerationformat_notsetted_then_partialcontroller_is_generated()
        {
            //// Arrange
            var document = await GetSwaggerDocument();

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, new SwaggerToCSharpControllerGeneratorSettings());
            var code    = codeGen.GenerateFile();

            //// Assert
            Assert.Contains("partial class TestController", code);
            Assert.Contains("ITestController", code);
            Assert.Contains("private ITestController _implementation;", code);
            Assert.DoesNotContain("abstract class TestController", code);
        }
コード例 #5
0
        public async Task When_usecancellationtokenparameter_notsetted_then_cancellationtoken_isnot_added()
        {
            //// Arrange
            var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
            var document   = await swaggerGen.GenerateForControllerAsync <TestController>();

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, new SwaggerToCSharpControllerGeneratorSettings
            {
                ControllerStyle = CSharpControllerStyle.Abstract
            });
            var code = codeGen.GenerateFile();

            //// Assert
            Assert.False(code.Contains("System.Threading.CancellationToken cancellationToken"));
        }
コード例 #6
0
        public async Task When_controllerstyleisabstract_and_usecancellationtokenistrue_and_requesthasparameter_then_cancellationtoken_is_added()
        {
            //// Arrange
            var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
            var document   = await swaggerGen.GenerateForControllerAsync <TestController>();

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, new SwaggerToCSharpControllerGeneratorSettings
            {
                ControllerStyle      = CSharpControllerStyle.Abstract,
                UseCancellationToken = true
            });
            var code = codeGen.GenerateFile();

            //// Assert
            Assert.Contains("System.Threading.Tasks.Task<string> Foo(string test, bool test2, System.Threading.CancellationToken cancellationToken)", code);
        }
コード例 #7
0
        public async Task When_success_responses_are_wrapped_then_SwaggerResponse_is_returned_web_api()
        {
            //// Arrange
            var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
            var document   = await swaggerGen.GenerateForControllerAsync <TestController>();

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, new SwaggerToCSharpControllerGeneratorSettings
            {
                WrapResponses = true
            });
            var code = codeGen.GenerateFile();

            //// Assert
            Assert.IsTrue(code.Contains("Task<SwaggerResponse<string>>"));
            Assert.IsTrue(code.Contains("Task<SwaggerResponse>"));
        }
コード例 #8
0
        public async Task When_aspnet_actiontype_inuse_with_abstract_then_actiontype_is_generated()
        {
            //// Arrange
            var document = await GetSwaggerDocument();

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, new SwaggerToCSharpControllerGeneratorSettings
            {
                ControllerStyle     = CSharpControllerStyle.Abstract,
                UseActionResultType = true
            });
            var code = codeGen.GenerateFile();

            //// Assert
            Assert.Contains("public abstract System.Threading.Tasks.Task<Microsoft.AspNetCore.Mvc.ActionResult<string>> Foo(string test, bool? test2);", code);
            Assert.Contains("public abstract System.Threading.Tasks.Task<Microsoft.AspNetCore.Mvc.IActionResult> Bar();", code);
        }
コード例 #9
0
        public async Task When_controller_has_operation_with_complextype_then_partialcontroller_is_generated_with_frombody_attribute()
        {
            //// Arrange
            var document = await GetSwaggerDocument();

            var settings = new SwaggerToCSharpControllerGeneratorSettings();

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, settings);
            var code    = codeGen.GenerateFile();

            //// Assert
            Assert.Contains("partial class TestController", code);
            Assert.Contains($"Complex([Microsoft.AspNetCore.Mvc.FromBody] ComplexType complexType)", code);
            Assert.Contains("Foo(string test, bool? test2)", code);
            Assert.Contains("Bar()", code);
        }
コード例 #10
0
        public async Task When_controllerroutenamingstrategy_none_then_route_attribute_name_not_specified()
        {
            //// Arrange
            var document = await GetSwaggerDocument();

            var settings = new SwaggerToCSharpControllerGeneratorSettings
            {
                RouteNamingStrategy = CSharpControllerRouteNamingStrategy.None
            };

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, settings);
            var code    = codeGen.GenerateFile();

            //// Assert
            Assert.Contains("Route(\"Foo\")", code);
            Assert.Contains("Route(\"Bar\")", code);
        }
コード例 #11
0
        public async Task When_controllerstyleispartial_and_usecancellationtokenistrue_and_requesthasparameter_then_cancellationtoken_is_added()
        {
            //// Arrange
            var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
            var document   = await swaggerGen.GenerateForControllerAsync <TestController>();

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, new SwaggerToCSharpControllerGeneratorSettings
            {
                UseCancellationToken = true
            });
            var code = codeGen.GenerateFile();

            //// Assert
            Assert.IsTrue(code.Contains("System.Threading.Tasks.Task<string> FooAsync(string test, bool test2, System.Threading.CancellationToken cancellationToken)"));
            Assert.IsTrue(code.Contains("_implementation.FooAsync(test, test2, cancellationToken);"));
            Assert.IsTrue(code.Contains("public System.Threading.Tasks.Task<string> Foo(string test, bool test2, System.Threading.CancellationToken cancellationToken)"));
        }
コード例 #12
0
        public async Task When_controller_has_operation_with_header_parameter_then_partialcontroller_is_generated_with_fromheader_attribute()
        {
            //// Arrange
            var document = await GetSwaggerDocument();

            var settings = new SwaggerToCSharpControllerGeneratorSettings
            {
                ControllerTarget = CSharpControllerTarget.AspNet
            };

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, settings);
            var code    = codeGen.GenerateFile();

            //// Assert
            Assert.Contains("partial class TestController", code);
            Assert.Contains($"HeaderParam([FromHeader] string comesFromHeader)", code);
        }
コード例 #13
0
        public async Task When_controllergenerationformat_notsetted_then_partialcontroller_is_generated()
        {
            //// Arrange
            var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
            var document   = await swaggerGen.GenerateForControllerAsync <TestController>();

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, new SwaggerToCSharpControllerGeneratorSettings
            {
            });
            var code = codeGen.GenerateFile();

            //// Assert
            Assert.IsTrue(code.Contains("partial class TestController"));
            Assert.IsTrue(code.Contains("ITestController"));
            Assert.IsTrue(code.Contains("private ITestController _implementation;"));
            Assert.IsFalse(code.Contains("abstract class TestController"));
        }
コード例 #14
0
        public async Task When_the_generation_of_dto_classes_are_disabled_then_file_is_generated_without_any_dto_clasess()
        {
            //// Arrange
            var document = await GetSwaggerDocument();

            var settings = new SwaggerToCSharpControllerGeneratorSettings
            {
                GenerateDtoTypes = false
            };

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, settings);
            var code    = codeGen.GenerateFile();

            //// Assert
            Assert.DoesNotContain("public partial class ComplexType", code);
            Assert.DoesNotContain("public partial class ComplexTypeResponse", code);
        }
コード例 #15
0
        public async Task When_controllerstyleispartial_and_usecancellationtokenistrue_and_requesthasnoparameter_then_cancellationtoken_is_added()
        {
            //// Arrange
            var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
            var document   = await swaggerGen.GenerateForControllerAsync <TestController>();

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, new SwaggerToCSharpControllerGeneratorSettings
            {
                UseCancellationToken = true
            });
            var code = codeGen.GenerateFile();

            //// Assert
            Assert.Contains("(System.Threading.CancellationToken cancellationToken)", code);
            Assert.Contains("_implementation.BarAsync(cancellationToken)", code);
            Assert.Contains("System.Threading.Tasks.Task BarAsync(System.Threading.CancellationToken cancellationToken)", code);
        }
コード例 #16
0
        public async Task When_controllergenerationformat_abstract_then_partialcontroller_is_generated()
        {
            //// Arrange
            var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
            var document   = await swaggerGen.GenerateForControllerAsync <TestController>();

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, new SwaggerToCSharpControllerGeneratorSettings
            {
                ControllerStyle = CSharpControllerStyle.Partial,
            });
            var code = codeGen.GenerateFile();

            //// Assert
            Assert.Contains("partial class TestController", code);
            Assert.Contains("ITestController", code);
            Assert.Contains("private ITestController _implementation;", code);
            Assert.DoesNotContain("abstract class TestController", code);
        }
コード例 #17
0
        public async Task When_controllerroutenamingstrategy_operationid_then_route_attribute_name_specified()
        {
            //// Arrange
            var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
            var document   = await swaggerGen.GenerateForControllerAsync <TestController>();

            var settings = new SwaggerToCSharpControllerGeneratorSettings
            {
                RouteNamingStrategy = CSharpControllerRouteNamingStrategy.OperationId
            };

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, settings);
            var code    = codeGen.GenerateFile();

            //// Assert
            Assert.Contains("Route(\"Foo\", Name = \"Test_Foo\")", code);
            Assert.Contains("Route(\"Bar\", Name = \"Test_Bar\")", code);
        }
コード例 #18
0
        public async Task When_controllertarget_aspnetcore_then_use_builtin_fromheader()
        {
            //// Arrange
            var document = await GetSwaggerDocument();

            var settings = new SwaggerToCSharpControllerGeneratorSettings
            {
                ControllerTarget = CSharpControllerTarget.AspNetCore
            };

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, settings);
            var code    = codeGen.GenerateFile();

            //// Assert
            Assert.Contains("[Microsoft.AspNetCore.Mvc.FromHeader", code);
            Assert.DoesNotContain("[FromHeader", code);
            Assert.DoesNotContain("public class FromHeaderBinding :", code);
            Assert.DoesNotContain("public class FromHeaderAttribute :", code);
        }
コード例 #19
0
        public async Task When_aspnet_actiontype_inuse_with_partial_then_actiontype_is_generated()
        {
            //// Arrange
            var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
            var document   = await swaggerGen.GenerateForControllerAsync <TestController>();

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, new SwaggerToCSharpControllerGeneratorSettings
            {
                ControllerStyle     = CSharpControllerStyle.Partial,
                UseActionResultType = true
            });
            var code = codeGen.GenerateFile();

            //// Assert
            Assert.Contains("System.Threading.Tasks.Task<Microsoft.AspNetCore.Mvc.ActionResult<string>> FooAsync(string test, bool test2);", code);
            Assert.Contains("public System.Threading.Tasks.Task<Microsoft.AspNetCore.Mvc.ActionResult<string>> Foo(string test, bool test2)", code);
            Assert.Contains("System.Threading.Tasks.Task<Microsoft.AspNetCore.Mvc.IActionResult> BarAsync();", code);
            Assert.Contains("public System.Threading.Tasks.Task<Microsoft.AspNetCore.Mvc.IActionResult> Bar()", code);
        }
コード例 #20
0
        public async Task When_controller_has_operation_with_complextype_then_partialcontroller_is_generated_with_frombody_attribute()
        {
            //// Arrange
            var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
            var document   = await swaggerGen.GenerateForControllerAsync <TestController>();

            var settings = new SwaggerToCSharpControllerGeneratorSettings
            {
                AspNetNamespace = "MyCustomNameSpace"
            };

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, settings);
            var code    = codeGen.GenerateFile();

            //// Assert
            Assert.IsTrue(code.Contains("partial class TestController"));
            Assert.IsTrue(code.Contains($"Complex([{settings.AspNetNamespace}.FromBody] ComplexType complexType)"));
            Assert.IsTrue(code.Contains("Foo(string test, bool test2)"));
            Assert.IsTrue(code.Contains("Bar()"));
        }
コード例 #21
0
        public async Task When_controller_has_operation_with_complextype_then_abstractcontroller_is_generated_with_frombody_attribute()
        {
            //// Arrange
            var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
            var document   = await swaggerGen.GenerateForControllerAsync <TestController>();

            var settings = new SwaggerToCSharpControllerGeneratorSettings
            {
                ControllerStyle = CSharpControllerStyle.Abstract,
            };

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, settings);
            var code    = codeGen.GenerateFile();

            //// Assert
            Assert.Contains("abstract class TestController", code);
            Assert.Contains($"Complex([Microsoft.AspNetCore.Mvc.FromBody] ComplexType complexType)", code);
            Assert.Contains("Foo(string test, bool test2)", code);
            Assert.Contains("Bar()", code);
        }
コード例 #22
0
        public async Task When_controller_has_operations_with_required_parameters_then_partialcontroller_is_generated_with_bindrequired_attribute()
        {
            //// Arrange
            var document = await GetSwaggerDocument();

            var settings = new SwaggerToCSharpControllerGeneratorSettings
            {
                GenerateModelValidationAttributes = true,
            };

            //// Act
            var codeGen = new SwaggerToCSharpControllerGenerator(document, settings);
            var code    = codeGen.GenerateFile();

            //// Assert
            Assert.Contains("partial class TestController", code);
            Assert.Contains($"Complex([Microsoft.AspNetCore.Mvc.FromBody] ComplexType complexType)", code);
            Assert.Contains($"ComplexRequired([Microsoft.AspNetCore.Mvc.FromBody] [Microsoft.AspNetCore.Mvc.ModelBinding.BindRequired] ComplexType complexType)", code);
            Assert.Contains($"Foo(string test, bool? test2)", code);
            Assert.Contains($"FooRequired([Microsoft.AspNetCore.Mvc.ModelBinding.BindRequired] string test, [Microsoft.AspNetCore.Mvc.ModelBinding.BindRequired] bool test2)", code);
            Assert.Contains("Bar()", code);
        }
コード例 #23
0
        public void When_parameter_has_default_then_set_in_partial_controller()
        {
            //// Arrange
            var document = new SwaggerDocument();

            document.Paths["foo/bar"] = new SwaggerPathItem
            {
                {
                    SwaggerOperationMethod.Get,
                    new SwaggerOperation {
                        Parameters =
                        {
                            new SwaggerParameter {
                                Name       = "booldef",
                                IsRequired = false,
                                Default    = true,
                                Kind       = SwaggerParameterKind.Query,
                                Type       = JsonObjectType.Boolean
                            },
                            new SwaggerParameter {
                                Name       = "intdef",
                                IsRequired = false,
                                Default    = 42,
                                Kind       = SwaggerParameterKind.Query,
                                Type       = JsonObjectType.Integer
                            },
                            new SwaggerParameter {
                                Name       = "bar",
                                IsRequired = false,
                                Kind       = SwaggerParameterKind.Query,
                                Type       = JsonObjectType.Integer
                            },
                            new SwaggerParameter {
                                Name       = "doubledef",
                                IsRequired = false,
                                Default    = 0.6822871999174,
                                Kind       = SwaggerParameterKind.Query,
                                Type       = JsonObjectType.Number
                            },
                            new SwaggerParameter {
                                Name       = "decdef",
                                IsRequired = false,
                                Default    = 79228162514264337593543950335M,
                                Kind       = SwaggerParameterKind.Query,
                                Type       = JsonObjectType.Number,
                                Format     = JsonFormatStrings.Decimal
                            },
                            new SwaggerParameter {
                                Name       = "abc",
                                IsRequired = true,
                                Default    = 84,
                                Kind       = SwaggerParameterKind.Query,
                                Type       = JsonObjectType.Integer
                            },
                            new SwaggerParameter {
                                Name       = "strdef",
                                Default    = @"default""string""",
                                IsRequired = false,
                                Kind       = SwaggerParameterKind.Query,
                                Type       = JsonObjectType.String
                            }
                        }
                    }
                }
            };

            //// Act
            var generator =
                new SwaggerToCSharpControllerGenerator(document,
                                                       new SwaggerToCSharpControllerGeneratorSettings {
                GenerateOptionalParameters = true, AspNetNamespace = ""
            });
            var code = generator.GenerateFile();

            //// Assert
            Assert.Contains("_implementation.BarAsync(abc, booldef ?? true, intdef ?? 42, doubledef ?? 0.6822871999174D, decdef ?? 79228162514264337593543950335M, strdef ?? \"default\\\"string\\\"\", bar)", code);
            Assert.Contains("BarAsync(int abc, bool booldef, int intdef, double doubledef, decimal decdef, string strdef, int? bar = null);", code);

            var trimmedCode = RemoveExternalReferences(code);

            //CompilerParameters parameters = new CompilerParameters { GenerateInMemory = true };

            //var result = new CSharpCodeProvider().CompileAssemblyFromSource(parameters, trimmedCode);
            //if (result.Errors.Count > 0)
            //{
            //    foreach (var error in result.Errors)
            //    {
            //        Console.WriteLine(error.ToString());
            //    }
            //}

            //Assert.True(result.Errors.Count == 0);
        }