diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/DefaultPropertyTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/DefaultPropertyTests.cs index 3368ed239..d1eb0b828 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/DefaultPropertyTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/DefaultPropertyTests.cs @@ -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> RequiredDictionary { get; set; } = new System.Collections.Generic.Dictionary>();", code); + Assert.Contains("System.Collections.Generic.IDictionary> RequiredDictionary { get; set; } = new System.Collections.Generic.Dictionary>();", 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> RequiredList { get; set; } = new System.Collections.ObjectModel.Collection>();", code); + Assert.Contains("System.Collections.Generic.ICollection> RequiredList { get; set; } = new System.Collections.ObjectModel.Collection>();", code); + } } } diff --git a/src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs b/src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs index bfd3df3b0..ae4dc542d 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/CSharpValueGenerator.cs @@ -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}()";