Skip to content

Commit

Permalink
Merge pull request #127 from NSwag/master
Browse files Browse the repository at this point in the history
Release v2.32
  • Loading branch information
RicoSuter committed May 20, 2016
2 parents a484a37 + d7599b7 commit 96dbf1b
Show file tree
Hide file tree
Showing 34 changed files with 942 additions and 802 deletions.
2 changes: 1 addition & 1 deletion src/NSwag.Annotations/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
[assembly: AssemblyCompany("Rico Suter")]
[assembly: AssemblyProduct("NSwag.Annotations")]
[assembly: AssemblyCopyright("Copyright © Rico Suter, 2015")]
[assembly: AssemblyVersion("2.31.*")]
[assembly: AssemblyVersion("2.32.*")]
4 changes: 2 additions & 2 deletions src/NSwag.AssemblyLoader/NSwag.AssemblyLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NJsonSchema, Version=2.22.5984.16716, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.2.22.5984.16716\lib\portable45-net45+win8+wp8+wpa81\NJsonSchema.dll</HintPath>
<Reference Include="NJsonSchema, Version=2.23.5984.32960, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.2.23.5984.32960\lib\portable45-net45+win8+wp8+wpa81\NJsonSchema.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.AssemblyLoader/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
[assembly: AssemblyCompany("Rico Suter")]
[assembly: AssemblyProduct("NSwag.AssemblyLoader")]
[assembly: AssemblyCopyright("Copyright © Rico Suter, 2015")]
[assembly: AssemblyVersion("2.31.*")]
[assembly: AssemblyVersion("2.32.*")]
2 changes: 1 addition & 1 deletion src/NSwag.AssemblyLoader/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="NJsonSchema" publicKeyToken="c2f9c3bdfae56102" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.22.5984.16716" newVersion="2.22.5984.16716" />
<bindingRedirect oldVersion="0.0.0.0-2.23.5984.32960" newVersion="2.23.5984.32960" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.AssemblyLoader/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net452" />
<package id="NJsonSchema" version="2.22.5984.16716" targetFramework="net45" />
<package id="NJsonSchema" version="2.23.5984.32960" targetFramework="net45" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NJsonSchema, Version=2.22.5984.16716, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.2.22.5984.16716\lib\portable45-net45+win8+wp8+wpa81\NJsonSchema.dll</HintPath>
<Reference Include="NJsonSchema, Version=2.23.5984.32960, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.2.23.5984.32960\lib\portable45-net45+win8+wp8+wpa81\NJsonSchema.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NJsonSchema.CodeGeneration, Version=2.22.5984.16718, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.CodeGeneration.2.22.5984.16718\lib\portable45-net45+win8+wp8+wpa81\NJsonSchema.CodeGeneration.dll</HintPath>
<Reference Include="NJsonSchema.CodeGeneration, Version=2.23.5984.32961, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.CodeGeneration.2.23.5984.32961\lib\portable45-net45+win8+wp8+wpa81\NJsonSchema.CodeGeneration.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.CodeGeneration.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("2.31.*")]
// [assembly: AssemblyVersion("2.32.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using System.Linq;
using System.Web.Http;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using NJsonSchema;
using NSwag.CodeGeneration.SwaggerGenerators.WebApi;

namespace NSwag.CodeGeneration.Tests.WebApiToSwaggerGenerator
Expand All @@ -25,5 +29,118 @@ public void When_controller_type_is_not_found_then_type_load_exception_is_thrown

//// Assert
}

public interface IFormFile
{
}

public interface IActionResult
{
}

public class FromUriFileParameterController : ApiController
{
public class ComplexClass
{
[JsonProperty("formFile")]
public IFormFile FormFile { get; set; }

public string CustomLocationToSave { get; set; }
}

[HttpPost, Route("upload")]
public IActionResult Upload([FromUri] ComplexClass parameters)
{
// TODO: Check if this is the correct behavior or if FromUri can be omitted when a property is a file
throw new NotImplementedException();
}
}

