Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public void processOpts() {
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"));

Expand Down Expand Up @@ -391,12 +392,12 @@ public boolean apply(Property property) {
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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ namespace IO.Swagger.v2.Modules
/// </summary>
public enum FindPetsByStatusStatusEnum
{
available,
pending,
sold
available = 1,
pending = 2,
sold = 3
};


Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ private static IDictionary<Type, Func<Parameter, object>> CreateParsers()
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));

Expand Down Expand Up @@ -372,6 +374,11 @@ private static ZonedDateTime ParseZonedDateTime(string value)
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ namespace IO.Swagger.v2.Modules
/// </summary>
public enum FindPetsByStatusStatusEnum
{
available,
pending,
sold
available = 1,
pending = 2,
sold = 3
};


Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ private static IDictionary<Type, Func<Parameter, object>> CreateParsers()
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));

Expand Down Expand Up @@ -372,6 +374,11 @@ private static ZonedDateTime ParseZonedDateTime(string value)
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;
Expand Down