Skip to content

Commit

Permalink
Merge pull request RicoSuter#1212 from RSuter/master
Browse files Browse the repository at this point in the history
Release v11.15.4
  • Loading branch information
RicoSuter authored Mar 4, 2018
2 parents c2a9cce + 50366ef commit 7493ad8
Show file tree
Hide file tree
Showing 79 changed files with 584 additions and 319 deletions.
2 changes: 1 addition & 1 deletion src/NSwag.Annotations/NSwag.Annotations.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.0;net45</TargetFrameworks>
<Description>NSwag: The Swagger API toolchain for .NET and TypeScript</Description>
<Version>11.15.3</Version>
<Version>11.15.4</Version>
<PackageTags>Swagger Documentation WebApi AspNet TypeScript CodeGen</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</Copyright>
<PackageLicenseUrl>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
4 changes: 2 additions & 2 deletions src/NSwag.AspNet.Owin/NSwag.AspNet.Owin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>net45</TargetFrameworks>
<Description>NSwag: The Swagger API toolchain for .NET and TypeScript</Description>
<Version>11.15.3</Version>
<Version>11.15.4</Version>
<PackageTags>Swagger Documentation WebApi AspNet TypeScript CodeGen</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</Copyright>
<PackageLicenseUrl>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down Expand Up @@ -39,7 +39,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Owin" Version="3.0.1" />
<PackageReference Include="Microsoft.Owin.StaticFiles" Version="3.0.1" />
<PackageReference Include="NJsonSchema" Version="9.10.27" />
<PackageReference Include="NJsonSchema" Version="9.10.29" />
<PackageReference Include="Owin" Version="1.0" />
</ItemGroup>
<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/NSwag.AspNet.WebApi/NSwag.AspNet.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>net45</TargetFrameworks>
<Description>NSwag: The Swagger API toolchain for .NET and TypeScript</Description>
<Version>11.15.3</Version>
<Version>11.15.4</Version>
<PackageTags>Swagger Documentation WebApi AspNet TypeScript CodeGen</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</Copyright>
<PackageLicenseUrl>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</PackageLicenseUrl>
Expand All @@ -21,7 +21,7 @@
<DefineConstants>TRACE;DEBUG;NET45</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NJsonSchema" Version="9.10.27" />
<PackageReference Include="NJsonSchema" Version="9.10.29" />
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.3" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.3" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PlatformTarget>x86</PlatformTarget>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
<Version>11.15.3</Version>
<Version>11.15.4</Version>
<Authors>Rico Suter</Authors>
<Company>Rico Suter</Company>
<Description>NSwag: The Swagger API toolchain for .NET and TypeScript</Description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PlatformTarget Condition="'$(TargetFramework)'=='net461'">x64</PlatformTarget>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
<Version>11.15.3</Version>
<Version>11.15.4</Version>
<Authors>Rico Suter</Authors>
<Company>Rico Suter</Company>
<Description>NSwag: The Swagger API toolchain for .NET and TypeScript</Description>
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.AspNetCore/NSwag.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>net451;netstandard1.6</TargetFrameworks>
<Description>NSwag: The Swagger API toolchain for .NET and TypeScript</Description>
<Version>11.15.3</Version>
<Version>11.15.4</Version>
<PackageTags>Swagger Documentation WebApi AspNet TypeScript CodeGen</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</Copyright>
<PackageLicenseUrl>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
1 change: 1 addition & 0 deletions src/NSwag.AspNetCore/SwaggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public static IApplicationBuilder UseSwaggerWithApiDescription(
/// This is currently only required in conjunction with
/// <see cref="SwaggerExtensions.UseSwaggerWithApiDescription(IApplicationBuilder, Action{AspNetCoreToSwaggerMiddlewareSettings})"/>.
/// </remarks>
[Obsolete("This method is currently experimental.")]
public static IServiceCollection AddSwagger(this IServiceCollection serviceCollection)
{
return serviceCollection.AddSingleton<IConfigureOptions<MvcOptions>, NSwagConfigureMvcOptions>();
Expand Down
4 changes: 2 additions & 2 deletions src/NSwag.AssemblyLoader/NSwag.AssemblyLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.6;net451</TargetFrameworks>
<Description>NSwag: The Swagger API toolchain for .NET and TypeScript</Description>
<Version>11.15.3</Version>
<Version>11.15.4</Version>
<PackageTags>Swagger Documentation WebApi AspNet TypeScript CodeGen</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</Copyright>
<PackageLicenseUrl>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</PackageLicenseUrl>
Expand All @@ -24,7 +24,7 @@
<DefineConstants>TRACE;RELEASE;FullNet;NET451</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NJsonSchema" Version="9.10.27" />
<PackageReference Include="NJsonSchema" Version="9.10.29" />
<PackageReference Include="System.Net.Http" Version="4.3.2" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.6' ">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ namespace NSwag.CodeGeneration.CSharp.Tests
[TestClass]
public class ControllerGenerationFormatTests
{
public class ComplexType
{
public string Prop1 { get; set; }
public int Prop2 { get; set; }
public bool Prop3 { get; set; }
public ComplexType Prop4 { get; set; }
}

public class TestController : ApiController
{
[Route("Foo")]
Expand All @@ -23,6 +31,12 @@ public void Bar()
{
throw new NotImplementedException();
}

[Route("Complex")]
public void Complex([FromBody] ComplexType complexType)
{
throw new NotImplementedException();
}
}

[TestMethod]
Expand All @@ -43,7 +57,7 @@ public async Task When_controllergenerationformat_abstract_then_abstractcontroll
Assert.IsTrue(code.Contains("abstract class TestController"));
Assert.IsFalse(code.Contains("ITestController"));
Assert.IsFalse(code.Contains("private ITestController _implementation;"));
Assert.IsFalse(code.Contains("partial"));
Assert.IsFalse(code.Contains("partial class TestController"));
}

[TestMethod]
Expand Down Expand Up @@ -86,6 +100,51 @@ public async Task When_controllergenerationformat_notsetted_then_partialcontroll
Assert.IsTrue(code.Contains("private ITestController _implementation;"));
Assert.IsFalse(code.Contains("abstract class TestController"));
}

[TestMethod]
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()"));
}

