From 9251062ec3ece46faf9d2dd5a1fca1f203334356 Mon Sep 17 00:00:00 2001 From: AroglDarthu Date: Thu, 26 May 2022 00:36:39 +0200 Subject: [PATCH 1/6] Fix output of JsonIgnore attributes --- .../GeneralGeneratorTests.cs | 126 ------------ .../JsonIgnoreAttributesTests.cs | 179 ++++++++++++++++++ .../Models/PropertyModel.cs | 38 +++- 3 files changed, 210 insertions(+), 133 deletions(-) create mode 100644 src/NJsonSchema.CodeGeneration.CSharp.Tests/JsonIgnoreAttributesTests.cs diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs index 306dd2ed5..388796f02 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/GeneralGeneratorTests.cs @@ -739,132 +739,6 @@ public async Task When_property_is_required_then_CSharp_code_is_correct() AssertCompile(code); } - [Fact] - public async Task When_using_SystemTextJson_JsonIgnoreAttributes_are_generated_based_on_optionality() { - //// Arrange - var schema = await JsonSchema.FromJsonAsync(@"{ - ""type"": ""object"", - ""required"": [""requiredValue"",""requiredRef""], - ""properties"": { - ""requiredValue"": { ""type"": ""integer"", ""format"": ""int32"" }, - ""requiredRef"": { ""type"": ""string"" }, - ""optionalValue"": { ""type"": ""integer"", ""format"": ""int32"" }, - ""optionalRef"": { ""type"": ""string"" } - } - }"); - - var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { - JsonLibrary = CSharpJsonLibrary.SystemTextJson - }); - - static string Normalized(string str) => - Regex.Replace(str, @"\s+", " "); - - //// Act - var code = generator.GenerateFile("MyClass"); - - /// Assert - Assert.Contains( - Normalized(@"public int OptionalValue {"), - Normalized(code) - ); - - Assert.Contains( - Normalized(@" - [System.Text.Json.Serialization.JsonPropertyName(""requiredValue"")] - [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)] - "), - Normalized(code) - ); - - Assert.Contains( - Normalized(@" - [System.Text.Json.Serialization.JsonPropertyName(""requiredRef"")] - [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)] - "), - Normalized(code) - ); - - Assert.Contains( - Normalized(@" - [System.Text.Json.Serialization.JsonPropertyName(""optionalValue"")] - [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)] - "), - Normalized(code) - ); - - Assert.Contains( - Normalized(@" - [System.Text.Json.Serialization.JsonPropertyName(""optionalRef"")] - [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)] - "), - Normalized(code) - ); - } - - [Fact] - public async Task When_using_SystemTextJson_and_RequiredPropertiesMustBeDefined_is_false_JsonIgnoreAttributes_are_not_generated_for_required_properties() { - //// Arrange - var schema = await JsonSchema.FromJsonAsync(@"{ - ""type"": ""object"", - ""required"": [""required""], - ""properties"": { - ""required"": { ""type"": ""string"" } - } - }"); - - var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { - JsonLibrary = CSharpJsonLibrary.SystemTextJson, - RequiredPropertiesMustBeDefined = false - }); - - static string Normalized(string str) => - Regex.Replace(str, @"\s+", " "); - - //// Act - var code = generator.GenerateFile("MyClass"); - - /// Assert - Assert.DoesNotContain( - Normalized(@" - [System.Text.Json.Serialization.JsonIgnore - "), - Normalized(code) - ); - } - - [Fact] - public async Task When_using_SystemTextJson_and_RequiredPropertiesMustBeDefined_is_false_JsonIgnoreAttributes_are_still_generated_for_optional_properties() { - //// Arrange - var schema = await JsonSchema.FromJsonAsync(@"{ - ""type"": ""object"", - ""required"": [], - ""properties"": { - ""optional"": { ""type"": ""string"" } - } - }"); - - var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { - JsonLibrary = CSharpJsonLibrary.SystemTextJson, - RequiredPropertiesMustBeDefined = false - }); - - static string Normalized(string str) => - Regex.Replace(str, @"\s+", " "); - - //// Act - var code = generator.GenerateFile("MyClass"); - - /// Assert - Assert.Contains( - Normalized(@" - [System.Text.Json.Serialization.JsonPropertyName(""optional"")] - [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)] - "), - Normalized(code) - ); - } - [Fact] public void When_array_property_is_required_or_not_then_the_code_has_correct_initializer() { diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/JsonIgnoreAttributesTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/JsonIgnoreAttributesTests.cs new file mode 100644 index 000000000..c07f34e0b --- /dev/null +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/JsonIgnoreAttributesTests.cs @@ -0,0 +1,179 @@ +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using Xunit; + +namespace NJsonSchema.CodeGeneration.CSharp.Tests +{ + public class JsonIgnoreAttributesTests + { + [Fact] + public async Task When_using_SystemTextJson_JsonIgnoreAttributes_are_generated_based_on_optionality() + { + //// Arrange + var schema = await JsonSchema.FromJsonAsync(@"{ + ""type"": ""object"", + ""required"": [""requiredValue"",""requiredNullableValue"",""requiredRef""], + ""properties"": { + ""requiredValue"": { ""type"": ""integer"", ""format"": ""int32"" }, + ""requiredNullableValue"": { ""type"": [""integer"", ""null""], ""format"": ""int32"" }, + ""requiredRef"": { ""type"": ""string"" }, + ""optionalValue"": { ""type"": ""integer"", ""format"": ""int32"" }, + ""optionalNullableValue"": { ""type"": [""integer"", ""null""], ""format"": ""int32"" }, + ""optionalRef"": { ""type"": ""string"" } + } + }"); + + var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings + { + JsonLibrary = CSharpJsonLibrary.SystemTextJson, + //GenerateNullableReferenceTypes = true, + //GenerateOptionalPropertiesAsNullable = true, + }); + + static string Normalized(string str) => + Regex.Replace(str, @"\s+", " "); + + //// Act + var code = generator.GenerateFile("MyClass"); + + /// Assert + Assert.Contains( + Normalized(@"public int OptionalValue {"), + Normalized(code) + ); + + Assert.Contains( + Normalized(@" + [System.Text.Json.Serialization.JsonPropertyName(""requiredValue"")] + [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)] + "), + Normalized(code) + ); + + Assert.Contains( + Normalized(@" + [System.Text.Json.Serialization.JsonPropertyName(""requiredNullableValue"")] + [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)] + "), + Normalized(code) + ); + + Assert.Contains( + Normalized(@" + [System.Text.Json.Serialization.JsonPropertyName(""requiredRef"")] + [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)] + "), + Normalized(code) + ); + + Assert.Contains( + Normalized(@" + [System.Text.Json.Serialization.JsonPropertyName(""optionalValue"")] + [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)] + "), + Normalized(code) + ); + + Assert.Contains( + Normalized(@" + [System.Text.Json.Serialization.JsonPropertyName(""optionalNullableValue"")] + [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)] + "), + Normalized(code) + ); + + Assert.Contains( + Normalized(@" + [System.Text.Json.Serialization.JsonPropertyName(""optionalRef"")] + [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)] + "), + Normalized(code) + ); + } + + [Fact] + public async Task When_using_SystemTextJson_and_RequiredPropertiesMustBeDefined_is_false_JsonIgnoreAttributes_are_not_generated_for_required_properties() + { + //// Arrange + var schema = await JsonSchema.FromJsonAsync(@"{ + ""type"": ""object"", + ""required"": [""required""], + ""properties"": { + ""required"": { ""type"": ""string"" } + } + }"); + + var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings + { + JsonLibrary = CSharpJsonLibrary.SystemTextJson, + RequiredPropertiesMustBeDefined = false + }); + + static string Normalized(string str) => + Regex.Replace(str, @"\s+", " "); + + //// Act + var code = generator.GenerateFile("MyClass"); + + /// Assert + Assert.DoesNotContain( + Normalized(@" + [System.Text.Json.Serialization.JsonIgnore + "), + Normalized(code) + ); + } + + [Fact] + public async Task When_using_SystemTextJson_and_RequiredPropertiesMustBeDefined_is_false_JsonIgnoreAttributes_are_still_generated_for_optional_properties() + { + //// Arrange + var schema = await JsonSchema.FromJsonAsync(@"{ + ""type"": ""object"", + ""required"": [], + ""properties"": { + ""optionalRef"": { ""type"": ""string"" }, + ""optionalValue"": { ""type"": ""integer"", ""format"": ""int32"" }, + ""optionalNullableValue"": { ""type"": [""integer"", ""null""], ""format"": ""int32"" } + } + }"); + + var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings + { + JsonLibrary = CSharpJsonLibrary.SystemTextJson, + RequiredPropertiesMustBeDefined = false + }); + + static string Normalized(string str) => + Regex.Replace(str, @"\s+", " "); + + //// Act + var code = generator.GenerateFile("MyClass"); + + /// Assert + Assert.Contains( + Normalized(@" + [System.Text.Json.Serialization.JsonPropertyName(""optionalRef"")] + [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)] + "), + Normalized(code) + ); + + Assert.Contains( + Normalized(@" + [System.Text.Json.Serialization.JsonPropertyName(""optionalValue"")] + [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)] + "), + Normalized(code) + ); + + Assert.Contains( + Normalized(@" + [System.Text.Json.Serialization.JsonPropertyName(""optionalNullableValue"")] + [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)] + "), + Normalized(code) + ); + } + } +} diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs index 185392862..c4c738d58 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs @@ -7,6 +7,7 @@ //----------------------------------------------------------------------- using System.Globalization; +using System.Linq; using NJsonSchema.CodeGeneration.Models; namespace NJsonSchema.CodeGeneration.CSharp.Models @@ -64,17 +65,40 @@ public PropertyModel( (_property.ActualTypeSchema.IsArray && _settings.GenerateImmutableArrayProperties) || (_property.ActualTypeSchema.IsDictionary && _settings.GenerateImmutableDictionaryProperties) )) == false; - + /// Indicates whether or not this property has a . public bool HasJsonIgnoreCondition => JsonIgnoreCondition != null; /// Returns the System.Text.Json.Serialization.JsonIgnoreCondition value to be applied to the property. - public string JsonIgnoreCondition => _property switch { - { IsRequired: false } => "System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull", - { IsRequired: true } when _settings.RequiredPropertiesMustBeDefined => "System.Text.Json.Serialization.JsonIgnoreCondition.Never", - _ => null - }; - + public string JsonIgnoreCondition + { + get + { + var isValueType = IsValueType(); + return _property switch + { + { IsRequired: true } when _settings.RequiredPropertiesMustBeDefined => "System.Text.Json.Serialization.JsonIgnoreCondition.Never", + { IsRequired: false } when isValueType && IsNullable => "System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull", + { IsRequired: false } when isValueType && !IsNullable => "System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault", + { IsRequired: false } when !isValueType => "System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull", + _ => null + }; + + bool IsValueType() + { + var dateFormats = new string[] { "date", "date-time" }; + return _property.ActualTypeSchema.Type switch + { + JsonObjectType.Boolean => true, + JsonObjectType.Integer => true, + JsonObjectType.Number => true, + JsonObjectType.String => dateFormats.Contains(_property.Format), + _ => false, + }; + } + } + } + /// Gets the json property required. public string JsonPropertyRequiredCode { From 310ca137f8f31e4360ce621df5109f0f013f1e07 Mon Sep 17 00:00:00 2001 From: AroglDarthu Date: Thu, 26 May 2022 01:09:09 +0200 Subject: [PATCH 2/6] Recognize Guid as value type --- src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs index c4c738d58..36b9cac94 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs @@ -86,13 +86,13 @@ public string JsonIgnoreCondition bool IsValueType() { - var dateFormats = new string[] { "date", "date-time" }; + var valueFormats = new string[] { "date", "date-time", "uuid" }; return _property.ActualTypeSchema.Type switch { JsonObjectType.Boolean => true, JsonObjectType.Integer => true, JsonObjectType.Number => true, - JsonObjectType.String => dateFormats.Contains(_property.Format), + JsonObjectType.String => valueFormats.Contains(_property.Format), _ => false, }; } From 80e1841fa60c2de7b9c163d71dbdc35ab654736f Mon Sep 17 00:00:00 2001 From: AroglDarthu Date: Thu, 26 May 2022 01:09:48 +0200 Subject: [PATCH 3/6] Rename variable --- src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs index 36b9cac94..031b293ae 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs @@ -86,13 +86,13 @@ public string JsonIgnoreCondition bool IsValueType() { - var valueFormats = new string[] { "date", "date-time", "uuid" }; + var valueTypeFormats = new string[] { "date", "date-time", "uuid" }; return _property.ActualTypeSchema.Type switch { JsonObjectType.Boolean => true, JsonObjectType.Integer => true, JsonObjectType.Number => true, - JsonObjectType.String => valueFormats.Contains(_property.Format), + JsonObjectType.String => valueTypeFormats.Contains(_property.Format), _ => false, }; } From 124c5e6d567e7351fbafab76e8e8d4533caa407e Mon Sep 17 00:00:00 2001 From: AroglDarthu Date: Sat, 28 May 2022 22:19:47 +0200 Subject: [PATCH 4/6] Use JsonFormatStrings for ValueTypeFormats --- .../Models/PropertyModel.cs | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs index 031b293ae..5cd6414c2 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs @@ -15,6 +15,13 @@ namespace NJsonSchema.CodeGeneration.CSharp.Models /// The CSharp property template model. public class PropertyModel : PropertyModelBase { + private readonly static string[] ValueTypeFormats = { + JsonFormatStrings.Date, + JsonFormatStrings.DateTime, + JsonFormatStrings.Time, + JsonFormatStrings.Duration, + JsonFormatStrings.Guid + }; private readonly JsonSchemaProperty _property; private readonly CSharpGeneratorSettings _settings; private readonly CSharpTypeResolver _resolver; @@ -86,15 +93,18 @@ public string JsonIgnoreCondition bool IsValueType() { - var valueTypeFormats = new string[] { "date", "date-time", "uuid" }; - return _property.ActualTypeSchema.Type switch + var type = _property.ActualTypeSchema.Type; + if (type.IsBoolean() || type.IsInteger() || type.IsNumber()) { - JsonObjectType.Boolean => true, - JsonObjectType.Integer => true, - JsonObjectType.Number => true, - JsonObjectType.String => valueTypeFormats.Contains(_property.Format), - _ => false, + return true; + } + + if (type.IsString()) + { + return ValueTypeFormats.Contains(_property.Format); }; + + return false; } } } From aafa124370f1d4ffcbd2445ca0199f89cd149f82 Mon Sep 17 00:00:00 2001 From: AroglDarthu Date: Mon, 30 May 2022 15:08:55 +0200 Subject: [PATCH 5/6] Easier fix --- .../JsonIgnoreAttributesTests.cs | 10 +++--- .../Models/PropertyModel.cs | 36 +++---------------- 2 files changed, 9 insertions(+), 37 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/JsonIgnoreAttributesTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/JsonIgnoreAttributesTests.cs index c07f34e0b..f5125244e 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/JsonIgnoreAttributesTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/JsonIgnoreAttributesTests.cs @@ -26,8 +26,6 @@ public async Task When_using_SystemTextJson_JsonIgnoreAttributes_are_generated_b var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings { JsonLibrary = CSharpJsonLibrary.SystemTextJson, - //GenerateNullableReferenceTypes = true, - //GenerateOptionalPropertiesAsNullable = true, }); static string Normalized(string str) => @@ -77,7 +75,7 @@ static string Normalized(string str) => Assert.Contains( Normalized(@" [System.Text.Json.Serialization.JsonPropertyName(""optionalNullableValue"")] - [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)] + [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)] "), Normalized(code) ); @@ -85,7 +83,7 @@ static string Normalized(string str) => Assert.Contains( Normalized(@" [System.Text.Json.Serialization.JsonPropertyName(""optionalRef"")] - [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)] + [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)] "), Normalized(code) ); @@ -154,7 +152,7 @@ static string Normalized(string str) => Assert.Contains( Normalized(@" [System.Text.Json.Serialization.JsonPropertyName(""optionalRef"")] - [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)] + [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)] "), Normalized(code) ); @@ -170,7 +168,7 @@ static string Normalized(string str) => Assert.Contains( Normalized(@" [System.Text.Json.Serialization.JsonPropertyName(""optionalNullableValue"")] - [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull)] + [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)] "), Normalized(code) ); diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs index 5cd6414c2..6ce272bd5 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs @@ -7,7 +7,6 @@ //----------------------------------------------------------------------- using System.Globalization; -using System.Linq; using NJsonSchema.CodeGeneration.Models; namespace NJsonSchema.CodeGeneration.CSharp.Models @@ -77,37 +76,12 @@ public PropertyModel( public bool HasJsonIgnoreCondition => JsonIgnoreCondition != null; /// Returns the System.Text.Json.Serialization.JsonIgnoreCondition value to be applied to the property. - public string JsonIgnoreCondition + public string JsonIgnoreCondition => _property switch { - get - { - var isValueType = IsValueType(); - return _property switch - { - { IsRequired: true } when _settings.RequiredPropertiesMustBeDefined => "System.Text.Json.Serialization.JsonIgnoreCondition.Never", - { IsRequired: false } when isValueType && IsNullable => "System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull", - { IsRequired: false } when isValueType && !IsNullable => "System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault", - { IsRequired: false } when !isValueType => "System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull", - _ => null - }; - - bool IsValueType() - { - var type = _property.ActualTypeSchema.Type; - if (type.IsBoolean() || type.IsInteger() || type.IsNumber()) - { - return true; - } - - if (type.IsString()) - { - return ValueTypeFormats.Contains(_property.Format); - }; - - return false; - } - } - } + { IsRequired: false } => "System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault", + { IsRequired: true } when _settings.RequiredPropertiesMustBeDefined => "System.Text.Json.Serialization.JsonIgnoreCondition.Never", + _ => null + }; /// Gets the json property required. public string JsonPropertyRequiredCode From c973f3195e387c9831f5f11a9c6380aa47d304ca Mon Sep 17 00:00:00 2001 From: AroglDarthu Date: Wed, 13 Jul 2022 16:35:02 +0200 Subject: [PATCH 6/6] Remove unused field --- .../Models/PropertyModel.cs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs index 6ce272bd5..62b3d8f35 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs @@ -14,13 +14,6 @@ namespace NJsonSchema.CodeGeneration.CSharp.Models /// The CSharp property template model. public class PropertyModel : PropertyModelBase { - private readonly static string[] ValueTypeFormats = { - JsonFormatStrings.Date, - JsonFormatStrings.DateTime, - JsonFormatStrings.Time, - JsonFormatStrings.Duration, - JsonFormatStrings.Guid - }; private readonly JsonSchemaProperty _property; private readonly CSharpGeneratorSettings _settings; private readonly CSharpTypeResolver _resolver;