Skip to content

Commit

Permalink
Merge pull request #92 from NSwag/master
Browse files Browse the repository at this point in the history
Release v2.18
  • Loading branch information
RicoSuter committed May 6, 2016
2 parents 007edea + 17aa940 commit 43d0a8f
Show file tree
Hide file tree
Showing 22 changed files with 527 additions and 447 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.17.*")]
[assembly: AssemblyVersion("2.18.*")]
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -71,9 +70,11 @@ public void When_parameter_is_array_then_TypeScript_is_correct()
var settings = new SwaggerToTypeScriptClientGeneratorSettings { ClassName = "MyClass" };
var generator = new SwaggerToTypeScriptClientGenerator(service, settings);
var code = generator.GenerateFile();

//// Assert
Assert.IsTrue(code.Contains(@"elementId.forEach(item => { url += ""elementId="" + encodeURIComponent("""" + item) + ""&""; });"));
Assert.IsTrue(
code.Contains(
@"elementId.forEach(item => { url += ""elementId="" + encodeURIComponent("""" + item) + ""&""; });"));
}

[TestMethod]
Expand Down Expand Up @@ -137,7 +138,10 @@ public void When_parameter_is_array_then_CSharp_is_correct()
var code = generator.GenerateFile();

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

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NJsonSchema;
using NSwag.CodeGeneration.CodeGenerators.CSharp;