[TestMethod]
public void When_parameter_is_from_uri_and_has_file_then_two_params_and_consumes_is_correct()
{
//// Arrange
var generator = new SwaggerGenerators.WebApi.WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

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

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

Assert.AreEqual(JsonObjectType.File, operation.Parameters.Single(p => p.Name == "formFile").Type);
Assert.IsTrue(operation.Parameters.Any(p => p.Name == "formFile"));
Assert.IsTrue(operation.Parameters.Any(p => p.Name == "CustomLocationToSave"));
Assert.AreEqual("multipart/form-data", operation.Consumes[0]);
}

public interface IFormFileCollection
{
}

public class FileCollectionController : ApiController
{
[HttpPost, Route("upload")]
public IActionResult Upload(IFormFileCollection files)
{
throw new NotImplementedException();
}
}

[TestMethod]
public void When_parameter_is_file_collection_then_type_is_correct_and_collection_format_is_multi()
{
//// Arrange
var generator = new SwaggerGenerators.WebApi.WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

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

//// Assert
var operation = service.Paths["upload"][SwaggerOperationMethod.Post];
var parameter = operation.Parameters.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]);
}

public class FromUriParameterController : ApiController
{
public class ComplexClass
{
public string Foo { get; set; }

public string Bar { get; set; }
}

[HttpPost, Route("upload")]
public IActionResult Upload([FromUri]ComplexClass parameters)
{
throw new NotImplementedException();
}
}

[TestMethod]
public void When_parameter_is_from_uri_then_two_params_are_generated()
{
//// Arrange
var generator = new SwaggerGenerators.WebApi.WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());

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

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

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

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

Assert.IsNull(operation.Consumes);
}
}
}
4 changes: 2 additions & 2 deletions src/NSwag.CodeGeneration.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net45" />
<package id="NJsonSchema" version="2.22.5984.16716" targetFramework="net45" />
<package id="NJsonSchema.CodeGeneration" version="2.22.5984.16718" targetFramework="net45" />
<package id="NJsonSchema" version="2.23.5984.32960" targetFramework="net45" />
<package id="NJsonSchema.CodeGeneration" version="2.23.5984.32961" targetFramework="net45" />
</packages>
21 changes: 16 additions & 5 deletions src/NSwag.CodeGeneration/CodeGenerators/ClientGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ internal List<OperationModel> GetOperations<TGenerator>(SwaggerService service,
{
Path = tuple.Path,
HttpMethod = tuple.HttpMethod,
Operation = tuple.Operation,
Operation = tuple.Operation,
OperationName = BaseSettings.OperationNameGenerator.GetOperationName(service, tuple.Path, tuple.HttpMethod, tuple.Operation),

ResultType = GetResultType(operation),
Expand All @@ -102,21 +102,32 @@ internal List<OperationModel> GetOperations<TGenerator>(SwaggerService service,

return new ParameterModel
{
Schema = p.ActualSchema,
Schema = p.ActualSchema,
Name = p.Name,
VariableNameLower = ConversionUtilities.ConvertToLowerCamelCase(p.Name.Replace("-", "_").Replace(".", "_")),
VariableNameLower = ConversionUtilities.ConvertToLowerCamelCase(p.Name.Replace("-", "_").Replace(".", "_")),
Kind = p.Kind,
IsRequired = p.IsRequired,
Type = resolver.Resolve(p.ActualParameterSchema, !p.IsRequired && p.IsNullable, p.Name),
Type = ResolveParameterType(p, resolver),
IsLast = operation.Parameters.LastOrDefault() == p,
Description = ConversionUtilities.TrimWhiteSpaces(p.Description)
};
}).ToList(),
}).ToList(),
};
}).ToList();
return operations;
}

