forked from loafle/openapi-generator-original
* 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:
parent
1b90a05754
commit
fc5ec6271a
@ -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 {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -15,9 +15,9 @@ namespace IO.Swagger.v2.Modules
|
||||
/// </summary>
|
||||
public enum FindPetsByStatusStatusEnum
|
||||
{
|
||||
available,
|
||||
pending,
|
||||
sold
|
||||
available = 1,
|
||||
pending = 2,
|
||||
sold = 3
|
||||
};
|
||||
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -14,9 +14,9 @@ namespace IO.Swagger.v2.Modules
|
||||
/// </summary>
|
||||
public enum FindPetsByStatusStatusEnum
|
||||
{
|
||||
available,
|
||||
pending,
|
||||
sold
|
||||
available = 1,
|
||||
pending = 2,
|
||||
sold = 3
|
||||
};
|
||||
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user