[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.
This commit is contained in:
craffael 2017-11-27 09:46:09 +01:00 committed by William Cheng
parent 1b90a05754
commit fc5ec6271a
9 changed files with 195 additions and 8 deletions

View File

@ -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<String, String> nodaTimeTypesMappings() {
return ImmutableMap.of(
"time", "LocalTime?",
"date", "ZonedDateTime?",
"date", "LocalDate?",
"datetime", "ZonedDateTime?");
}
private static Set<String> nodaTimePrimitiveTypes() {
return ImmutableSet.of("LocalTime?", "ZonedDateTime?");
return ImmutableSet.of("LocalTime?", "LocalDate?","ZonedDateTime?");
}
private class DependencyInfo {

View File

@ -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
{
/// <summary>
/// (De)serializes a <see cref="NodaTime.LocalDate"/> to a string using
/// the <a href="https://xml2rfc.tools.ietf.org/public/rfc/html/rfc3339.html#anchor14">RFC3339</a>
/// <code>full-date</code> format.
/// </summary>
public class LocalDateConverter : JavaScriptPrimitiveConverter, IApplicationStartup
{
public override IEnumerable<Type> 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;
}
}
}

View File

@ -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;

View File

@ -15,9 +15,9 @@ namespace IO.Swagger.v2.Modules
/// </summary>
public enum FindPetsByStatusStatusEnum
{
available,
pending,
sold
available = 1,
pending = 2,
sold = 3
};

View File

@ -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
{
/// <summary>
/// (De)serializes a <see cref="NodaTime.LocalDate"/> to a string using
/// the <a href="https://xml2rfc.tools.ietf.org/public/rfc/html/rfc3339.html#anchor14">RFC3339</a>
/// <code>full-date</code> format.
/// </summary>
public class LocalDateConverter : JavaScriptPrimitiveConverter, IApplicationStartup
{
public override IEnumerable<Type> 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;
}
}
}

View File

@ -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;

View File

@ -14,9 +14,9 @@ namespace IO.Swagger.v2.Modules
/// </summary>
public enum FindPetsByStatusStatusEnum
{
available,
pending,
sold
available = 1,
pending = 2,
sold = 3
};

View File

@ -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
{
/// <summary>
/// (De)serializes a <see cref="NodaTime.LocalDate"/> to a string using
/// the <a href="https://xml2rfc.tools.ietf.org/public/rfc/html/rfc3339.html#anchor14">RFC3339</a>
/// <code>full-date</code> format.
/// </summary>
public class LocalDateConverter : JavaScriptPrimitiveConverter, IApplicationStartup
{
public override IEnumerable<Type> 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;
}
}
}

View File

@ -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;