[TestMethod]
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,
AspNetNamespace = "MyCustomNameSpace"
};

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

//// Assert
Assert.IsTrue(code.Contains("abstract 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()"));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NJsonSchema, Version=9.10.27.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.9.10.27\lib\net45\NJsonSchema.dll</HintPath>
<Reference Include="NJsonSchema, Version=9.10.29.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.9.10.29\lib\net45\NJsonSchema.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.CodeGeneration.CSharp.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net451" />
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="net462" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net451" />
<package id="NJsonSchema" version="9.10.27" targetFramework="net462" />
<package id="NJsonSchema" version="9.10.29" targetFramework="net462" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ public CSharpClientTemplateModel(
/// <summary>Gets or sets the format for DateTime type method parameters.</summary>
public string ParameterDateTimeFormat => _settings.ParameterDateTimeFormat;

/// <summary>Gets or sets a value indicating whether to expose the JsonSerializerSettings property.</summary>
public bool ExposeJsonSerializerSettings => _settings.ExposeJsonSerializerSettings;

/// <summary>Gets or sets a value indicating whether to generate the UpdateJsonSerializerSettings method.</summary>
public bool GenerateUpdateJsonSerializerSettingsMethod => _settings.GenerateUpdateJsonSerializerSettingsMethod;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;net451</TargetFrameworks>
<Description>NSwag: The Swagger API toolchain for .NET and TypeScript</Description>
<Version>11.15.3</Version>
<Version>11.15.4</Version>
<PackageTags>Swagger Documentation WebApi AspNet TypeScript CodeGen</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</Copyright>
<PackageLicenseUrl>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</PackageLicenseUrl>
Expand All @@ -19,11 +19,13 @@
</PropertyGroup>
<ItemGroup>
<None Remove="Templates\Client.Class.BeforeSend.liquid" />
<None Remove="Templates\Client.Class.ConvertToString.liquid" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Templates\Client.Class.BeforeSend.liquid" />
<EmbeddedResource Include="Templates\Client.Class.liquid" />
<EmbeddedResource Include="Templates\Client.Class.ProcessResponse.liquid" />
<EmbeddedResource Include="Templates\Client.Class.ConvertToString.liquid" />
<EmbeddedResource Include="Templates\Client.Interface.Body.liquid" />
<EmbeddedResource Include="Templates\Client.Interface.liquid" />
<EmbeddedResource Include="Templates\Client.liquid" />
Expand All @@ -33,7 +35,7 @@
<EmbeddedResource Include="Templates\JsonExceptionConverter.liquid" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="NJsonSchema.CodeGeneration.CSharp" Version="9.10.27" />
<PackageReference Include="NJsonSchema.CodeGeneration.CSharp" Version="9.10.29" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net451'">
<Reference Include="Microsoft.CSharp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public SwaggerToCSharpClientGeneratorSettings()
GenerateUpdateJsonSerializerSettingsMethod = true;
QueryNullValue = "";
GenerateBaseUrlProperty = true;
ExposeJsonSerializerSettings = false;
}

