Skip to content

Commit

Permalink
fixed default value generator for nested generic types (#1511)
Browse files Browse the repository at this point in the history
  • Loading branch information
aboryczko committed May 15, 2022
1 parent c66ffc8 commit 86c6cfa
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
Expand Up @@ -127,5 +127,69 @@ public async Task When_generating_CSharp_code_then_default_value_with_decimal_ge
Assert.DoesNotContain("SomeOptionalProperty { get; set; } = D;", code);
Assert.Contains("double SomeOptionalProperty { get; set; } = 123.456D;", code);
}

[Fact]
public async Task When_generating_CSharp_code_then_default_value_of_dictionary_with_array_values_generates_expected_expression()
{
// Arrange
var document = await JsonSchema.FromJsonAsync(@"{
""type"": ""object"",
""required"": [""requiredDictionary""],
""properties"": {
""requiredDictionary"": {
""type"": ""object"",
""additionalProperties"": {
""type"": ""array"",
""items"": {
""type"": ""string""
}
}
}
}
}");

// Act
var settings = new CSharpGeneratorSettings();
settings.GenerateDefaultValues = true;

var generator = new CSharpGenerator(document, settings);
var code = generator.GenerateFile();

// Assert
Assert.DoesNotContain("System.Collections.Generic.IDictionary<string, System.Collections.Generic.ICollection<string>> RequiredDictionary { get; set; } = new System.Collections.Generic.Dictionary<string, System.Collections.ObjectModel.Collection<string>>();", code);
Assert.Contains("System.Collections.Generic.IDictionary<string, System.Collections.Generic.ICollection<string>> RequiredDictionary { get; set; } = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.ICollection<string>>();", code);
}

[Fact]
public async Task When_generating_CSharp_code_then_default_value_of_array_of_arrays_generates_expected_expression()
{
// Arrange
var document = await JsonSchema.FromJsonAsync(@"{
""type"": ""object"",
""required"": [""requiredList""],
""properties"": {
""requiredList"": {
""type"": ""array"",
""items"": {
""type"": ""array"",
""items"": {
""type"": ""string""
}
}
}
}
}");

// Act
var settings = new CSharpGeneratorSettings();
settings.GenerateDefaultValues = true;

var generator = new CSharpGenerator(document, settings);
var code = generator.GenerateFile();

// Assert
Assert.DoesNotContain("System.Collections.Generic.ICollection<System.Collections.Generic.ICollection<string>> RequiredList { get; set; } = new System.Collections.ObjectModel.Collection<System.Collections.ObjectModel.Collection<string>>();", code);
Assert.Contains("System.Collections.Generic.ICollection<System.Collections.Generic.ICollection<string>> RequiredList { get; set; } = new System.Collections.ObjectModel.Collection<System.Collections.Generic.ICollection<string>>();", code);
}
}
}
8 changes: 4 additions & 4 deletions src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs
Expand Up @@ -66,12 +66,12 @@ public override string GetDefaultValue(JsonSchema schema, bool allowsNull, strin
if (schema.Type.IsArray() ||
schema.Type.IsObject())
{
targetType = !string.IsNullOrEmpty(_settings.DictionaryInstanceType)
? targetType.Replace(_settings.DictionaryType + "<", _settings.DictionaryInstanceType + "<")
targetType = !string.IsNullOrEmpty(_settings.DictionaryInstanceType) && targetType.StartsWith(_settings.DictionaryType + "<")
? _settings.DictionaryInstanceType + targetType.Substring(_settings.DictionaryType.Length)
: targetType;

targetType = !string.IsNullOrEmpty(_settings.ArrayInstanceType)
? targetType.Replace(_settings.ArrayType + "<", _settings.ArrayInstanceType + "<")
targetType = !string.IsNullOrEmpty(_settings.ArrayInstanceType) && targetType.StartsWith(_settings.ArrayType + "<")
? _settings.ArrayInstanceType + targetType.Substring(_settings.ArrayType.Length)
: targetType;

return $"new {targetType}()";
Expand Down

0 comments on commit 86c6cfa

Please sign in to comment.