/// <summary>Initializes a new instance of the <see cref="SwaggerToCSharpGenerator" /> class.</summary>
        /// <param name="service">The service.</param>
        /// <param name="settings">The settings.</param>
        /// <exception cref="System.ArgumentNullException">service</exception>
        /// <exception cref="ArgumentNullException"><paramref name="service" /> is <see langword="null" />.</exception>
        public SwaggerToCSharpGenerator(SwaggerService service, SwaggerToCSharpGeneratorSettings settings)
        {
            if (service == null)
                throw new ArgumentNullException("service");

            Settings = settings; 

            _service = service;
            foreach (var definition in _service.Definitions)
                definition.Value.TypeName = definition.Key;

            _resolver = new SwaggerToCSharpTypeResolver(settings.CSharpGeneratorSettings, _service.Definitions);
        }
Esempio n. 2
0
        /// <summary>Initializes a new instance of the <see cref="SwaggerToCSharpGenerator" /> class.</summary>
        /// <param name="service">The service.</param>
        /// <param name="settings">The settings.</param>
        /// <exception cref="System.ArgumentNullException">service</exception>
        /// <exception cref="ArgumentNullException"><paramref name="service" /> is <see langword="null" />.</exception>
        public SwaggerToCSharpGenerator(SwaggerService service, SwaggerToCSharpGeneratorSettings settings)
        {
            if (service == null)
            {
                throw new ArgumentNullException("service");
            }

            Settings = settings;

            _service = service;
            foreach (var definition in _service.Definitions)
            {
                definition.Value.TypeName = definition.Key;
            }

            _resolver = new SwaggerToCSharpTypeResolver(settings.CSharpGeneratorSettings, _service.Definitions);
        }
Esempio n. 3
0
        public void When_generating_CSharp_code_then_output_contains_expected_classes()
        {
            // Arrange
            var service = CreateService();

            //// Act
            var settings = new SwaggerToCSharpGeneratorSettings { ClassName = "MyClass" };
            settings.CSharpGeneratorSettings.Namespace = "MyNamespace";

            var generator = new SwaggerToCSharpGenerator(service, settings);

            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"));
        }
Esempio n. 4
0
 public SwaggerToCSharpCommand()
 {
     Settings = new SwaggerToCSharpGeneratorSettings();
     Namespace = "MyNamespace";
 }
Esempio 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())); }"));
        }