/// <summary>Gets or sets the full name of the base class.</summary>
Expand Down Expand Up @@ -88,5 +89,8 @@ public SwaggerToCSharpClientGeneratorSettings()

/// <summary>Gets or sets the null value used for query parameters which are null (default: '').</summary>
public string QueryNullValue { get; set; }

/// <summary>Gets or sets a value indicating whether to expose the JsonSerializerSettings property (default: false).</summary>
public bool ExposeJsonSerializerSettings { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
private string ConvertToString(object value, System.Globalization.CultureInfo cultureInfo)
{
if (value is System.Enum)
{
string name = System.Enum.GetName(value.GetType(), value);
if (name != null)
{
var field = System.Reflection.IntrospectionExtensions.GetTypeInfo(value.GetType()).GetDeclaredField(name);
if (field != null)
{
var attribute = System.Reflection.CustomAttributeExtensions.GetCustomAttribute(field, typeof(System.Runtime.Serialization.EnumMemberAttribute))
as System.Runtime.Serialization.EnumMemberAttribute;
if (attribute != null)
{
return attribute.Value;
}
}
}
}
else if (value is byte[])
{
return System.Convert.ToBase64String((byte[]) value);
}
else if (value.GetType().IsArray)
{
var array = System.Linq.Enumerable.OfType<object>((System.Array) value);
return string.Join(",", System.Linq.Enumerable.Select(array, o => ConvertToString(o, cultureInfo)));
}

return System.Convert.ToString(value, cultureInfo);
}
30 changes: 3 additions & 27 deletions src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
}

{% endif -%}
{% if ExposeJsonSerializerSettings %}public{% else %}protected{% endif %} Newtonsoft.Json.JsonSerializerSettings JsonSerializerSettings { get { return _settings.Value; } }

{% if GenerateUpdateJsonSerializerSettingsMethod -%}
partial void UpdateJsonSerializerSettings(Newtonsoft.Json.JsonSerializerSettings settings);
{% endif -%}
Expand Down Expand Up @@ -276,31 +278,5 @@ urlBuilder_.Append("{{ parameter.Name }}=").Append(System.Uri.EscapeDataString({
}

{% endfor -%}
private string ConvertToString(object value, System.Globalization.CultureInfo cultureInfo)
{
if (value is System.Enum)
{
string name = System.Enum.GetName(value.GetType(), value);
if (name != null)
{
var field = System.Reflection.IntrospectionExtensions.GetTypeInfo(value.GetType()).GetDeclaredField(name);
if (field != null)
{
var attribute = System.Reflection.CustomAttributeExtensions.GetCustomAttribute(field, typeof(System.Runtime.Serialization.EnumMemberAttribute))
as System.Runtime.Serialization.EnumMemberAttribute;
if (attribute != null)
{
return attribute.Value;
}
}
}
}
else if (value.GetType().IsArray)
{
var array = System.Linq.Enumerable.OfType<object>((System.Array) value);
return string.Join(",", System.Linq.Enumerable.Select(array, o => ConvertToString(o, cultureInfo)));
}

return System.Convert.ToString(value, cultureInfo);
}
{% template Client.Class.ConvertToString %}
}
Loading

0 comments on commit 7493ad8

Please sign in to comment.