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 +}