From bf6797caecd84337656bd153cebfe90fdcfdf02a Mon Sep 17 00:00:00 2001 From: WISNI4 Date: Tue, 5 Mar 2019 14:23:35 +0100 Subject: [PATCH 1/2] Update CSharpValueGenerator.cs uri and guid default values --- .../CSharpValueGenerator.cs | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs b/src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs index 31dd78b0b..d6ad73d87 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,16 @@ 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) + { + var stringLiteral = GetDefaultAsStringLiteral(schema); + + if (_typesWithStringConstructor.Contains(targetType)) + { + return $"new {targetType} ({stringLiteral})"; + } + } + var isOptional = (schema as JsonProperty)?.IsRequired == false; schema = schema.ActualSchema; @@ -53,7 +69,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 +121,4 @@ protected override string GetEnumDefaultValue(JsonSchema4 schema, JsonSchema4 ac return _settings.Namespace + "." + base.GetEnumDefaultValue(schema, actualSchema, typeNameHint, typeResolver); } } -} \ No newline at end of file +} From 321f0aa3f11518c4481acd795f057ca185557dc2 Mon Sep 17 00:00:00 2001 From: Rico Suter Date: Tue, 5 Mar 2019 15:30:13 +0100 Subject: [PATCH 2/2] Update CSharpValueGenerator.cs --- .../CSharpValueGenerator.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs b/src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs index d6ad73d87..f8858551d 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs @@ -43,13 +43,12 @@ 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 (schema.Default != null && useSchemaDefault) { - var stringLiteral = GetDefaultAsStringLiteral(schema); - if (_typesWithStringConstructor.Contains(targetType)) { - return $"new {targetType} ({stringLiteral})"; + var stringLiteral = GetDefaultAsStringLiteral(schema); + return $"new {targetType}({stringLiteral})"; } }