From fc5ec6271a28a3f690736f791632ec2ac5d60d76 Mon Sep 17 00:00:00 2001 From: craffael Date: Mon, 27 Nov 2017 09:46:09 +0100 Subject: [PATCH] [NancyFx] Fix for issue #7024, date field mapped to ZonedDateTime (#7025) * add support for async routes/endpoints you can now set --optional-properties async=true to generate a nancyfx server stub that uses asynchronous programming. (cherry picked from commit 126869cb0b967e8063417e11993cf6326ce8ffd4) * add nancyfx-petstore-server-async.sh to generate sample of async nancyfx. * Rename async => asyncServer * update bin/nancyfx-petstore-server-async.sh * rename async => asyncServer in api.mustache + small bugfix * run ./bin/nancyfx-petstore-server.sh and ./bin/nancyfx-petstore-server-async.sh * remove additional new line in api.mustache + add space after if * run ./bin/nancyfx-petstore-server.sh and ./bin/nancyfx-petstore-server-async.sh * Map `date` fields to LocalDate c# type + add a JsonConverter for LocalDate. --- .../languages/NancyFXServerCodegen.java | 5 +- .../nancyfx/localDateConverter.mustache | 55 +++++++++++++++++++ .../resources/nancyfx/parameters.mustache | 7 +++ .../src/IO.Swagger/Modules/PetModule.cs | 6 +- .../IO.Swagger/Utils/LocalDateConverter.cs | 55 +++++++++++++++++++ .../src/IO.Swagger/Utils/Parameters.cs | 7 +++ .../src/IO.Swagger/Modules/PetModule.cs | 6 +- .../IO.Swagger/Utils/LocalDateConverter.cs | 55 +++++++++++++++++++ .../src/IO.Swagger/Utils/Parameters.cs | 7 +++ 9 files changed, 195 insertions(+), 8 deletions(-) create mode 100644 modules/swagger-codegen/src/main/resources/nancyfx/localDateConverter.mustache create mode 100644 samples/server/petstore/nancyfx-async/src/IO.Swagger/Utils/LocalDateConverter.cs create mode 100644 samples/server/petstore/nancyfx/src/IO.Swagger/Utils/LocalDateConverter.cs 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 70fd5dcbbeb..8acdbe5cb97 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 @@ -121,6 +121,7 @@ public class NancyFXServerCodegen extends AbstractCSharpCodegen { modelPackage = isNullOrEmpty(packageName) ? MODEL_NAMESPACE : packageName + "." + MODEL_NAMESPACE; supportingFiles.add(new SupportingFile("parameters.mustache", sourceFile("Utils"), "Parameters.cs")); + supportingFiles.add(new SupportingFile("localDateConverter.mustache", sourceFile("Utils"), "LocalDateConverter.cs")); supportingFiles.add(new SupportingFile("packages.config.mustache", sourceFolder(), "packages.config")); supportingFiles.add(new SupportingFile("nuspec.mustache", sourceFolder(), packageName + ".nuspec")); @@ -391,12 +392,12 @@ public class NancyFXServerCodegen extends AbstractCSharpCodegen { private static Map nodaTimeTypesMappings() { return ImmutableMap.of( "time", "LocalTime?", - "date", "ZonedDateTime?", + "date", "LocalDate?", "datetime", "ZonedDateTime?"); } private static Set nodaTimePrimitiveTypes() { - return ImmutableSet.of("LocalTime?", "ZonedDateTime?"); + return ImmutableSet.of("LocalTime?", "LocalDate?","ZonedDateTime?"); } private class DependencyInfo { diff --git a/modules/swagger-codegen/src/main/resources/nancyfx/localDateConverter.mustache b/modules/swagger-codegen/src/main/resources/nancyfx/localDateConverter.mustache new file mode 100644 index 00000000000..f8495d6fadd --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/nancyfx/localDateConverter.mustache @@ -0,0 +1,55 @@ +using Nancy.Bootstrapper; +using Nancy.Json; +using NodaTime; +using NodaTime.Text; +using System; +using System.Collections.Generic; + +namespace {{packageName}}.{{packageContext}}.Utils +{ + /// + /// (De)serializes a to a string using + /// the RFC3339 + /// full-date format. + /// + public class LocalDateConverter : JavaScriptPrimitiveConverter, IApplicationStartup + { + public override IEnumerable SupportedTypes + { + get + { + yield return typeof(LocalDate); + yield return typeof(LocalDate?); + } + } + + public void Initialize(IPipelines pipelines) + { + JsonSettings.PrimitiveConverters.Add(new LocalDateConverter()); + } + + + public override object Serialize(object obj, JavaScriptSerializer serializer) + { + if (obj is LocalDate) + { + LocalDate localDate = (LocalDate)obj; + return LocalDatePattern.IsoPattern.Format(localDate); + } + return null; + } + + public override object Deserialize(object primitiveValue, Type type, JavaScriptSerializer serializer) + { + if ((type == typeof(LocalDate) || type == typeof(LocalDate?)) && primitiveValue is string) + { + try + { + return LocalDatePattern.IsoPattern.Parse(primitiveValue as string).GetValueOrThrow(); + } + catch { } + } + return null; + } + } +} diff --git a/modules/swagger-codegen/src/main/resources/nancyfx/parameters.mustache b/modules/swagger-codegen/src/main/resources/nancyfx/parameters.mustache index 75486f59f46..f760dfec75f 100644 --- a/modules/swagger-codegen/src/main/resources/nancyfx/parameters.mustache +++ b/modules/swagger-codegen/src/main/resources/nancyfx/parameters.mustache @@ -163,6 +163,8 @@ namespace {{packageName}}.{{packageContext}}.Utils parsers.Put(typeof(TimeSpan?), SafeParse(TimeSpan.Parse)); parsers.Put(typeof(ZonedDateTime), SafeParse(ParseZonedDateTime)); parsers.Put(typeof(ZonedDateTime?), SafeParse(ParseZonedDateTime)); + parsers.Put(typeof(LocalDate), SafeParse(ParseLocalDate)); + parsers.Put(typeof(LocalDate?), SafeParse(ParseLocalDate)); parsers.Put(typeof(LocalTime), SafeParse(ParseLocalTime)); parsers.Put(typeof(LocalTime?), SafeParse(ParseLocalTime)); @@ -372,6 +374,11 @@ namespace {{packageName}}.{{packageContext}}.Utils return new ZonedDateTime(Instant.FromDateTimeUtc(dateTime.ToUniversalTime()), DateTimeZone.Utc); } + private static LocalDate ParseLocalDate(string value) + { + return LocalDatePattern.IsoPattern.Parse(value).Value; + } + private static LocalTime ParseLocalTime(string value) { return LocalTimePattern.ExtendedIsoPattern.Parse(value).Value; diff --git a/samples/server/petstore/nancyfx-async/src/IO.Swagger/Modules/PetModule.cs b/samples/server/petstore/nancyfx-async/src/IO.Swagger/Modules/PetModule.cs index 0d4cf8616c7..6b3e8cc1db8 100644 --- a/samples/server/petstore/nancyfx-async/src/IO.Swagger/Modules/PetModule.cs +++ b/samples/server/petstore/nancyfx-async/src/IO.Swagger/Modules/PetModule.cs @@ -15,9 +15,9 @@ namespace IO.Swagger.v2.Modules /// public enum FindPetsByStatusStatusEnum { - available, - pending, - sold + available = 1, + pending = 2, + sold = 3 }; diff --git a/samples/server/petstore/nancyfx-async/src/IO.Swagger/Utils/LocalDateConverter.cs b/samples/server/petstore/nancyfx-async/src/IO.Swagger/Utils/LocalDateConverter.cs new file mode 100644 index 00000000000..d801e962c6a --- /dev/null +++ b/samples/server/petstore/nancyfx-async/src/IO.Swagger/Utils/LocalDateConverter.cs @@ -0,0 +1,55 @@ +using Nancy.Bootstrapper; +using Nancy.Json; +using NodaTime; +using NodaTime.Text; +using System; +using System.Collections.Generic; + +namespace IO.Swagger.v2.Utils +{ + /// + /// (De)serializes a to a string using + /// the RFC3339 + /// full-date format. + /// + public class LocalDateConverter : JavaScriptPrimitiveConverter, IApplicationStartup + { + public override IEnumerable SupportedTypes + { + get + { + yield return typeof(LocalDate); + yield return typeof(LocalDate?); + } + } + + public void Initialize(IPipelines pipelines) + { + JsonSettings.PrimitiveConverters.Add(new LocalDateConverter()); + } + + + public override object Serialize(object obj, JavaScriptSerializer serializer) + { + if (obj is LocalDate) + { + LocalDate localDate = (LocalDate)obj; + return LocalDatePattern.IsoPattern.Format(localDate); + } + return null; + } + + public override object Deserialize(object primitiveValue, Type type, JavaScriptSerializer serializer) + { + if ((type == typeof(LocalDate) || type == typeof(LocalDate?)) && primitiveValue is string) + { + try + { + return LocalDatePattern.IsoPattern.Parse(primitiveValue as string).GetValueOrThrow(); + } + catch { } + } + return null; + } + } +} diff --git a/samples/server/petstore/nancyfx-async/src/IO.Swagger/Utils/Parameters.cs b/samples/server/petstore/nancyfx-async/src/IO.Swagger/Utils/Parameters.cs index 6175758fd9a..d2198945763 100644 --- a/samples/server/petstore/nancyfx-async/src/IO.Swagger/Utils/Parameters.cs +++ b/samples/server/petstore/nancyfx-async/src/IO.Swagger/Utils/Parameters.cs @@ -163,6 +163,8 @@ namespace IO.Swagger.v2.Utils parsers.Put(typeof(TimeSpan?), SafeParse(TimeSpan.Parse)); parsers.Put(typeof(ZonedDateTime), SafeParse(ParseZonedDateTime)); parsers.Put(typeof(ZonedDateTime?), SafeParse(ParseZonedDateTime)); + parsers.Put(typeof(LocalDate), SafeParse(ParseLocalDate)); + parsers.Put(typeof(LocalDate?), SafeParse(ParseLocalDate)); parsers.Put(typeof(LocalTime), SafeParse(ParseLocalTime)); parsers.Put(typeof(LocalTime?), SafeParse(ParseLocalTime)); @@ -372,6 +374,11 @@ namespace IO.Swagger.v2.Utils return new ZonedDateTime(Instant.FromDateTimeUtc(dateTime.ToUniversalTime()), DateTimeZone.Utc); } + private static LocalDate ParseLocalDate(string value) + { + return LocalDatePattern.IsoPattern.Parse(value).Value; + } + private static LocalTime ParseLocalTime(string value) { return LocalTimePattern.ExtendedIsoPattern.Parse(value).Value; diff --git a/samples/server/petstore/nancyfx/src/IO.Swagger/Modules/PetModule.cs b/samples/server/petstore/nancyfx/src/IO.Swagger/Modules/PetModule.cs index edfa0ecac4a..2e5d229cdfe 100644 --- a/samples/server/petstore/nancyfx/src/IO.Swagger/Modules/PetModule.cs +++ b/samples/server/petstore/nancyfx/src/IO.Swagger/Modules/PetModule.cs @@ -14,9 +14,9 @@ namespace IO.Swagger.v2.Modules /// public enum FindPetsByStatusStatusEnum { - available, - pending, - sold + available = 1, + pending = 2, + sold = 3 }; diff --git a/samples/server/petstore/nancyfx/src/IO.Swagger/Utils/LocalDateConverter.cs b/samples/server/petstore/nancyfx/src/IO.Swagger/Utils/LocalDateConverter.cs new file mode 100644 index 00000000000..d801e962c6a --- /dev/null +++ b/samples/server/petstore/nancyfx/src/IO.Swagger/Utils/LocalDateConverter.cs @@ -0,0 +1,55 @@ +using Nancy.Bootstrapper; +using Nancy.Json; +using NodaTime; +using NodaTime.Text; +using System; +using System.Collections.Generic; + +namespace IO.Swagger.v2.Utils +{ + /// + /// (De)serializes a to a string using + /// the RFC3339 + /// full-date format. + /// + public class LocalDateConverter : JavaScriptPrimitiveConverter, IApplicationStartup + { + public override IEnumerable SupportedTypes + { + get + { + yield return typeof(LocalDate); + yield return typeof(LocalDate?); + } + } + + public void Initialize(IPipelines pipelines) + { + JsonSettings.PrimitiveConverters.Add(new LocalDateConverter()); + } + + + public override object Serialize(object obj, JavaScriptSerializer serializer) + { + if (obj is LocalDate) + { + LocalDate localDate = (LocalDate)obj; + return LocalDatePattern.IsoPattern.Format(localDate); + } + return null; + } + + public override object Deserialize(object primitiveValue, Type type, JavaScriptSerializer serializer) + { + if ((type == typeof(LocalDate) || type == typeof(LocalDate?)) && primitiveValue is string) + { + try + { + return LocalDatePattern.IsoPattern.Parse(primitiveValue as string).GetValueOrThrow(); + } + catch { } + } + return null; + } + } +} diff --git a/samples/server/petstore/nancyfx/src/IO.Swagger/Utils/Parameters.cs b/samples/server/petstore/nancyfx/src/IO.Swagger/Utils/Parameters.cs index 6175758fd9a..d2198945763 100644 --- a/samples/server/petstore/nancyfx/src/IO.Swagger/Utils/Parameters.cs +++ b/samples/server/petstore/nancyfx/src/IO.Swagger/Utils/Parameters.cs @@ -163,6 +163,8 @@ namespace IO.Swagger.v2.Utils parsers.Put(typeof(TimeSpan?), SafeParse(TimeSpan.Parse)); parsers.Put(typeof(ZonedDateTime), SafeParse(ParseZonedDateTime)); parsers.Put(typeof(ZonedDateTime?), SafeParse(ParseZonedDateTime)); + parsers.Put(typeof(LocalDate), SafeParse(ParseLocalDate)); + parsers.Put(typeof(LocalDate?), SafeParse(ParseLocalDate)); parsers.Put(typeof(LocalTime), SafeParse(ParseLocalTime)); parsers.Put(typeof(LocalTime?), SafeParse(ParseLocalTime)); @@ -372,6 +374,11 @@ namespace IO.Swagger.v2.Utils return new ZonedDateTime(Instant.FromDateTimeUtc(dateTime.ToUniversalTime()), DateTimeZone.Utc); } + private static LocalDate ParseLocalDate(string value) + { + return LocalDatePattern.IsoPattern.Parse(value).Value; + } + private static LocalTime ParseLocalTime(string value) { return LocalTimePattern.ExtendedIsoPattern.Parse(value).Value;