Ejemplo n.º 1
0
        public override async Task RunAsync(CommandLineProcessor processor, IConsoleHost host)
        {
            var clientGenerator = new SwaggerToCSharpGenerator(InputSwaggerService);
            clientGenerator.Class = Class;
            clientGenerator.Namespace = Namespace;

            var output = clientGenerator.GenerateFile();
            WriteOutput(host, output);
        }
Ejemplo n.º 2
0
        public void When_generating_CSharp_code_then_output_contains_expected_classes()
        {
            // Arrange
            var service = CreateService();

            //// Act
            var generator = new SwaggerToCSharpGenerator(service);
            generator.Class = "MyClass";
            generator.Namespace = "MyNamespace";
            var code = generator.GenerateFile();

            // Assert
            Assert.IsTrue(code.Contains("namespace MyNamespace"));
            Assert.IsTrue(code.Contains("class MyClass"));
            Assert.IsTrue(code.Contains("class Person"));
            Assert.IsTrue(code.Contains("class Address"));
        }
        public Task GenerateClientAsync(string swaggerData)
        {
            return RunTaskAsync(async () =>
            {
                var code = string.Empty;
                await Task.Run(() =>
                {
                    if (!string.IsNullOrEmpty(swaggerData))
                    {
                        var service = SwaggerService.FromJson(swaggerData);

                        var codeGenerator = new SwaggerToCSharpGenerator(service);
                        codeGenerator.Class = ClassName;
                        codeGenerator.Namespace = Namespace;

                        code = codeGenerator.GenerateFile();
                    }
                });

                ClientCode = code;
            });
        }
Ejemplo n.º 4
0
 public async Task<string> RunAsync()
 {
     var clientGenerator = new SwaggerToCSharpGenerator(InputSwaggerService, Settings);
     return clientGenerator.GenerateFile();
 }
Ejemplo n.º 5
0
        public void When_parameter_is_array_then_CSharp_is_correct()
        {
            //// Arrange
            var swagger = @"{
  ""swagger"" : ""2.0"",
  ""info"" : {
    ""version"" : ""1.0.2"",
    ""title"" : ""Test API""
  },
  ""host"" : ""localhost:8080"",
  ""basePath"" : ""/"",
  ""tags"" : [ {
    ""name"" : ""api""
  } ],
  ""schemes"" : [ ""http"" ],
  ""paths"" : {
     ""/removeElement"" : {
      ""delete"" : {
        ""tags"" : [ ""api"" ],
        ""summary"" : ""Removes elements"",
        ""description"" : ""Removes elements"",
        ""operationId"" : ""removeElement"",
        ""consumes"" : [ ""application/json"" ],
        ""produces"" : [ ""application/json"" ],
        ""parameters"" : [ {
          ""name"" : ""X-User"",
          ""in"" : ""header"",
          ""description"" : ""User identifier"",
          ""required"" : true,
          ""type"" : ""string""
        }, {
          ""name"" : ""elementId"",
          ""in"" : ""query"",
          ""description"" : ""The ids of existing elements that should be removed"",
          ""required"" : false,
          ""type"" : ""array"",
          ""items"" : {
            ""type"" : ""integer"",
            ""format"" : ""int64""
          },
        } ],
        ""responses"" : {
          ""default"" : {
            ""description"" : ""successful operation""
          }
        }
      }
    }
  },
    ""definitions"" : { }
}
";
            var service = SwaggerService.FromJson(swagger);

            //// Act
            var settings = new SwaggerToCSharpGeneratorSettings { ClassName = "MyClass" };
            var generator = new SwaggerToCSharpGenerator(service, settings);
            var code = generator.GenerateFile();

            //// Assert
            Assert.IsTrue(code.Contains(@"foreach(var item in elementId) { url += string.Format(""elementId={0}&"", Uri.EscapeUriString(item.ToString())); }"));
        }