From 3a2cbd890482f6b3b67100dc27574a3847c3c8d6 Mon Sep 17 00:00:00 2001 From: Marcin Stefaniuk Date: Tue, 17 May 2016 12:58:50 +0200 Subject: [PATCH] Added nuget packages, handling empty return types and fixes. --- .../languages/NancyFXServerCodegen.java | 3 + .../resources/nancyfx/ApiException.mustache | 49 ++++++ .../main/resources/nancyfx/Project.mustache | 139 +++++++++++------- .../src/main/resources/nancyfx/api.mustache | 29 ++-- .../src/main/resources/nancyfx/model.mustache | 53 +------ .../nancyfx/packages.config.mustache | 12 ++ 6 files changed, 173 insertions(+), 112 deletions(-) create mode 100644 modules/swagger-codegen/src/main/resources/nancyfx/ApiException.mustache create mode 100644 modules/swagger-codegen/src/main/resources/nancyfx/packages.config.mustache diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/NancyFXServerCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/NancyFXServerCodegen.java index a6a021407a4..90104d5b05d 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/NancyFXServerCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/NancyFXServerCodegen.java @@ -91,6 +91,9 @@ public class NancyFXServerCodegen extends AbstractCSharpCodegen { apiPackage = packageName + ".Api"; modelPackage = packageName + ".Models"; + supportingFiles.add(new SupportingFile("ApiException.mustache", sourceFolder, "ApiException.cs")); + supportingFiles.add(new SupportingFile("packages.config.mustache", sourceFolder, "packages.config")); + if (optionalProjectFileFlag) { supportingFiles.add(new SupportingFile("Solution.mustache", "", packageName + ".sln")); supportingFiles.add(new SupportingFile("Project.mustache", sourceFolder, packageName + ".csproj")); diff --git a/modules/swagger-codegen/src/main/resources/nancyfx/ApiException.mustache b/modules/swagger-codegen/src/main/resources/nancyfx/ApiException.mustache new file mode 100644 index 00000000000..6bc23093a87 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/nancyfx/ApiException.mustache @@ -0,0 +1,49 @@ +using System; + +namespace {{packageName}}.Api +{ + /// + /// API Exception + /// + public class ApiException : Exception + { + /// + /// Gets or sets the error code (HTTP status code) + /// + /// The error code (HTTP status code). + public int ErrorCode { get; set; } + + /// + /// Gets or sets the error content (body json object) + /// + /// The error content (Http response body). + public {{#supportsAsync}}dynamic{{/supportsAsync}}{{^supportsAsync}}object{{/supportsAsync}} ErrorContent { get; private set; } + + /// + /// Initializes a new instance of the class. + /// + public ApiException() {} + + /// + /// Initializes a new instance of the class. + /// + /// HTTP status code. + /// Error message. + public ApiException(int errorCode, string message) : base(message) + { + this.ErrorCode = errorCode; + } + + /// + /// Initializes a new instance of the class. + /// + /// HTTP status code. + /// Error message. + /// Error content. + public ApiException(int errorCode, string message, {{#supportsAsync}}dynamic{{/supportsAsync}}{{^supportsAsync}}object{{/supportsAsync}} errorContent = null) : base(message) + { + this.ErrorCode = errorCode; + this.ErrorContent = errorContent; + } + } +} diff --git a/modules/swagger-codegen/src/main/resources/nancyfx/Project.mustache b/modules/swagger-codegen/src/main/resources/nancyfx/Project.mustache index 3a753d150f6..fb2a115d4df 100644 --- a/modules/swagger-codegen/src/main/resources/nancyfx/Project.mustache +++ b/modules/swagger-codegen/src/main/resources/nancyfx/Project.mustache @@ -1,54 +1,93 @@ - - Debug - AnyCPU - {{packageGuid}} - Library - Properties - {{packageTitle}} - {{packageTitle}} - {{^supportsUWP}} - {{targetFramework}} - {{/supportsUWP}} - {{#supportsUWP}} - UAP - 10.0.10240.0 - 10.0.10240.0 - 14 - {{/supportsUWP}} - 512 - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - + + Debug + AnyCPU + {{packageGuid}} + Library + Properties + {{packageTitle}} + {{packageTitle}} + {{^supportsUWP}} + v4.5 + {{/supportsUWP}} + {{#supportsUWP}} + UAP + 10.0.10240.0 + 10.0.10240.0 + 14 + {{/supportsUWP}} + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\..\packages\Nancy.1.3.0\lib\net40\Nancy.dll + True + + + ..\..\packages\Nancy.Authentication.Token.1.3.0\lib\net40\Nancy.Authentication.Token.dll + True + + + ..\..\packages\Nancy.Hosting.Aspnet.1.3.0\lib\net40\Nancy.Hosting.Aspnet.dll + True + + + ..\..\packages\Nancy.Metadata.Modules.1.3.0\lib\net40\Nancy.Metadata.Modules.dll + True + + + ..\..\packages\Nancy.Serialization.JsonNet.1.3.0\lib\net40\Nancy.Serialization.JsonNet.dll + True + + + ..\..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll + True + + + ..\..\packages\NodaTime.1.3.1\lib\net35-Client\NodaTime.dll + True + + + ..\..\packages\Sharpility.1.2.1\lib\net45\Sharpility.dll + True + + + ..\..\packages\System.Collections.Immutable.1.1.37\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll + True + + + + + + + + + + + + + + + + + diff --git a/modules/swagger-codegen/src/main/resources/nancyfx/api.mustache b/modules/swagger-codegen/src/main/resources/nancyfx/api.mustache index d29b750c9e5..3c4ba5309b0 100644 --- a/modules/swagger-codegen/src/main/resources/nancyfx/api.mustache +++ b/modules/swagger-codegen/src/main/resources/nancyfx/api.mustache @@ -1,6 +1,7 @@ -using System; -using System.IO; using Nancy; +using Nancy.ModelBinding; +using System.Collections.Generic; +using Sharpility.Net; using {{packageName}}.Models; namespace {{packageName}}.Api @@ -11,32 +12,28 @@ namespace {{packageName}}.Api public {{classname}}Module({{classname}}Service service) : base("") { {{#operation}} {{httpMethod}}["{{path}}"] = parameters => - { - // existence validation of obligatory parameters - {{#allParams}}{{#required}} + { {{#allParams}}{{#required}} if (parameters.{{paramName}} == null) { throw new ApiException(400, "Missing the required parameter '{{paramName}}' when calling {{operationId}}"); } - {{/required}}{{/allParams}} - {{#allParams}}{{#isBodyParam}} - {{&dataType}} {{paramName}} = Bind<{{&dataType}}>(); - {{/isBodyParam}}{{#isPathParam}}{{&dataType}} {{paramName}} = parameters.{{paramName}}; - {{/isPathParam}}{{#isHeaderParam}}{{&dataType}} {{paramName}} = parameters.{{paramName}}; - {{/isHeaderParam}}{{#isQueryParam}}{{&dataType}} {{paramName}} = parameters.{{paramName}}; - {{/isQueryParam}}{{/allParams}} - return service.{{operationId}}( + {{/required}}{{/allParams}}{{#allParams}}{{#isBodyParam}} + var {{paramName}} = this.Bind<{{&dataType}}>(); + {{/isBodyParam}}{{^isBodyParam}}{{&dataType}} {{paramName}} = parameters.{{paramName}}; + {{/isBodyParam}}{{/allParams}} + {{#returnType}}return {{/returnType}}service.{{operationId}}( {{#allParams}} {{paramName}}{{#hasMore}},{{/hasMore}} {{/allParams}} - ); + );{{^returnType}} + return new Response { ContentType = "{{produces.0.mediaType}}"};{{/returnType}} }; {{/operation}} } } - interface {{classname}}Service + public interface {{classname}}Service { {{#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}} } diff --git a/modules/swagger-codegen/src/main/resources/nancyfx/model.mustache b/modules/swagger-codegen/src/main/resources/nancyfx/model.mustache index 7df65b03b41..ab15150cad8 100644 --- a/modules/swagger-codegen/src/main/resources/nancyfx/model.mustache +++ b/modules/swagger-codegen/src/main/resources/nancyfx/model.mustache @@ -1,12 +1,8 @@ using System; -using System.Linq; +using System.Collections.Generic; using System.IO; using System.Text; -using System.Collections; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Runtime.Serialization; -using Newtonsoft.Json; +using Sharpility.Extensions; {{#models}} {{#model}} @@ -15,7 +11,7 @@ namespace {{packageName}}.Models /// /// {{description}} /// - public partial class {{classname}} : {{#parent}}{{{parent}}}, {{/parent}} IEquatable<{{classname}}> + public class {{classname}} : {{#parent}}{{{parent}}}, {{/parent}} IEquatable<{{classname}}> { /// /// Initializes a new instance of the class. @@ -61,12 +57,7 @@ namespace {{packageName}}.Models /// String presentation of the object public override string ToString() { - var sb = new StringBuilder(); - sb.Append("class {{classname}} {\n"); - {{#vars}}sb.Append(" {{name}}: ").Append({{name}}).Append("\n"); - {{/vars}} - sb.Append("}\n"); - return sb.ToString(); + return this.PropertiesToString(); } /// @@ -76,10 +67,7 @@ namespace {{packageName}}.Models /// Boolean public override bool Equals(object obj) { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != GetType()) return false; - return Equals(({{classname}})obj); + return this.EqualsByProperties(obj); } /// @@ -89,21 +77,7 @@ namespace {{packageName}}.Models /// Boolean public bool Equals({{classname}} 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}}; + return this.Equals((object) other); } /// @@ -112,21 +86,10 @@ namespace {{packageName}}.Models /// Hash code public override int GetHashCode() { - // credit: http://stackoverflow.com/a/263416/677735 - 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; - } + return this.PropertiesHash(); } #region Operators - public static bool operator ==({{classname}} left, {{classname}} right) { return Equals(left, right); @@ -136,9 +99,7 @@ namespace {{packageName}}.Models { return !Equals(left, right); } - #endregion Operators - } {{/model}} {{/models}} diff --git a/modules/swagger-codegen/src/main/resources/nancyfx/packages.config.mustache b/modules/swagger-codegen/src/main/resources/nancyfx/packages.config.mustache new file mode 100644 index 00000000000..5267cce16e1 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/nancyfx/packages.config.mustache @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file