Skip to content

Commit

Permalink
Merge pull request #926 from RSuter/master
Browse files Browse the repository at this point in the history
Release v9.13.23
  • Loading branch information
RicoSuter authored Mar 14, 2019
2 parents 082b6e4 + 2f32e00 commit 52400b6
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 10 deletions.
19 changes: 17 additions & 2 deletions src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Globalization;

namespace NJsonSchema.CodeGeneration.CSharp
Expand All @@ -15,6 +16,11 @@ namespace NJsonSchema.CodeGeneration.CSharp
public class CSharpValueGenerator : ValueGeneratorBase
{
private readonly CSharpGeneratorSettings _settings;
private readonly List<string> _typesWithStringConstructor = new List<string>()
{
"System.Guid",
"System.Uri"
};

/// <summary>Initializes a new instance of the <see cref="CSharpValueGenerator" /> class.</summary>
/// <param name="settings">The settings.</param>
Expand All @@ -37,6 +43,15 @@ public override string GetDefaultValue(JsonSchema4 schema, bool allowsNull, stri
var value = base.GetDefaultValue(schema, allowsNull, targetType, typeNameHint, useSchemaDefault, typeResolver);
if (value == null)
{
if (schema.Default != null && useSchemaDefault)
{
if (_typesWithStringConstructor.Contains(targetType))
{
var stringLiteral = GetDefaultAsStringLiteral(schema);
return $"new {targetType}({stringLiteral})";
}
}

var isOptional = (schema as JsonProperty)?.IsRequired == false;

schema = schema.ActualSchema;
Expand All @@ -53,7 +68,7 @@ public override string GetDefaultValue(JsonSchema4 schema, bool allowsNull, stri
? targetType.Replace(_settings.ArrayType + "<", _settings.ArrayInstanceType + "<")
: targetType;

return "new " + targetType + "()";
return $"new {targetType}()";
}
}
}
Expand Down Expand Up @@ -105,4 +120,4 @@ protected override string GetEnumDefaultValue(JsonSchema4 schema, JsonSchema4 ac
return _settings.Namespace + "." + base.GetEnumDefaultValue(schema, actualSchema, typeNameHint, typeResolver);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.22</Version>
<Version>9.13.23</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.22</Version>
<Version>9.13.23</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if ({{ Value }}) {
{{ Variable }} = {};
for (let key in {{ Value }}) {
if ({{ Value }}.hasOwnProperty(key))
{% if IsDictionaryValueNewableObjec -%}
{% if IsDictionaryValueNewableObject -%}
{{ Variable }}[key] = {{ Value }}[key] ? {{ Value }}[key].toJSON() : <any>{{ NullValue }};
{% elseif IsDictionaryValueDate -%}
{{ Variable }}[key] = {{ Value }}[key] ? {% if UseJsDate %}formatDate({{ Value }}[key]){% else %}{{ Value }}[key].format('YYYY-MM-DD'){% endif %} : <any>{{ NullValue }};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.22</Version>
<Version>9.13.23</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
17 changes: 17 additions & 0 deletions src/NJsonSchema.Tests/Validation/FormatDateTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,22 @@ public void When_format_date_time_with_iso8601_with_timezone_then_validation_suc
//// Assert
Assert.Empty(errors);
}

[Fact]
public void When_format_date_time_with_iso8601_and_fractional_seconds_then_validation_succeeds()
{
//// Arrange
var schema = new JsonSchema4();
schema.Type = JsonObjectType.String;
schema.Format = JsonFormatStrings.DateTime;

var token = new JValue("2015-01-25T15:43:30.1234567Z");

//// Act
var errors = schema.Validate(token);

//// Assert
Assert.Empty(errors);
}
}
}
2 changes: 1 addition & 1 deletion src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net45</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.22</Version>
<Version>9.13.23</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema/NJsonSchema.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.0;netstandard2.0;net40;net45</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.22</Version>
<Version>9.13.23</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace NJsonSchema.Validation.FormatValidators
public class DateTimeFormatValidator : IFormatValidator
{
private readonly string[] _acceptableFormats = new [] {
"yyyy-MM-dd'T'HH:mm:ss.FFFK",
"yyyy-MM-dd' 'HH:mm:ss.FFFK",
"yyyy-MM-dd'T'HH:mm:ss.FFFFFFFK",
"yyyy-MM-dd' 'HH:mm:ss.FFFFFFFK",
"yyyy-MM-dd'T'HH:mm:ssK",
"yyyy-MM-dd' 'HH:mm:ssK",
"yyyy-MM-dd'T'HH:mm:ss",
Expand Down

0 comments on commit 52400b6

Please sign in to comment.