Skip to content

Commit

Permalink
Merge pull request #920 from WISNI4/patch-2
Browse files Browse the repository at this point in the history
Update CSharpValueGenerator.cs
  • Loading branch information
RicoSuter authored Mar 5, 2019
2 parents 3b5d785 + 321f0aa commit 1d77820
Showing 1 changed file with 17 additions and 2 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);
}
}
}
}

0 comments on commit 1d77820

Please sign in to comment.