namespace NSwag.CodeGeneration.Tests.ClientGeneration
{
[TestClass]
public class FormParameterTests
{
[TestMethod]
public void When_form_parameters_are_defined_then_MultipartFormDataContent_is_generated()
{
//// Arrange
var service = new SwaggerService();
service.Paths["foo/bar"] = new SwaggerOperations
{
{
SwaggerOperationMethod.Post,
new SwaggerOperation
{
Parameters = new List<SwaggerParameter>
{
new SwaggerParameter
{
Name = "foo",
Kind = SwaggerParameterKind.FormData,
Type = JsonObjectType.String
},
new SwaggerParameter
{
Name = "bar",
IsRequired = true,
Kind = SwaggerParameterKind.FormData,
Type = JsonObjectType.String
}
}
}
}
};

//// Act
var generator = new SwaggerToCSharpClientGenerator(service, new SwaggerToCSharpClientGeneratorSettings
{

});
var code = generator.GenerateFile();

//// Assert
Assert.IsTrue(code.Contains("new MultipartFormDataContent"));
Assert.IsTrue(code.Contains("if (foo != null)"));
Assert.IsTrue(code.Contains("throw new ArgumentNullException(\"bar\");"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
</Choose>
<ItemGroup>
<Compile Include="ClientGeneration\ArrayParameterTests.cs" />
<Compile Include="ClientGeneration\FormParameterTests.cs" />
<Compile Include="CodeGenerationTests.cs" />
<Compile Include="CommandLine\WebApiCommandLineTests.cs" />
<Compile Include="OperationNameGenerator\MultipleClientsFromOperationIdOperationNameGeneratorTests.cs" />
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.17.*")]
// [assembly: AssemblyVersion("2.18.*")]
[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,5 @@
//-----------------------------------------------------------------------
// <copyright file="SwaggerToCSharpGenerator.cs" company="NSwag">
// <copyright file="SwaggerToCSharpClientGenerator.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</license>
Expand All @@ -15,7 +15,7 @@
namespace NSwag.CodeGeneration.CodeGenerators.CSharp
{
/// <summary>Generates the CSharp service client code. </summary>
public class SwaggerToCSharpClientGenerator : SwaggerToCSharpGenerator
public class SwaggerToCSharpClientGenerator : SwaggerToCSharpGeneratorBase
{
private readonly SwaggerService _service;

Expand Down Expand Up @@ -44,15 +44,15 @@ public SwaggerToCSharpClientGenerator(SwaggerService service, SwaggerToCSharpCli
/// <summary>Gets the language.</summary>
protected override string Language => "CSharp";

internal override CodeGeneratorBaseSettings BaseSettings => Settings;

/// <summary>Generates the file.</summary>
/// <returns>The file contents.</returns>
public override string GenerateFile()
{
return GenerateFile(_service, Resolver);
}

internal override CodeGeneratorBaseSettings BaseSettings => Settings;

internal override string RenderFile(string clientCode)
{
var template = LoadTemplate("File");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------
// <copyright file="SwaggerToCSharpGenerator.cs" company="NSwag">
// <copyright file="SwaggerToCSharpGeneratorBase.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</license>
Expand All @@ -13,11 +13,11 @@
namespace NSwag.CodeGeneration.CodeGenerators.CSharp
{
/// <summary>The CSharp generator base class.</summary>
public abstract class SwaggerToCSharpGenerator : ClientGeneratorBase
public abstract class SwaggerToCSharpGeneratorBase : ClientGeneratorBase
{
internal SwaggerToCSharpTypeResolver Resolver { get; private set; }

internal SwaggerToCSharpGenerator(SwaggerService service, CSharpGeneratorSettings settings)
internal SwaggerToCSharpGeneratorBase(SwaggerService service, CSharpGeneratorSettings settings)
{
Resolver = new SwaggerToCSharpTypeResolver(settings, service.Definitions);
}
Expand All @@ -33,7 +33,7 @@ internal override string GetExceptionType(SwaggerOperation operation)
internal override string GetResultType(SwaggerOperation operation)
{
var response = GetSuccessResponse(operation);
if (response == null || response.Schema == null)
if (response?.Schema == null)
return "Task";

return "Task<" + GetType(response.Schema, "Response") + ">";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
//-----------------------------------------------------------------------
// <copyright file="SwaggerToCSharpGeneratorSettings.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</license>
// <author>Rico Suter, [email protected]</author>
//-----------------------------------------------------------------------

using NJsonSchema.CodeGeneration.CSharp;

namespace NSwag.CodeGeneration.CodeGenerators.CSharp
{
/// <summary>Settings for the <see cref="SwaggerToCSharpGenerator"/>.</summary>
public abstract class SwaggerToCSharpGeneratorSettings : CodeGeneratorBaseSettings
{
/// <summary>Initializes a new instance of the <see cref="SwaggerToCSharpClientGeneratorSettings"/> class.</summary>
protected SwaggerToCSharpGeneratorSettings()
{
AdditionalNamespaceUsages = null;
CSharpGeneratorSettings = new CSharpGeneratorSettings
{
Namespace = "MyNamespace"
};
}

/// <summary>Gets or sets the CSharp generator settings.</summary>
public CSharpGeneratorSettings CSharpGeneratorSettings { get; set; }

/// <summary>Gets or sets the class name of the service client.</summary>
public string ClassName { get; set; }

/// <summary>Gets or sets the additional namespace usages.</summary>
public string[] AdditionalNamespaceUsages { get; set; }
}
//-----------------------------------------------------------------------
// <copyright file="SwaggerToCSharpGeneratorSettings.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</license>
// <author>Rico Suter, [email protected]</author>
//-----------------------------------------------------------------------

using NJsonSchema.CodeGeneration.CSharp;

namespace NSwag.CodeGeneration.CodeGenerators.CSharp
{
/// <summary>Settings for the <see cref="SwaggerToCSharpGeneratorBase"/>.</summary>
public abstract class SwaggerToCSharpGeneratorSettings : CodeGeneratorBaseSettings
{
/// <summary>Initializes a new instance of the <see cref="SwaggerToCSharpClientGeneratorSettings"/> class.</summary>
protected SwaggerToCSharpGeneratorSettings()
{
AdditionalNamespaceUsages = null;
CSharpGeneratorSettings = new CSharpGeneratorSettings
{
Namespace = "MyNamespace"
};
}

/// <summary>Gets or sets the CSharp generator settings.</summary>
public CSharpGeneratorSettings CSharpGeneratorSettings { get; set; }

/// <summary>Gets or sets the class name of the service client.</summary>
public string ClassName { get; set; }

/// <summary>Gets or sets the additional namespace usages.</summary>
public string[] AdditionalNamespaceUsages { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------
// <copyright file="SwaggerToCSharpGenerator.cs" company="NSwag">
// <copyright file="SwaggerToCSharpWebApiControllerGenerator.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</license>
Expand All @@ -15,7 +15,7 @@
namespace NSwag.CodeGeneration.CodeGenerators.CSharp
{
/// <summary>Generates the CSharp service client code. </summary>
public class SwaggerToCSharpWebApiControllerGenerator : SwaggerToCSharpGenerator
public class SwaggerToCSharpWebApiControllerGenerator : SwaggerToCSharpGeneratorBase
{
private readonly SwaggerService _service;

Expand Down Expand Up @@ -43,15 +43,15 @@ public SwaggerToCSharpWebApiControllerGenerator(SwaggerService service, SwaggerT
/// <summary>Gets the language.</summary>
protected override string Language => "CSharp";

internal override CodeGeneratorBaseSettings BaseSettings => Settings;

/// <summary>Generates the file.</summary>
/// <returns>The file contents.</returns>
public override string GenerateFile()
{
return GenerateFile(_service, Resolver);
}

internal override CodeGeneratorBaseSettings BaseSettings => Settings;

internal override string RenderFile(string clientCode)
{
var template = LoadTemplate("File");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,22 @@ public partial class <class> <if(hasBaseType)>: <endif><if(hasBaseClass)><baseCl
<if(operation.IsGetOrDelete)>
var response_ = await client_.<operation.HttpMethodUpper>Async(url_, cancellationToken).ConfigureAwait(false);
<else>
var response_ = await client_.<operation.HttpMethodUpper>Async(url_, new StringContent(string.Empty), cancellationToken).ConfigureAwait(false);
<if(operation.HasFormParameters)>
var content_ = new MultipartFormDataContent();
<operation.FormParameters:{parameter |
<if(parameter.IsOptional)>
if (<parameter.VariableNameLower> != null)
<else>
if (<parameter.VariableNameLower> == null)
throw new ArgumentNullException("<parameter.VariableNameLower>");
else
<endif>
content_.Add(new StringContent(<parameter.VariableNameLower>.ToString()), "<parameter.Name>");
}>
<else>
var content_ = new StringContent(string.Empty);
<endif>
var response_ = await client_.<operation.HttpMethodUpper>Async(url_, content_, cancellationToken).ConfigureAwait(false);
<endif>
ProcessResponse(client_, response_);
<endif>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ internal List<OperationModel> GetOperations<TGenerator>(SwaggerService service,
HasResultType = HasResultType(operation),
ResultDescription = GetResultDescription(operation),
ExceptionType = GetExceptionType(operation),
HasFormParameters = operation.Parameters.Any(p => p.Kind == SwaggerParameterKind.FormData),
Responses = responses,
DefaultResponse = defaultResponse,
Parameters = operation.Parameters.Select(p =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ internal class OperationModel

public IEnumerable<ParameterModel> HeaderParameters => Parameters.Where(p => p.Kind == SwaggerParameterKind.Header);

public IEnumerable<ParameterModel> FormParameters => Parameters.Where(p => p.Kind == SwaggerParameterKind.FormData);

public string Summary => ConversionUtilities.RemoveWhiteSpaces(Operation.Summary);

public bool HasSummary => !string.IsNullOrEmpty(Summary);

public bool HasDocumentation => HasSummary || HasResultDescription || Parameters.Any(p => p.HasDescription);

public bool HasFormParameters { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal sealed class AppDomainIsolation<T> : IDisposable where T : MarshalByRef
public AppDomainIsolation(string assemblyDirectory, string assemblyConfiguration)
{
if (string.IsNullOrEmpty(assemblyDirectory))
throw new ArgumentNullException("assemblyDirectory");
throw new ArgumentNullException(nameof(assemblyDirectory));

using (var transformer = new AssemblyConfigurationFileTransformer())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace NSwag.CodeGeneration.Infrastructure
{
internal class AssemblyConfigurationFileTransformer : IDisposable
{
private string _transformedConfiguationPath = null;
private string _transformedConfigurationPath = null;

public string GetConfigurationPath(string assemblyDirectory)
{
Expand All @@ -32,7 +32,7 @@ public string GetConfigurationPath(string assemblyDirectory)
RegexOptions.Singleline);

File.WriteAllText(configPath, content, Encoding.UTF8);
_transformedConfiguationPath = configPath;
_transformedConfigurationPath = configPath;
return configPath;
}

Expand All @@ -41,10 +41,10 @@ public string GetConfigurationPath(string assemblyDirectory)

public void Dispose()
{
if (_transformedConfiguationPath != null)
if (_transformedConfigurationPath != null)
{
File.Delete(_transformedConfiguationPath);
_transformedConfiguationPath = null;
File.Delete(_transformedConfigurationPath);
_transformedConfigurationPath = null;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.CodeGeneration/NSwag.CodeGeneration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<ItemGroup>
<Compile Include="CodeGenerators\ControllerGeneratorBaseSettings.cs" />
<Compile Include="CodeGenerators\CSharp\SwaggerToCSharpClientGeneratorSettings.cs" />
<Compile Include="CodeGenerators\CSharp\SwaggerToCSharpGenerator.cs" />
<Compile Include="CodeGenerators\CSharp\SwaggerToCSharpGeneratorBase.cs" />
<Compile Include="CodeGenerators\CSharp\SwaggerToCSharpGeneratorSettings.cs" />
<Compile Include="CodeGenerators\CSharp\SwaggerToCSharpTypeResolver.cs" />
<Compile Include="CodeGenerators\OperationNameGenerators\IOperationNameGenerator.cs" />
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.17.*")]
[assembly: AssemblyVersion("2.18.*")]
2 changes: 1 addition & 1 deletion src/NSwag.Core/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")]
[assembly: AssemblyCopyright("Copyright © Rico Suter, 2015")]
[assembly: AssemblyVersion("2.17.*")]
[assembly: AssemblyVersion("2.18.*")]
2 changes: 1 addition & 1 deletion src/NSwag.Demo.Client/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,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.17.*")]
// [assembly: AssemblyVersion("2.18.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading

0 comments on commit 43d0a8f

Please sign in to comment.