diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/JsonIgnoreAttributesTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/JsonIgnoreAttributesTests.cs deleted file mode 100644 index f5125244e..000000000 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/JsonIgnoreAttributesTests.cs +++ /dev/null @@ -1,177 +0,0 @@ -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, - }); - - 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.WhenWritingDefault)] - "), - Normalized(code) - ); - - Assert.Contains( - Normalized(@" - [System.Text.Json.Serialization.JsonPropertyName(""optionalRef"")] - [System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)] - "), - 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.WhenWritingDefault)] - "), - 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.WhenWritingDefault)] - "), - Normalized(code) - ); - } - } -} diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs index 771167aed..bc1684a9b 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs @@ -66,17 +66,6 @@ public class PropertyModel : PropertyModelBase (_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.WhenWritingDefault", - { IsRequired: true } when _settings.RequiredPropertiesMustBeDefined => "System.Text.Json.Serialization.JsonIgnoreCondition.Never", - _ => null - }; - /// Gets the json property required. public string JsonPropertyRequiredCode { diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid index 41666803f..b04a017f0 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid +++ b/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid @@ -53,9 +53,6 @@ {%- endif -%} {%- if UseSystemTextJson %} [System.Text.Json.Serialization.JsonPropertyName("{{ property.Name }}")] -{%- if property.HasJsonIgnoreCondition %} - [System.Text.Json.Serialization.JsonIgnore(Condition = {{ property.JsonIgnoreCondition }})] -{%- endif -%} {%- if property.IsStringEnumArray %} // TODO(system.text.json): Add string enum item converter {%- endif -%}