Skip to content

Commit

Permalink
Support duration as of JSON Schema 2019-09 (RicoSuter#1478)
Browse files Browse the repository at this point in the history
This should problably deperecate the time-span format specifier,
but currently duration and time-span just coexist. Duration is
the preferred output, as time-span was never formalized.
  • Loading branch information
simonthum authored Jan 27, 2022
1 parent ecee450 commit acb54ca
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private string ResolveString(JsonSchema schema, bool isNullable, string typeName
return isNullable && Settings.TimeType?.ToLowerInvariant() != "string" ? Settings.TimeType + "?" : Settings.TimeType;
}

if (schema.Format == JsonFormatStrings.TimeSpan)
if (schema.Format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return isNullable && Settings.TimeSpanType?.ToLowerInvariant() != "string" ? Settings.TimeSpanType + "?" : Settings.TimeSpanType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private static string GetStringToDateTime(DataConversionParameters parameters, J

case TypeScriptDateTimeType.MomentJS:
case TypeScriptDateTimeType.OffsetMomentJS:
if (typeSchema.Format == JsonFormatStrings.TimeSpan)
if (typeSchema.Format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return "moment.duration";
}
Expand All @@ -119,7 +119,7 @@ private static string GetStringToDateTime(DataConversionParameters parameters, J
return "";

case TypeScriptDateTimeType.Luxon:
if (typeSchema.Format == JsonFormatStrings.TimeSpan)
if (typeSchema.Format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return "Duration.fromISO";
}
Expand Down Expand Up @@ -163,7 +163,7 @@ private static string GetDateTimeToString(DataConversionParameters parameters, J

case TypeScriptDateTimeType.MomentJS:
case TypeScriptDateTimeType.OffsetMomentJS:
if (typeSchema.Format == JsonFormatStrings.TimeSpan)
if (typeSchema.Format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return "format('d.hh:mm:ss.SS', { trim: false })";
}
Expand All @@ -181,7 +181,7 @@ private static string GetDateTimeToString(DataConversionParameters parameters, J
return "toString()";

case TypeScriptDateTimeType.DayJS:
if (typeSchema.Format == JsonFormatStrings.TimeSpan)
if (typeSchema.Format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return "format('d.hh:mm:ss.SSS')";
}
Expand All @@ -208,7 +208,7 @@ private static bool IsDateTime(string format, TypeScriptDateTimeType type)
return false;
}

if (format == JsonFormatStrings.TimeSpan)
if (format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return false;
}
Expand All @@ -227,7 +227,7 @@ private static bool IsDateTime(string format, TypeScriptDateTimeType type)
return true;
}

if (format == JsonFormatStrings.TimeSpan)
if (format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return true;
}
Expand All @@ -244,7 +244,7 @@ private static bool IsDateTime(string format, TypeScriptDateTimeType type)
return true;
}

if (format == JsonFormatStrings.TimeSpan)
if (format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private string ResolveString(JsonSchema schema, string typeNameHint)
return "string";
}

if (schema.Format == JsonFormatStrings.TimeSpan)
if (schema.Format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return "string";
}
Expand All @@ -236,7 +236,7 @@ private string ResolveString(JsonSchema schema, string typeNameHint)
return "moment.Moment";
}

if (schema.Format == JsonFormatStrings.TimeSpan)
if (schema.Format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return "moment.Duration";
}
Expand All @@ -258,7 +258,7 @@ private string ResolveString(JsonSchema schema, string typeNameHint)
return "DateTime";
}

if (schema.Format == JsonFormatStrings.TimeSpan)
if (schema.Format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return "Duration";
}
Expand All @@ -280,7 +280,7 @@ private string ResolveString(JsonSchema schema, string typeNameHint)
return "dayjs.Dayjs";
}

if (schema.Format == JsonFormatStrings.TimeSpan)
if (schema.Format is JsonFormatStrings.Duration or JsonFormatStrings.TimeSpan)
{
return "dayjs.Dayjs";
}
Expand Down
1 change: 1 addition & 0 deletions src/NJsonSchema.CodeGeneration/ValueGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public abstract class ValueGeneratorBase
JsonFormatStrings.Date,
JsonFormatStrings.DateTime,
JsonFormatStrings.Time,
JsonFormatStrings.Duration,
JsonFormatStrings.TimeSpan,
JsonFormatStrings.Uri,
JsonFormatStrings.Guid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public async Task When_property_is_timespan_then_schema_type_is_string()

//// Assert
Assert.Equal(JsonObjectType.String, schema.Properties["TimeSpan"].Type);
Assert.Equal(JsonFormatStrings.TimeSpan, schema.Properties["TimeSpan"].Format);
Assert.Equal(JsonFormatStrings.Duration, schema.Properties["TimeSpan"].Format);
}

[Fact]
Expand Down Expand Up @@ -155,7 +155,7 @@ public async Task When_property_is_duration_then_schema_type_is_string()

//// Assert
Assert.Equal(JsonObjectType.String, schema.Properties["Duration"].Type);
Assert.Equal(JsonFormatStrings.TimeSpan, schema.Properties["Duration"].Format);
Assert.Equal(JsonFormatStrings.Duration, schema.Properties["Duration"].Format);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void PrimitiveProperties()
Assert.Equal(JsonFormatStrings.DateTime, schema.Properties["datetime"].Format);

Assert.Equal(JsonObjectType.String, schema.Properties["timespan"].Type);
Assert.Equal(JsonFormatStrings.TimeSpan, schema.Properties["timespan"].Format);
Assert.Equal(JsonFormatStrings.Duration, schema.Properties["timespan"].Format);
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema/Generation/DefaultReflectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public virtual JsonTypeDescription GetDescription(ContextualType contextualType,
if (type == typeof(TimeSpan) ||
type.FullName == "NodaTime.Duration")
{
return JsonTypeDescription.Create(contextualType, JsonObjectType.String, false, JsonFormatStrings.TimeSpan);
return JsonTypeDescription.Create(contextualType, JsonObjectType.String, false, JsonFormatStrings.Duration);
}

if (type.FullName == "NodaTime.LocalDate" ||
Expand Down
4 changes: 2 additions & 2 deletions src/NJsonSchema/Generation/SampleJsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private void GenerateWithoutReference(JToken token, JsonSchema schema, JsonSchem

case JTokenType.TimeSpan:
schema.Type = JsonObjectType.String;
schema.Format = JsonFormatStrings.TimeSpan;
schema.Format = JsonFormatStrings.Duration;
break;

case JTokenType.Guid:
Expand All @@ -157,7 +157,7 @@ private void GenerateWithoutReference(JToken token, JsonSchema schema, JsonSchem

if (schema.Type == JsonObjectType.String && Regex.IsMatch(token.Value<string>(), "^[0-9][0-9]:[0-9][0-9](:[0-9][0-9])?$"))
{
schema.Format = JsonFormatStrings.TimeSpan;
schema.Format = JsonFormatStrings.Duration;
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/NJsonSchema/JsonFormatStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ public static class JsonFormatStrings
/// <summary>Format for a <see cref="System.DateTime"/>. </summary>
public const string DateTime = "date-time";

/// <summary>Format for a <see cref="TimeSpan"/>. </summary>
/// <summary>Non-standard Format for a duration (time span)<see cref="TimeSpan"/>. </summary>
public const string TimeSpan = "time-span";

/// <summary>Format for a duration (time span) as of 2019-09 <see cref="TimeSpan"/>. </summary>
public const string Duration = "duration";

/// <summary>Format for an email. </summary>
public const string Email = "email";

Expand Down

0 comments on commit acb54ca

Please sign in to comment.