private string ResolveParameterType<TGenerator>(SwaggerParameter parameter, TypeResolverBase<TGenerator> resolver)
where TGenerator : TypeGeneratorBase
{
var schema = parameter.ActualParameterSchema;

if (parameter.CollectionFormat == SwaggerParameterCollectionFormat.Multi)
schema = new JsonSchema4 { Type = JsonObjectType.Array, Item = schema };

return resolver.Resolve(schema, !parameter.IsRequired && parameter.IsNullable, parameter.Name);
}

internal SwaggerResponse GetSuccessResponse(SwaggerOperation operation)
{
if (operation.Responses.Any(r => r.Key == "200"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal override string RenderFile(string clientCode)

Clients = Settings.GenerateClientClasses ? clientCode : string.Empty,
Types = GenerateDtoTypes(),
ExtensionCode = Settings.TypeScriptGeneratorSettings.TransformedExtensionCode,
ExtensionCode = Settings.TypeScriptGeneratorSettings.TransformedExtensionCode,

HasModuleName = !string.IsNullOrEmpty(Settings.ModuleName),
ModuleName = Settings.ModuleName
Expand All @@ -82,11 +82,11 @@ internal override string RenderClientCode(string controllerName, IEnumerable<Ope
var template = Settings.CreateTemplate();
template.Initialize(new
{
Class = Settings.ClassName.Replace("{controller}", ConversionUtilities.ConvertToUpperCamelCase(controllerName)),
Class = GetClassName(controllerName),

HasOperations = operations.Any(),
Operations = operations,
UsesKnockout = Settings.TypeScriptGeneratorSettings.TypeStyle == TypeScriptTypeStyle.KnockoutClass,
UsesKnockout = Settings.TypeScriptGeneratorSettings.TypeStyle == TypeScriptTypeStyle.KnockoutClass,

GenerateClientInterfaces = Settings.GenerateClientInterfaces,
BaseUrl = _service.BaseUrl,
Expand All @@ -99,6 +99,19 @@ internal override string RenderClientCode(string controllerName, IEnumerable<Ope
return template.Render();
}

private string GetClassName(string controllerName)
{
var className = Settings.ClassName.Replace("{controller}",
ConversionUtilities.ConvertToUpperCamelCase(controllerName));

if (Settings.TypeScriptGeneratorSettings.ExtendedClasses != null &&
Settings.TypeScriptGeneratorSettings.ExtendedClasses.Contains(className))
{
className = className + "Base";
}
return className;
}

private string GenerateDtoTypes()
{
var code = Settings.GenerateDtoTypes ? _resolver.GenerateTypes() : string.Empty;
Expand Down
8 changes: 4 additions & 4 deletions src/NSwag.CodeGeneration/NSwag.CodeGeneration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NJsonSchema, Version=2.22.5984.16716, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.2.22.5984.16716\lib\portable45-net45+win8+wp8+wpa81\NJsonSchema.dll</HintPath>
<Reference Include="NJsonSchema, Version=2.23.5984.32960, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.2.23.5984.32960\lib\portable45-net45+win8+wp8+wpa81\NJsonSchema.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NJsonSchema.CodeGeneration, Version=2.22.5984.16718, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.CodeGeneration.2.22.5984.16718\lib\portable45-net45+win8+wp8+wpa81\NJsonSchema.CodeGeneration.dll</HintPath>
<Reference Include="NJsonSchema.CodeGeneration, Version=2.23.5984.32961, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.CodeGeneration.2.23.5984.32961\lib\portable45-net45+win8+wp8+wpa81\NJsonSchema.CodeGeneration.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.CodeGeneration/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
[assembly: AssemblyCompany("Rico Suter")]
[assembly: AssemblyProduct("NSwag.CodeGeneration")]
[assembly: AssemblyCopyright("Copyright © Rico Suter, 2015")]
[assembly: AssemblyVersion("2.31.*")]
[assembly: AssemblyVersion("2.32.*")]
Loading

0 comments on commit 96dbf1b

Please sign in to comment.