diff --git a/src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs b/src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs index 31dd78b0b..f8858551d 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs @@ -7,6 +7,7 @@ //----------------------------------------------------------------------- using System; +using System.Collections.Generic; using System.Globalization; namespace NJsonSchema.CodeGeneration.CSharp @@ -15,6 +16,11 @@ namespace NJsonSchema.CodeGeneration.CSharp public class CSharpValueGenerator : ValueGeneratorBase { private readonly CSharpGeneratorSettings _settings; + private readonly List _typesWithStringConstructor = new List() + { + "System.Guid", + "System.Uri" + }; /// Initializes a new instance of the class. /// The settings. @@ -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; @@ -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}()"; } } } @@ -105,4 +120,4 @@ protected override string GetEnumDefaultValue(JsonSchema4 schema, JsonSchema4 ac return _settings.Namespace + "." + base.GetEnumDefaultValue(schema, actualSchema, typeNameHint, typeResolver); } } -} \ No newline at end of file +} diff --git a/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj b/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj index fd9ef4adb..9a9951a52 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj +++ b/src/NJsonSchema.CodeGeneration.CSharp/NJsonSchema.CodeGeneration.CSharp.csproj @@ -2,7 +2,7 @@ netstandard1.3;netstandard2.0;net451 JSON Schema reader, generator and validator for .NET - 9.13.22 + 9.13.23 json schema validation generator .net Copyright © Rico Suter, 2018 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md diff --git a/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj b/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj index 9e2d1f865..9dbe51e2e 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj +++ b/src/NJsonSchema.CodeGeneration.TypeScript/NJsonSchema.CodeGeneration.TypeScript.csproj @@ -2,7 +2,7 @@ netstandard1.3;netstandard2.0;net451 JSON Schema reader, generator and validator for .NET - 9.13.22 + 9.13.23 json schema validation generator .net Copyright © Rico Suter, 2018 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md diff --git a/src/NJsonSchema.CodeGeneration.TypeScript/Templates/ConvertToJavaScript.liquid b/src/NJsonSchema.CodeGeneration.TypeScript/Templates/ConvertToJavaScript.liquid index 3c426c806..6f7411adc 100644 --- a/src/NJsonSchema.CodeGeneration.TypeScript/Templates/ConvertToJavaScript.liquid +++ b/src/NJsonSchema.CodeGeneration.TypeScript/Templates/ConvertToJavaScript.liquid @@ -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() : {{ NullValue }}; {% elseif IsDictionaryValueDate -%} {{ Variable }}[key] = {{ Value }}[key] ? {% if UseJsDate %}formatDate({{ Value }}[key]){% else %}{{ Value }}[key].format('YYYY-MM-DD'){% endif %} : {{ NullValue }}; diff --git a/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj b/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj index b09c328e9..527878918 100644 --- a/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj +++ b/src/NJsonSchema.CodeGeneration/NJsonSchema.CodeGeneration.csproj @@ -2,7 +2,7 @@ netstandard1.3;netstandard2.0;net451 JSON Schema reader, generator and validator for .NET - 9.13.22 + 9.13.23 json schema validation generator .net Copyright © Rico Suter, 2018 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md diff --git a/src/NJsonSchema.Tests/Validation/FormatDateTimeTests.cs b/src/NJsonSchema.Tests/Validation/FormatDateTimeTests.cs index a7769477c..2bfe7676a 100644 --- a/src/NJsonSchema.Tests/Validation/FormatDateTimeTests.cs +++ b/src/NJsonSchema.Tests/Validation/FormatDateTimeTests.cs @@ -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); + } } } \ No newline at end of file diff --git a/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj b/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj index aee1549cb..2b6af2e84 100644 --- a/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj +++ b/src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj @@ -2,7 +2,7 @@ netstandard1.3;netstandard2.0;net45 JSON Schema reader, generator and validator for .NET - 9.13.22 + 9.13.23 json schema validation generator .net Copyright © Rico Suter, 2018 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md diff --git a/src/NJsonSchema/NJsonSchema.csproj b/src/NJsonSchema/NJsonSchema.csproj index 0865c2b7c..765bed8f8 100644 --- a/src/NJsonSchema/NJsonSchema.csproj +++ b/src/NJsonSchema/NJsonSchema.csproj @@ -2,7 +2,7 @@ netstandard1.0;netstandard2.0;net40;net45 JSON Schema reader, generator and validator for .NET - 9.13.22 + 9.13.23 json schema validation generator .net Copyright © Rico Suter, 2018 https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md diff --git a/src/NJsonSchema/Validation/FormatValidators/DateTimeFormatValidator.cs b/src/NJsonSchema/Validation/FormatValidators/DateTimeFormatValidator.cs index 85c414f2e..2324700a2 100644 --- a/src/NJsonSchema/Validation/FormatValidators/DateTimeFormatValidator.cs +++ b/src/NJsonSchema/Validation/FormatValidators/DateTimeFormatValidator.cs @@ -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",