Added nuget packages, handling empty return types and fixes.

This commit is contained in:
Marcin Stefaniuk 2016-05-17 12:58:50 +02:00
parent 8fc25d9aad
commit 3a2cbd8904
6 changed files with 173 additions and 112 deletions

View File

@ -91,6 +91,9 @@ public class NancyFXServerCodegen extends AbstractCSharpCodegen {
apiPackage = packageName + ".Api"; apiPackage = packageName + ".Api";
modelPackage = packageName + ".Models"; modelPackage = packageName + ".Models";
supportingFiles.add(new SupportingFile("ApiException.mustache", sourceFolder, "ApiException.cs"));
supportingFiles.add(new SupportingFile("packages.config.mustache", sourceFolder, "packages.config"));
if (optionalProjectFileFlag) { if (optionalProjectFileFlag) {
supportingFiles.add(new SupportingFile("Solution.mustache", "", packageName + ".sln")); supportingFiles.add(new SupportingFile("Solution.mustache", "", packageName + ".sln"));
supportingFiles.add(new SupportingFile("Project.mustache", sourceFolder, packageName + ".csproj")); supportingFiles.add(new SupportingFile("Project.mustache", sourceFolder, packageName + ".csproj"));

View File

@ -0,0 +1,49 @@
using System;
namespace {{packageName}}.Api
{
/// <summary>
/// API Exception
/// </summary>
public class ApiException : Exception
{
/// <summary>
/// Gets or sets the error code (HTTP status code)
/// </summary>
/// <value>The error code (HTTP status code).</value>
public int ErrorCode { get; set; }
/// <summary>
/// Gets or sets the error content (body json object)
/// </summary>
/// <value>The error content (Http response body).</value>
public {{#supportsAsync}}dynamic{{/supportsAsync}}{{^supportsAsync}}object{{/supportsAsync}} ErrorContent { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="ApiException"/> class.
/// </summary>
public ApiException() {}
/// <summary>
/// Initializes a new instance of the <see cref="ApiException"/> class.
/// </summary>
/// <param name="errorCode">HTTP status code.</param>
/// <param name="message">Error message.</param>
public ApiException(int errorCode, string message) : base(message)
{
this.ErrorCode = errorCode;
}
/// <summary>
/// Initializes a new instance of the <see cref="ApiException"/> class.
/// </summary>
/// <param name="errorCode">HTTP status code.</param>
/// <param name="message">Error message.</param>
/// <param name="errorContent">Error content.</param>
public ApiException(int errorCode, string message, {{#supportsAsync}}dynamic{{/supportsAsync}}{{^supportsAsync}}object{{/supportsAsync}} errorContent = null) : base(message)
{
this.ErrorCode = errorCode;
this.ErrorContent = errorContent;
}
}
}

View File

@ -9,7 +9,7 @@
<RootNamespace>{{packageTitle}}</RootNamespace> <RootNamespace>{{packageTitle}}</RootNamespace>
<AssemblyName>{{packageTitle}}</AssemblyName> <AssemblyName>{{packageTitle}}</AssemblyName>
{{^supportsUWP}} {{^supportsUWP}}
<TargetFrameworkVersion>{{targetFramework}}</TargetFrameworkVersion> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
{{/supportsUWP}} {{/supportsUWP}}
{{#supportsUWP}} {{#supportsUWP}}
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier> <TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
@ -37,18 +37,57 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="Nancy, Version=1.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<Reference Include="System.Core" /> <HintPath>..\..\packages\Nancy.1.3.0\lib\net40\Nancy.dll</HintPath>
<Reference Include="System.Xml.Linq" /> <Private>True</Private>
<Reference Include="System.Data.DataSetExtensions" /> </Reference>
<Reference Include="Microsoft.CSharp" /> <Reference Include="Nancy.Authentication.Token, Version=1.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<Reference Include="System.Data" /> <HintPath>..\..\packages\Nancy.Authentication.Token.1.3.0\lib\net40\Nancy.Authentication.Token.dll</HintPath>
<Reference Include="System.Runtime.Serialization" /> <Private>True</Private>
<Reference Include="System.Xml" /> </Reference>
<Reference Include="Nancy.Hosting.Aspnet, Version=1.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Nancy.Hosting.Aspnet.1.3.0\lib\net40\Nancy.Hosting.Aspnet.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nancy.Metadata.Modules, Version=1.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Nancy.Metadata.Modules.1.3.0\lib\net40\Nancy.Metadata.Modules.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nancy.Serialization.JsonNet, Version=1.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Nancy.Serialization.JsonNet.1.3.0\lib\net40\Nancy.Serialization.JsonNet.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NodaTime, Version=1.3.0.0, Culture=neutral, PublicKeyToken=4226afe0d9b296d1, processorArchitecture=MSIL">
<HintPath>..\..\packages\NodaTime.1.3.1\lib\net35-Client\NodaTime.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Sharpility, Version=1.2.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Sharpility.1.2.1\lib\net45\Sharpility.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Collections.Immutable, Version=1.1.37.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.Collections.Immutable.1.1.37\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System"/>
<Reference Include="System.Core"/>
<Reference Include="System.Xml.Linq"/>
<Reference Include="System.Data.DataSetExtensions"/>
<Reference Include="Microsoft.CSharp"/>
<Reference Include="System.Data"/>
<Reference Include="System.Runtime.Serialization"/>
<Reference Include="System.Xml"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="**\*.cs"/> <Compile Include="**\*.cs"/>
</ItemGroup> </ItemGroup>
<Import Project="$(MsBuildToolsPath)\Microsoft.CSharp.targets" /> <ItemGroup>
<Content Include="packages.config"/>
</ItemGroup>
<Import Project="$(MsBuildToolsPath)\Microsoft.CSharp.targets"/>
</Project> </Project>

View File

@ -1,6 +1,7 @@
using System;
using System.IO;
using Nancy; using Nancy;
using Nancy.ModelBinding;
using System.Collections.Generic;
using Sharpility.Net;
using {{packageName}}.Models; using {{packageName}}.Models;
namespace {{packageName}}.Api namespace {{packageName}}.Api
@ -11,32 +12,28 @@ namespace {{packageName}}.Api
public {{classname}}Module({{classname}}Service service) : base("") public {{classname}}Module({{classname}}Service service) : base("")
{ {{#operation}} { {{#operation}}
{{httpMethod}}["{{path}}"] = parameters => {{httpMethod}}["{{path}}"] = parameters =>
{ { {{#allParams}}{{#required}}
// existence validation of obligatory parameters
{{#allParams}}{{#required}}
if (parameters.{{paramName}} == null) { if (parameters.{{paramName}} == null) {
throw new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{operationId}}"); throw new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{operationId}}");
} }
{{/required}}{{/allParams}} {{/required}}{{/allParams}}{{#allParams}}{{#isBodyParam}}
{{#allParams}}{{#isBodyParam}} var {{paramName}} = this.Bind<{{&dataType}}>();
{{&dataType}} {{paramName}} = Bind<{{&dataType}}>(); {{/isBodyParam}}{{^isBodyParam}}{{&dataType}} {{paramName}} = parameters.{{paramName}};
{{/isBodyParam}}{{#isPathParam}}{{&dataType}} {{paramName}} = parameters.{{paramName}}; {{/isBodyParam}}{{/allParams}}
{{/isPathParam}}{{#isHeaderParam}}{{&dataType}} {{paramName}} = parameters.{{paramName}}; {{#returnType}}return {{/returnType}}service.{{operationId}}(
{{/isHeaderParam}}{{#isQueryParam}}{{&dataType}} {{paramName}} = parameters.{{paramName}};
{{/isQueryParam}}{{/allParams}}
return service.{{operationId}}(
{{#allParams}} {{#allParams}}
{{paramName}}{{#hasMore}},{{/hasMore}} {{paramName}}{{#hasMore}},{{/hasMore}}
{{/allParams}} {{/allParams}}
); );{{^returnType}}
return new Response { ContentType = "{{produces.0.mediaType}}"};{{/returnType}}
}; };
{{/operation}} {{/operation}}
} }
} }
interface {{classname}}Service public interface {{classname}}Service
{ {{#operation}} { {{#operation}}
public {{#returnType}}{{&returnType}}{{/returnType}}{{^returnType}}void{{/returnType}} {{operationId}}({{#allParams}}{{>pathParam}}{{>queryParam}}{{>bodyParam}}{{>formParam}}{{>headerParam}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); {{#returnType}}{{&returnType}}{{/returnType}}{{^returnType}}void{{/returnType}} {{operationId}}({{#allParams}}{{>pathParam}}{{>queryParam}}{{>bodyParam}}{{>formParam}}{{>headerParam}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
{{/operation}} {{/operation}}
} }

View File

@ -1,12 +1,8 @@
using System; using System;
using System.Linq; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Collections; using Sharpility.Extensions;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using Newtonsoft.Json;
{{#models}} {{#models}}
{{#model}} {{#model}}
@ -15,7 +11,7 @@ namespace {{packageName}}.Models
/// <summary> /// <summary>
/// {{description}} /// {{description}}
/// </summary> /// </summary>
public partial class {{classname}} : {{#parent}}{{{parent}}}, {{/parent}} IEquatable<{{classname}}> public class {{classname}} : {{#parent}}{{{parent}}}, {{/parent}} IEquatable<{{classname}}>
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="{{classname}}" /> class. /// Initializes a new instance of the <see cref="{{classname}}" /> class.
@ -61,12 +57,7 @@ namespace {{packageName}}.Models
/// <returns>String presentation of the object</returns> /// <returns>String presentation of the object</returns>
public override string ToString() public override string ToString()
{ {
var sb = new StringBuilder(); return this.PropertiesToString();
sb.Append("class {{classname}} {\n");
{{#vars}}sb.Append(" {{name}}: ").Append({{name}}).Append("\n");
{{/vars}}
sb.Append("}\n");
return sb.ToString();
} }
/// <summary> /// <summary>
@ -76,10 +67,7 @@ namespace {{packageName}}.Models
/// <returns>Boolean</returns> /// <returns>Boolean</returns>
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (ReferenceEquals(null, obj)) return false; return this.EqualsByProperties(obj);
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals(({{classname}})obj);
} }
/// <summary> /// <summary>
@ -89,21 +77,7 @@ namespace {{packageName}}.Models
/// <returns>Boolean</returns> /// <returns>Boolean</returns>
public bool Equals({{classname}} other) public bool Equals({{classname}} other)
{ {
return this.Equals((object) other);
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return {{#vars}}{{#isNotContainer}}
(
this.{{name}} == other.{{name}} ||
this.{{name}} != null &&
this.{{name}}.Equals(other.{{name}})
){{#hasMore}} && {{/hasMore}}{{/isNotContainer}}{{^isNotContainer}}
(
this.{{name}} == other.{{name}} ||
this.{{name}} != null &&
this.{{name}}.SequenceEqual(other.{{name}})
){{#hasMore}} && {{/hasMore}}{{/isNotContainer}}{{/vars}}{{^vars}}false{{/vars}};
} }
/// <summary> /// <summary>
@ -112,21 +86,10 @@ namespace {{packageName}}.Models
/// <returns>Hash code</returns> /// <returns>Hash code</returns>
public override int GetHashCode() public override int GetHashCode()
{ {
// credit: http://stackoverflow.com/a/263416/677735 return this.PropertiesHash();
unchecked // Overflow is fine, just wrap
{
int hash = 41;
// Suitable nullity checks etc, of course :)
{{#vars}}
if (this.{{name}} != null)
hash = hash * 59 + this.{{name}}.GetHashCode();
{{/vars}}
return hash;
}
} }
#region Operators #region Operators
public static bool operator ==({{classname}} left, {{classname}} right) public static bool operator ==({{classname}} left, {{classname}} right)
{ {
return Equals(left, right); return Equals(left, right);
@ -136,9 +99,7 @@ namespace {{packageName}}.Models
{ {
return !Equals(left, right); return !Equals(left, right);
} }
#endregion Operators #endregion Operators
} }
{{/model}} {{/model}}
{{/models}} {{/models}}

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Nancy" version="1.3.0" targetFramework="net45" />
<package id="Nancy.Authentication.Token" version="1.3.0" targetFramework="net45" />
<package id="Nancy.Hosting.Aspnet" version="1.3.0" targetFramework="net45" />
<package id="Nancy.Metadata.Modules" version="1.3.0" targetFramework="net45" />
<package id="Nancy.Serialization.JsonNet" version="1.3.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" />
<package id="NodaTime" version="1.3.1" targetFramework="net45" />
<package id="Sharpility" version="1.2.1" targetFramework="net45" />
<package id="System.Collections.Immutable" version="1.1.37" targetFramework="net45" />
</packages>