From 3a6e7031dd59bf8f3943269569d629397caf1894 Mon Sep 17 00:00:00 2001 From: Kloizdena <17968888+Kloizdena@users.noreply.github.com> Date: Sat, 15 Jan 2022 03:03:25 +0100 Subject: [PATCH 1/6] Avoid name collision for covariant and hidden properties (#63443) --- .../gen/JsonSourceGenerator.Parser.cs | 2 +- .../gen/TypeGenerationSpec.cs | 2 +- .../Serialization/Metadata/JsonTypeInfo.cs | 2 +- .../ContextClasses.cs | 8 ++ .../MetadataAndSerializationContextTests.cs | 17 +++ .../MetadataContextTests.cs | 32 +++++ .../MixedModeContextTests.cs | 16 +++ .../RealWorldContextTests.cs | 116 ++++++++++++++++++ .../SerializationContextTests.cs | 40 ++++++ .../TestClasses.cs | 48 ++++++++ 10 files changed, 280 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs index 9590a3b90f07d..d9c5bd824f2e4 100644 --- a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs +++ b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs @@ -1115,7 +1115,7 @@ private void CacheMember( if (propGenSpec.DefaultIgnoreCondition == JsonIgnoreCondition.Always) { ignoredMembers ??= new Dictionary(); - ignoredMembers.Add(propGenSpec.ClrName, propGenSpec); + ignoredMembers.TryAdd(propGenSpec.ClrName, propGenSpec); } } diff --git a/src/libraries/System.Text.Json/gen/TypeGenerationSpec.cs b/src/libraries/System.Text.Json/gen/TypeGenerationSpec.cs index 39fcd9827b38e..9b67accce2409 100644 --- a/src/libraries/System.Text.Json/gen/TypeGenerationSpec.cs +++ b/src/libraries/System.Text.Json/gen/TypeGenerationSpec.cs @@ -222,7 +222,7 @@ public bool TryFilterSerializableProps( if (propGenSpec.DefaultIgnoreCondition == JsonIgnoreCondition.Always) { - (ignoredMembers ??= new Dictionary()).Add(memberName, propGenSpec); + (ignoredMembers ??= new Dictionary()).TryAdd(memberName, propGenSpec); } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs index 4bd852e447e2b..0bc5a2de3e674 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs @@ -409,7 +409,7 @@ private void CacheMember(JsonPropertyInfo jsonPropertyInfo, JsonPropertyDictiona if (jsonPropertyInfo.IsIgnored) { - (ignoredMembers ??= new Dictionary()).Add(memberName, jsonPropertyInfo); + (ignoredMembers ??= new Dictionary()).TryAdd(memberName, jsonPropertyInfo); } } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/ContextClasses.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/ContextClasses.cs index c01499f7fef3e..f9b8aa817ce7d 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/ContextClasses.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/ContextClasses.cs @@ -47,6 +47,14 @@ public interface ITestContext public JsonTypeInfo ClassWithBadCustomConverter { get; } public JsonTypeInfo StructWithBadCustomConverter { get; } public JsonTypeInfo NullablePersonStruct { get; } + public JsonTypeInfo CovariantDerived { get; } + public JsonTypeInfo> CovariantDerivedGenericString { get; } + public JsonTypeInfo IgnoredPropertyBase { get; } + public JsonTypeInfo NotIgnoredPropertyBase { get; } + public JsonTypeInfo IgnoredPropertyBase_NotIgnoredPropertyDerived { get; } + public JsonTypeInfo IgnoredPropertyBase_IgnoredPropertyDerived { get; } + public JsonTypeInfo NotIgnoredPropertyBase_NotIgnoredPropertyDerived { get; } + public JsonTypeInfo NotIgnoredPropertyBase_IgnoredPropertyDerived { get; } } internal partial class JsonContext : JsonSerializerContext diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataAndSerializationContextTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataAndSerializationContextTests.cs index 57abdf04808a0..8830c84e3366f 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataAndSerializationContextTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataAndSerializationContextTests.cs @@ -41,6 +41,14 @@ namespace System.Text.Json.SourceGeneration.Tests [JsonSerializable(typeof(ClassWithBadCustomConverter))] [JsonSerializable(typeof(StructWithBadCustomConverter))] [JsonSerializable(typeof(PersonStruct?))] + [JsonSerializable(typeof(CovariantDerived))] + [JsonSerializable(typeof(CovariantDerivedGeneric))] + [JsonSerializable(typeof(IgnoredPropertyBase))] + [JsonSerializable(typeof(NotIgnoredPropertyBase))] + [JsonSerializable(typeof(IgnoredPropertyBase_NotIgnoredPropertyDerived))] + [JsonSerializable(typeof(NotIgnoredPropertyBase_IgnoredPropertyDerived))] + [JsonSerializable(typeof(NotIgnoredPropertyBase_NotIgnoredPropertyDerived))] + [JsonSerializable(typeof(IgnoredPropertyBase_IgnoredPropertyDerived))] internal partial class MetadataAndSerializationContext : JsonSerializerContext, ITestContext { public JsonSourceGenerationMode JsonSourceGenerationMode => JsonSourceGenerationMode.Default; @@ -88,6 +96,15 @@ public override void EnsureFastPathGeneratedAsExpected() Assert.Throws(() => MetadataAndSerializationContext.Default.StructWithBadCustomConverter); Assert.Null(MetadataAndSerializationContext.Default.NullablePersonStruct.SerializeHandler); Assert.NotNull(MetadataAndSerializationContext.Default.PersonStruct.SerializeHandler); + Assert.Null(MetadataAndSerializationContext.Default.CovariantDerived.SerializeHandler); + Assert.Null(MetadataAndSerializationContext.Default.CovariantDerivedGenericString.SerializeHandler); + Assert.NotNull(MetadataAndSerializationContext.Default.IgnoredPropertyBase.SerializeHandler); + Assert.NotNull(MetadataAndSerializationContext.Default.NotIgnoredPropertyBase.SerializeHandler); + Assert.NotNull(MetadataAndSerializationContext.Default.IgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); + Assert.NotNull(MetadataAndSerializationContext.Default.IgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); + Assert.NotNull(MetadataAndSerializationContext.Default.NotIgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); + Assert.NotNull(MetadataAndSerializationContext.Default.NotIgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); + } } } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataContextTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataContextTests.cs index d0c3023b41ae9..4faa0afcb1650 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataContextTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataContextTests.cs @@ -40,6 +40,14 @@ namespace System.Text.Json.SourceGeneration.Tests [JsonSerializable(typeof(ClassWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Metadata)] [JsonSerializable(typeof(StructWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Metadata)] [JsonSerializable(typeof(PersonStruct?), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(CovariantDerived), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(CovariantDerivedGeneric), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(IgnoredPropertyBase), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(NotIgnoredPropertyBase), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(IgnoredPropertyBase_NotIgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(NotIgnoredPropertyBase_IgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(NotIgnoredPropertyBase_NotIgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(IgnoredPropertyBase_IgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata)] internal partial class MetadataWithPerTypeAttributeContext : JsonSerializerContext, ITestContext { public JsonSourceGenerationMode JsonSourceGenerationMode => JsonSourceGenerationMode.Metadata; @@ -86,6 +94,14 @@ public override void EnsureFastPathGeneratedAsExpected() Assert.Throws(() => MetadataWithPerTypeAttributeContext.Default.StructWithBadCustomConverter.SerializeHandler); Assert.Null(MetadataWithPerTypeAttributeContext.Default.NullablePersonStruct.SerializeHandler); Assert.Null(MetadataWithPerTypeAttributeContext.Default.PersonStruct.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.CovariantDerived.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.CovariantDerivedGenericString.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.IgnoredPropertyBase.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.NotIgnoredPropertyBase.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.IgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.IgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.NotIgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.NotIgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); } } @@ -124,6 +140,14 @@ public override void EnsureFastPathGeneratedAsExpected() [JsonSerializable(typeof(ClassWithBadCustomConverter))] [JsonSerializable(typeof(StructWithBadCustomConverter))] [JsonSerializable(typeof(PersonStruct?))] + [JsonSerializable(typeof(CovariantDerived))] + [JsonSerializable(typeof(CovariantDerivedGeneric))] + [JsonSerializable(typeof(IgnoredPropertyBase))] + [JsonSerializable(typeof(NotIgnoredPropertyBase))] + [JsonSerializable(typeof(IgnoredPropertyBase_NotIgnoredPropertyDerived))] + [JsonSerializable(typeof(NotIgnoredPropertyBase_IgnoredPropertyDerived))] + [JsonSerializable(typeof(NotIgnoredPropertyBase_NotIgnoredPropertyDerived))] + [JsonSerializable(typeof(IgnoredPropertyBase_IgnoredPropertyDerived))] internal partial class MetadataContext : JsonSerializerContext, ITestContext { public JsonSourceGenerationMode JsonSourceGenerationMode => JsonSourceGenerationMode.Metadata; @@ -193,6 +217,14 @@ public override void EnsureFastPathGeneratedAsExpected() Assert.Throws(() => MetadataContext.Default.StructWithBadCustomConverter.SerializeHandler); Assert.Null(MetadataContext.Default.NullablePersonStruct.SerializeHandler); Assert.Null(MetadataContext.Default.PersonStruct.SerializeHandler); + Assert.Null(MetadataContext.Default.CovariantDerived.SerializeHandler); + Assert.Null(MetadataContext.Default.CovariantDerivedGenericString.SerializeHandler); + Assert.Null(MetadataContext.Default.IgnoredPropertyBase.SerializeHandler); + Assert.Null(MetadataContext.Default.NotIgnoredPropertyBase.SerializeHandler); + Assert.Null(MetadataContext.Default.IgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); + Assert.Null(MetadataContext.Default.IgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); + Assert.Null(MetadataContext.Default.NotIgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); + Assert.Null(MetadataContext.Default.NotIgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); } [Fact] diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MixedModeContextTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MixedModeContextTests.cs index d547551569935..9a10fbff8758d 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MixedModeContextTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MixedModeContextTests.cs @@ -41,6 +41,14 @@ namespace System.Text.Json.SourceGeneration.Tests [JsonSerializable(typeof(ClassWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(StructWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(PersonStruct?), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantDerived), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantDerivedGeneric), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(IgnoredPropertyBase), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(NotIgnoredPropertyBase), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(IgnoredPropertyBase_NotIgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(NotIgnoredPropertyBase_IgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(NotIgnoredPropertyBase_NotIgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(IgnoredPropertyBase_IgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] internal partial class MixedModeContext : JsonSerializerContext, ITestContext { public JsonSourceGenerationMode JsonSourceGenerationMode => JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization; @@ -88,6 +96,14 @@ public override void EnsureFastPathGeneratedAsExpected() Assert.Throws(() => MixedModeContext.Default.StructWithBadCustomConverter.SerializeHandler); Assert.Null(MixedModeContext.Default.NullablePersonStruct.SerializeHandler); Assert.NotNull(MixedModeContext.Default.PersonStruct.SerializeHandler); + Assert.Null(MixedModeContext.Default.CovariantDerived.SerializeHandler); + Assert.Null(MixedModeContext.Default.CovariantDerivedGenericString.SerializeHandler); + Assert.NotNull(MixedModeContext.Default.IgnoredPropertyBase.SerializeHandler); + Assert.NotNull(MixedModeContext.Default.NotIgnoredPropertyBase.SerializeHandler); + Assert.NotNull(MixedModeContext.Default.IgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); + Assert.NotNull(MixedModeContext.Default.IgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); + Assert.NotNull(MixedModeContext.Default.NotIgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); + Assert.NotNull(MixedModeContext.Default.NotIgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); } [Fact] diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/RealWorldContextTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/RealWorldContextTests.cs index 1cb9cb56bbf9f..31a26e0e67bb3 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/RealWorldContextTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/RealWorldContextTests.cs @@ -882,5 +882,121 @@ public virtual void NullableStruct() Assert.Equal("Jane", person.Value.FirstName); Assert.Equal("Doe", person.Value.LastName); } + + [Fact] + public void JsonIgnoreForCovariantProperties() + { + var derived = new CovariantDerived(); + + if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) + { + Assert.Throws(() => JsonSerializer.Serialize(derived, DefaultContext.CovariantDerived)); + } + else + { + string json = JsonSerializer.Serialize(derived, DefaultContext.CovariantDerived); + JsonTestHelper.AssertJsonEqual(@"{}", json); + derived = JsonSerializer.Deserialize(json, DefaultContext.CovariantDerived); + Assert.Null(derived.Id); + } + } + + [Fact] + public void JsonIgnoreForCovariantGenericProperties() + { + var derived = new CovariantDerivedGeneric(); + if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) + { + Assert.Throws(() => JsonSerializer.Serialize(derived, DefaultContext.CovariantDerivedGenericString)); + } + else + { + string json = JsonSerializer.Serialize(derived, DefaultContext.CovariantDerivedGenericString); + JsonTestHelper.AssertJsonEqual(@"{}", json); + + derived = JsonSerializer.Deserialize(json, DefaultContext.CovariantDerivedGenericString); + Assert.Null(derived.Id); + } + } + + [Fact] + public void JsonIgnoreForHiddenProperties_IgnoredBase_NotIgnoredDerived() + { + var derived = new IgnoredPropertyBase_NotIgnoredPropertyDerived { Id = "Test" }; + + string json = JsonSerializer.Serialize(derived, DefaultContext.IgnoredPropertyBase_NotIgnoredPropertyDerived); + JsonTestHelper.AssertJsonEqual("{\"Id\":\"Test\"}", json); + + if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) + { + // Deserialization not supported in fast path serialization only mode + Assert.Throws(() => JsonSerializer.Deserialize(json, DefaultContext.IgnoredPropertyBase_NotIgnoredPropertyDerived)); + } + else + { + derived = JsonSerializer.Deserialize(json, DefaultContext.IgnoredPropertyBase_NotIgnoredPropertyDerived); + Assert.Equal("Test", derived.Id); + } + } + + [Fact] + public void JsonIgnoreForHiddenProperties_NotIgnoredBase_IgnoredDerived() + { + var derived = new NotIgnoredPropertyBase_IgnoredPropertyDerived { Id = "Test" }; + + string json = JsonSerializer.Serialize(derived, DefaultContext.NotIgnoredPropertyBase_IgnoredPropertyDerived); + JsonTestHelper.AssertJsonEqual("{\"Id\":null}", json); + + if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) + { + // Deserialization not supported in fast path serialization only mode + Assert.Throws(() => JsonSerializer.Deserialize(json, DefaultContext.NotIgnoredPropertyBase_IgnoredPropertyDerived)); + } + else + { + derived = JsonSerializer.Deserialize(json, DefaultContext.NotIgnoredPropertyBase_IgnoredPropertyDerived); + Assert.Null(derived.Id); + } + } + + [Fact] + public void JsonIgnoreForHiddenProperties_IgnoredBase_IgnoredDerived() + { + var derived = new IgnoredPropertyBase_IgnoredPropertyDerived { Id = "Test" }; + + string json = JsonSerializer.Serialize(derived, DefaultContext.IgnoredPropertyBase_IgnoredPropertyDerived); + JsonTestHelper.AssertJsonEqual(@"{}", json); + + if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) + { + // Deserialization not supported in fast path serialization only mode + Assert.Throws(() => JsonSerializer.Deserialize(json, DefaultContext.IgnoredPropertyBase_IgnoredPropertyDerived)); + } + else + { + derived = JsonSerializer.Deserialize(json, DefaultContext.IgnoredPropertyBase_IgnoredPropertyDerived); + Assert.Null(derived.Id); + } + } + + [Fact] + public void JsonIgnoreForHiddenProperties_NotIgnoredBase_NotIgnoredDerived() + { + var derived = new NotIgnoredPropertyBase_NotIgnoredPropertyDerived { Id = "Test" }; + + string json = JsonSerializer.Serialize(derived, DefaultContext.NotIgnoredPropertyBase_NotIgnoredPropertyDerived); + JsonTestHelper.AssertJsonEqual("{\"Id\":\"Test\"}", json); + + if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) + { + // Deserialization not supported in fast path serialization only mode + Assert.Throws(() => JsonSerializer.Deserialize(json, DefaultContext.NotIgnoredPropertyBase_NotIgnoredPropertyDerived)); + } + else + { + derived = JsonSerializer.Deserialize(json, DefaultContext.NotIgnoredPropertyBase_NotIgnoredPropertyDerived); + Assert.Equal("Test", derived.Id); + } + } } } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationContextTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationContextTests.cs index b51ba070be188..dda810daea77b 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationContextTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationContextTests.cs @@ -41,6 +41,14 @@ namespace System.Text.Json.SourceGeneration.Tests [JsonSerializable(typeof(ClassWithBadCustomConverter))] [JsonSerializable(typeof(StructWithBadCustomConverter))] [JsonSerializable(typeof(PersonStruct?))] + [JsonSerializable(typeof(CovariantDerived))] + [JsonSerializable(typeof(CovariantDerivedGeneric))] + [JsonSerializable(typeof(IgnoredPropertyBase))] + [JsonSerializable(typeof(NotIgnoredPropertyBase))] + [JsonSerializable(typeof(IgnoredPropertyBase_NotIgnoredPropertyDerived))] + [JsonSerializable(typeof(NotIgnoredPropertyBase_IgnoredPropertyDerived))] + [JsonSerializable(typeof(NotIgnoredPropertyBase_NotIgnoredPropertyDerived))] + [JsonSerializable(typeof(IgnoredPropertyBase_IgnoredPropertyDerived))] internal partial class SerializationContext : JsonSerializerContext, ITestContext { public JsonSourceGenerationMode JsonSourceGenerationMode => JsonSourceGenerationMode.Serialization; @@ -80,6 +88,14 @@ internal partial class SerializationContext : JsonSerializerContext, ITestContex [JsonSerializable(typeof(ClassWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(StructWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(PersonStruct?), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantDerivedGeneric), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(IgnoredPropertyBase), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(NotIgnoredPropertyBase), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(IgnoredPropertyBase_NotIgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(NotIgnoredPropertyBase_IgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(NotIgnoredPropertyBase_NotIgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(IgnoredPropertyBase_IgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] internal partial class SerializationWithPerTypeAttributeContext : JsonSerializerContext, ITestContext { public JsonSourceGenerationMode JsonSourceGenerationMode => JsonSourceGenerationMode.Serialization; @@ -120,6 +136,14 @@ internal partial class SerializationWithPerTypeAttributeContext : JsonSerializer [JsonSerializable(typeof(ClassWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(StructWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(PersonStruct?), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantDerivedGeneric), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(IgnoredPropertyBase), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(NotIgnoredPropertyBase), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(IgnoredPropertyBase_NotIgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(NotIgnoredPropertyBase_IgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(NotIgnoredPropertyBase_NotIgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(IgnoredPropertyBase_IgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] internal partial class SerializationContextWithCamelCase : JsonSerializerContext, ITestContext { public JsonSourceGenerationMode JsonSourceGenerationMode => JsonSourceGenerationMode.Serialization; @@ -169,6 +193,14 @@ public override void EnsureFastPathGeneratedAsExpected() Assert.Throws(() => SerializationContext.Default.StructWithBadCustomConverter.SerializeHandler); Assert.Null(SerializationContext.Default.NullablePersonStruct.SerializeHandler); Assert.NotNull(SerializationContext.Default.PersonStruct.SerializeHandler); + Assert.Null(SerializationContext.Default.CovariantDerived.SerializeHandler); + Assert.Null(SerializationContext.Default.CovariantDerivedGenericString.SerializeHandler); + Assert.NotNull(SerializationContext.Default.IgnoredPropertyBase.SerializeHandler); + Assert.NotNull(SerializationContext.Default.NotIgnoredPropertyBase.SerializeHandler); + Assert.NotNull(SerializationContext.Default.IgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); + Assert.NotNull(SerializationContext.Default.IgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); + Assert.NotNull(SerializationContext.Default.NotIgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); + Assert.NotNull(SerializationContext.Default.NotIgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); } [Fact] @@ -492,6 +524,14 @@ public override void EnsureFastPathGeneratedAsExpected() Assert.Throws(() => SerializationWithPerTypeAttributeContext.Default.StructWithBadCustomConverter.SerializeHandler); Assert.Null(SerializationWithPerTypeAttributeContext.Default.NullablePersonStruct.SerializeHandler); Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.PersonStruct.SerializeHandler); + Assert.Null(SerializationWithPerTypeAttributeContext.Default.CovariantDerived.SerializeHandler); + Assert.Null(SerializationWithPerTypeAttributeContext.Default.CovariantDerivedGenericString.SerializeHandler); + Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.IgnoredPropertyBase.SerializeHandler); + Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.NotIgnoredPropertyBase.SerializeHandler); + Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.IgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); + Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.IgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); + Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.NotIgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); + Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.NotIgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); } } } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs index 29e76ec6e1560..ae37ad1fdf017 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs @@ -158,4 +158,52 @@ public struct PersonStruct public string FirstName { get; set; } public string LastName { get; set; } } + + + + public class CovariantBase + { + [JsonIgnore] + public virtual object Id { get; } + } + public class CovariantDerived : CovariantBase + { + [JsonIgnore] + public override string Id { get; } + } + public class CovariantDerivedGeneric : CovariantBase + where T : class + { + [JsonIgnore] + public override T Id { get; } + } + + public class IgnoredPropertyBase + { + [JsonIgnore] + public string Id { get; set; } + } + public class NotIgnoredPropertyBase + { + public string Id { get; set; } + } + + public class IgnoredPropertyBase_NotIgnoredPropertyDerived : IgnoredPropertyBase + { + public new string Id { get; set; } + } + public class NotIgnoredPropertyBase_IgnoredPropertyDerived : NotIgnoredPropertyBase + { + [JsonIgnore] + public new string Id { get; set; } + } + public class NotIgnoredPropertyBase_NotIgnoredPropertyDerived : NotIgnoredPropertyBase + { + public new string Id { get; set; } + } + public class IgnoredPropertyBase_IgnoredPropertyDerived : IgnoredPropertyBase + { + [JsonIgnore] + public new string Id { get; set; } + } } From 4c520e4114e0c715daade7d0dc4740947f93a3ed Mon Sep 17 00:00:00 2001 From: Kloizdena <17968888+Kloizdena@users.noreply.github.com> Date: Mon, 24 Jan 2022 13:12:13 +0100 Subject: [PATCH 2/6] code style fix for TestClasses --- .../TestClasses.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs index ae37ad1fdf017..e2bf6a49dfcc0 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs @@ -159,18 +159,18 @@ public struct PersonStruct public string LastName { get; set; } } - - public class CovariantBase { [JsonIgnore] public virtual object Id { get; } } + public class CovariantDerived : CovariantBase { [JsonIgnore] public override string Id { get; } } + public class CovariantDerivedGeneric : CovariantBase where T : class { @@ -183,6 +183,7 @@ public class IgnoredPropertyBase [JsonIgnore] public string Id { get; set; } } + public class NotIgnoredPropertyBase { public string Id { get; set; } @@ -192,15 +193,18 @@ public class IgnoredPropertyBase_NotIgnoredPropertyDerived : IgnoredPropertyBase { public new string Id { get; set; } } + public class NotIgnoredPropertyBase_IgnoredPropertyDerived : NotIgnoredPropertyBase { [JsonIgnore] public new string Id { get; set; } } + public class NotIgnoredPropertyBase_NotIgnoredPropertyDerived : NotIgnoredPropertyBase { public new string Id { get; set; } } + public class IgnoredPropertyBase_IgnoredPropertyDerived : IgnoredPropertyBase { [JsonIgnore] From 5947879440248943b1ff9ccfae50457bb80348d1 Mon Sep 17 00:00:00 2001 From: Kloizdena <17968888+Kloizdena@users.noreply.github.com> Date: Mon, 24 Jan 2022 13:18:49 +0100 Subject: [PATCH 3/6] test class reordering --- .../MetadataAndSerializationContextTests.cs | 1 - .../TestClasses.cs | 16 ++++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataAndSerializationContextTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataAndSerializationContextTests.cs index 8830c84e3366f..038e73da7717d 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataAndSerializationContextTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataAndSerializationContextTests.cs @@ -104,7 +104,6 @@ public override void EnsureFastPathGeneratedAsExpected() Assert.NotNull(MetadataAndSerializationContext.Default.IgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); Assert.NotNull(MetadataAndSerializationContext.Default.NotIgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); Assert.NotNull(MetadataAndSerializationContext.Default.NotIgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); - } } } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs index e2bf6a49dfcc0..0e1e9b294b1c9 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs @@ -178,18 +178,12 @@ public class CovariantDerivedGeneric : CovariantBase public override T Id { get; } } - public class IgnoredPropertyBase - { - [JsonIgnore] - public string Id { get; set; } - } - public class NotIgnoredPropertyBase { public string Id { get; set; } } - public class IgnoredPropertyBase_NotIgnoredPropertyDerived : IgnoredPropertyBase + public class NotIgnoredPropertyBase_NotIgnoredPropertyDerived : NotIgnoredPropertyBase { public new string Id { get; set; } } @@ -200,7 +194,13 @@ public class NotIgnoredPropertyBase_IgnoredPropertyDerived : NotIgnoredPropertyB public new string Id { get; set; } } - public class NotIgnoredPropertyBase_NotIgnoredPropertyDerived : NotIgnoredPropertyBase + public class IgnoredPropertyBase + { + [JsonIgnore] + public string Id { get; set; } + } + + public class IgnoredPropertyBase_NotIgnoredPropertyDerived : IgnoredPropertyBase { public new string Id { get; set; } } From 0a2fe14e46f6644c73bb7ef913e5559339d35329 Mon Sep 17 00:00:00 2001 From: Kloizdena <17968888+Kloizdena@users.noreply.github.com> Date: Mon, 24 Jan 2022 20:43:49 +0100 Subject: [PATCH 4/6] Update src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs Co-authored-by: Eirik Tsarpalis --- .../src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs index 0bc5a2de3e674..51d134f98dbe0 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfo.cs @@ -409,7 +409,7 @@ private void CacheMember(JsonPropertyInfo jsonPropertyInfo, JsonPropertyDictiona if (jsonPropertyInfo.IsIgnored) { - (ignoredMembers ??= new Dictionary()).TryAdd(memberName, jsonPropertyInfo); + (ignoredMembers ??= new()).TryAdd(memberName, jsonPropertyInfo); } } From e79d39676fe77c26bf61bed9c14debc19fd64e23 Mon Sep 17 00:00:00 2001 From: Kloizdena <17968888+Kloizdena@users.noreply.github.com> Date: Mon, 24 Jan 2022 20:43:55 +0100 Subject: [PATCH 5/6] Update src/libraries/System.Text.Json/gen/TypeGenerationSpec.cs Co-authored-by: Eirik Tsarpalis --- src/libraries/System.Text.Json/gen/TypeGenerationSpec.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Text.Json/gen/TypeGenerationSpec.cs b/src/libraries/System.Text.Json/gen/TypeGenerationSpec.cs index 9b67accce2409..2077673b8afb8 100644 --- a/src/libraries/System.Text.Json/gen/TypeGenerationSpec.cs +++ b/src/libraries/System.Text.Json/gen/TypeGenerationSpec.cs @@ -222,7 +222,7 @@ public bool TryFilterSerializableProps( if (propGenSpec.DefaultIgnoreCondition == JsonIgnoreCondition.Always) { - (ignoredMembers ??= new Dictionary()).TryAdd(memberName, propGenSpec); + (ignoredMembers ??= new()).TryAdd(memberName, propGenSpec); } } From 7d75c92a69e37fe810920e69f478a952123ba933 Mon Sep 17 00:00:00 2001 From: Kloizdena <17968888+Kloizdena@users.noreply.github.com> Date: Mon, 24 Jan 2022 23:23:10 +0100 Subject: [PATCH 6/6] Additional inheritance tests for JsonIgnore --- .../ContextClasses.cs | 16 +- .../MetadataAndSerializationContextTests.cs | 32 ++- .../MetadataContextTests.cs | 64 ++++- .../MixedModeContextTests.cs | 32 ++- .../RealWorldContextTests.cs | 226 ++++++++++++++++-- .../SerializationContextTests.cs | 80 ++++++- .../TestClasses.cs | 97 +++++++- 7 files changed, 488 insertions(+), 59 deletions(-) diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/ContextClasses.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/ContextClasses.cs index f9b8aa817ce7d..7c6980be2951b 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/ContextClasses.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/ContextClasses.cs @@ -47,14 +47,26 @@ public interface ITestContext public JsonTypeInfo ClassWithBadCustomConverter { get; } public JsonTypeInfo StructWithBadCustomConverter { get; } public JsonTypeInfo NullablePersonStruct { get; } - public JsonTypeInfo CovariantDerived { get; } - public JsonTypeInfo> CovariantDerivedGenericString { get; } + public JsonTypeInfo CovariantBaseNotIgnored_CovariantDerivedNotIgnored { get; } + public JsonTypeInfo> CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnoredString { get; } + public JsonTypeInfo CovariantBaseNotIgnored_CovariantDerivedIgnored { get; } + public JsonTypeInfo> CovariantBaseNotIgnored_CovariantDerivedGenericIgnoredString { get; } + public JsonTypeInfo CovariantBaseIgnored_CovariantDerivedNotIgnored { get; } + public JsonTypeInfo> CovariantBaseIgnored_CovariantDerivedGenericNotIgnoredString { get; } + public JsonTypeInfo CovariantBaseIgnored_CovariantDerivedIgnored { get; } + public JsonTypeInfo> CovariantBaseIgnored_CovariantDerivedGenericIgnoredString { get; } public JsonTypeInfo IgnoredPropertyBase { get; } public JsonTypeInfo NotIgnoredPropertyBase { get; } public JsonTypeInfo IgnoredPropertyBase_NotIgnoredPropertyDerived { get; } public JsonTypeInfo IgnoredPropertyBase_IgnoredPropertyDerived { get; } public JsonTypeInfo NotIgnoredPropertyBase_NotIgnoredPropertyDerived { get; } public JsonTypeInfo NotIgnoredPropertyBase_IgnoredPropertyDerived { get; } + public JsonTypeInfo IgnoredVirtualPropertyBase { get; } + public JsonTypeInfo NotIgnoredVirtualPropertyBase { get; } + public JsonTypeInfo IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived { get; } + public JsonTypeInfo IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived { get; } + public JsonTypeInfo NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived { get; } + public JsonTypeInfo NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived { get; } } internal partial class JsonContext : JsonSerializerContext diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataAndSerializationContextTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataAndSerializationContextTests.cs index 038e73da7717d..07a25951cd35a 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataAndSerializationContextTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataAndSerializationContextTests.cs @@ -41,14 +41,26 @@ namespace System.Text.Json.SourceGeneration.Tests [JsonSerializable(typeof(ClassWithBadCustomConverter))] [JsonSerializable(typeof(StructWithBadCustomConverter))] [JsonSerializable(typeof(PersonStruct?))] - [JsonSerializable(typeof(CovariantDerived))] - [JsonSerializable(typeof(CovariantDerivedGeneric))] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedNotIgnored))] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnored))] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedIgnored))] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedGenericIgnored))] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedNotIgnored))] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedGenericNotIgnored))] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedIgnored))] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedGenericIgnored))] [JsonSerializable(typeof(IgnoredPropertyBase))] [JsonSerializable(typeof(NotIgnoredPropertyBase))] [JsonSerializable(typeof(IgnoredPropertyBase_NotIgnoredPropertyDerived))] [JsonSerializable(typeof(NotIgnoredPropertyBase_IgnoredPropertyDerived))] [JsonSerializable(typeof(NotIgnoredPropertyBase_NotIgnoredPropertyDerived))] [JsonSerializable(typeof(IgnoredPropertyBase_IgnoredPropertyDerived))] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase))] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase))] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived))] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived))] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived))] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived))] internal partial class MetadataAndSerializationContext : JsonSerializerContext, ITestContext { public JsonSourceGenerationMode JsonSourceGenerationMode => JsonSourceGenerationMode.Default; @@ -96,14 +108,26 @@ public override void EnsureFastPathGeneratedAsExpected() Assert.Throws(() => MetadataAndSerializationContext.Default.StructWithBadCustomConverter); Assert.Null(MetadataAndSerializationContext.Default.NullablePersonStruct.SerializeHandler); Assert.NotNull(MetadataAndSerializationContext.Default.PersonStruct.SerializeHandler); - Assert.Null(MetadataAndSerializationContext.Default.CovariantDerived.SerializeHandler); - Assert.Null(MetadataAndSerializationContext.Default.CovariantDerivedGenericString.SerializeHandler); + Assert.Null(MetadataAndSerializationContext.Default.CovariantBaseNotIgnored_CovariantDerivedNotIgnored.SerializeHandler); + Assert.Null(MetadataAndSerializationContext.Default.CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnoredString.SerializeHandler); + Assert.Null(MetadataAndSerializationContext.Default.CovariantBaseNotIgnored_CovariantDerivedIgnored.SerializeHandler); + Assert.Null(MetadataAndSerializationContext.Default.CovariantBaseNotIgnored_CovariantDerivedGenericIgnoredString.SerializeHandler); + Assert.Null(MetadataAndSerializationContext.Default.CovariantBaseIgnored_CovariantDerivedNotIgnored.SerializeHandler); + Assert.Null(MetadataAndSerializationContext.Default.CovariantBaseIgnored_CovariantDerivedGenericNotIgnoredString.SerializeHandler); + Assert.Null(MetadataAndSerializationContext.Default.CovariantBaseIgnored_CovariantDerivedIgnored.SerializeHandler); + Assert.Null(MetadataAndSerializationContext.Default.CovariantBaseIgnored_CovariantDerivedGenericIgnoredString.SerializeHandler); Assert.NotNull(MetadataAndSerializationContext.Default.IgnoredPropertyBase.SerializeHandler); Assert.NotNull(MetadataAndSerializationContext.Default.NotIgnoredPropertyBase.SerializeHandler); Assert.NotNull(MetadataAndSerializationContext.Default.IgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); Assert.NotNull(MetadataAndSerializationContext.Default.IgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); Assert.NotNull(MetadataAndSerializationContext.Default.NotIgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); Assert.NotNull(MetadataAndSerializationContext.Default.NotIgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); + Assert.NotNull(MetadataAndSerializationContext.Default.IgnoredVirtualPropertyBase.SerializeHandler); + Assert.NotNull(MetadataAndSerializationContext.Default.NotIgnoredVirtualPropertyBase.SerializeHandler); + Assert.NotNull(MetadataAndSerializationContext.Default.IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived.SerializeHandler); + Assert.NotNull(MetadataAndSerializationContext.Default.IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived.SerializeHandler); + Assert.NotNull(MetadataAndSerializationContext.Default.NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived.SerializeHandler); + Assert.NotNull(MetadataAndSerializationContext.Default.NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived.SerializeHandler); } } } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataContextTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataContextTests.cs index 4faa0afcb1650..50079c21bbb52 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataContextTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MetadataContextTests.cs @@ -40,14 +40,26 @@ namespace System.Text.Json.SourceGeneration.Tests [JsonSerializable(typeof(ClassWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Metadata)] [JsonSerializable(typeof(StructWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Metadata)] [JsonSerializable(typeof(PersonStruct?), GenerationMode = JsonSourceGenerationMode.Metadata)] - [JsonSerializable(typeof(CovariantDerived), GenerationMode = JsonSourceGenerationMode.Metadata)] - [JsonSerializable(typeof(CovariantDerivedGeneric), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedNotIgnored), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnored), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedIgnored), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedGenericIgnored), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedNotIgnored), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedGenericNotIgnored), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedIgnored), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedGenericIgnored), GenerationMode = JsonSourceGenerationMode.Metadata)] [JsonSerializable(typeof(IgnoredPropertyBase), GenerationMode = JsonSourceGenerationMode.Metadata)] [JsonSerializable(typeof(NotIgnoredPropertyBase), GenerationMode = JsonSourceGenerationMode.Metadata)] [JsonSerializable(typeof(IgnoredPropertyBase_NotIgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata)] [JsonSerializable(typeof(NotIgnoredPropertyBase_IgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata)] [JsonSerializable(typeof(NotIgnoredPropertyBase_NotIgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata)] [JsonSerializable(typeof(IgnoredPropertyBase_IgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata)] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata)] internal partial class MetadataWithPerTypeAttributeContext : JsonSerializerContext, ITestContext { public JsonSourceGenerationMode JsonSourceGenerationMode => JsonSourceGenerationMode.Metadata; @@ -94,14 +106,26 @@ public override void EnsureFastPathGeneratedAsExpected() Assert.Throws(() => MetadataWithPerTypeAttributeContext.Default.StructWithBadCustomConverter.SerializeHandler); Assert.Null(MetadataWithPerTypeAttributeContext.Default.NullablePersonStruct.SerializeHandler); Assert.Null(MetadataWithPerTypeAttributeContext.Default.PersonStruct.SerializeHandler); - Assert.Null(MetadataWithPerTypeAttributeContext.Default.CovariantDerived.SerializeHandler); - Assert.Null(MetadataWithPerTypeAttributeContext.Default.CovariantDerivedGenericString.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.CovariantBaseNotIgnored_CovariantDerivedNotIgnored.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnoredString.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.CovariantBaseNotIgnored_CovariantDerivedIgnored.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.CovariantBaseNotIgnored_CovariantDerivedGenericIgnoredString.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.CovariantBaseIgnored_CovariantDerivedNotIgnored.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.CovariantBaseIgnored_CovariantDerivedGenericNotIgnoredString.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.CovariantBaseIgnored_CovariantDerivedIgnored.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.CovariantBaseIgnored_CovariantDerivedGenericIgnoredString.SerializeHandler); Assert.Null(MetadataWithPerTypeAttributeContext.Default.IgnoredPropertyBase.SerializeHandler); Assert.Null(MetadataWithPerTypeAttributeContext.Default.NotIgnoredPropertyBase.SerializeHandler); Assert.Null(MetadataWithPerTypeAttributeContext.Default.IgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); Assert.Null(MetadataWithPerTypeAttributeContext.Default.IgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); Assert.Null(MetadataWithPerTypeAttributeContext.Default.NotIgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); Assert.Null(MetadataWithPerTypeAttributeContext.Default.NotIgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.IgnoredVirtualPropertyBase.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.NotIgnoredVirtualPropertyBase.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived.SerializeHandler); + Assert.Null(MetadataWithPerTypeAttributeContext.Default.NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived.SerializeHandler); } } @@ -140,14 +164,26 @@ public override void EnsureFastPathGeneratedAsExpected() [JsonSerializable(typeof(ClassWithBadCustomConverter))] [JsonSerializable(typeof(StructWithBadCustomConverter))] [JsonSerializable(typeof(PersonStruct?))] - [JsonSerializable(typeof(CovariantDerived))] - [JsonSerializable(typeof(CovariantDerivedGeneric))] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedNotIgnored))] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnored))] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedIgnored))] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedGenericIgnored))] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedNotIgnored))] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedGenericNotIgnored))] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedIgnored))] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedGenericIgnored))] [JsonSerializable(typeof(IgnoredPropertyBase))] [JsonSerializable(typeof(NotIgnoredPropertyBase))] [JsonSerializable(typeof(IgnoredPropertyBase_NotIgnoredPropertyDerived))] [JsonSerializable(typeof(NotIgnoredPropertyBase_IgnoredPropertyDerived))] [JsonSerializable(typeof(NotIgnoredPropertyBase_NotIgnoredPropertyDerived))] [JsonSerializable(typeof(IgnoredPropertyBase_IgnoredPropertyDerived))] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase))] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase))] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived))] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived))] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived))] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived))] internal partial class MetadataContext : JsonSerializerContext, ITestContext { public JsonSourceGenerationMode JsonSourceGenerationMode => JsonSourceGenerationMode.Metadata; @@ -217,14 +253,26 @@ public override void EnsureFastPathGeneratedAsExpected() Assert.Throws(() => MetadataContext.Default.StructWithBadCustomConverter.SerializeHandler); Assert.Null(MetadataContext.Default.NullablePersonStruct.SerializeHandler); Assert.Null(MetadataContext.Default.PersonStruct.SerializeHandler); - Assert.Null(MetadataContext.Default.CovariantDerived.SerializeHandler); - Assert.Null(MetadataContext.Default.CovariantDerivedGenericString.SerializeHandler); + Assert.Null(MetadataContext.Default.CovariantBaseNotIgnored_CovariantDerivedNotIgnored.SerializeHandler); + Assert.Null(MetadataContext.Default.CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnoredString.SerializeHandler); + Assert.Null(MetadataContext.Default.CovariantBaseNotIgnored_CovariantDerivedIgnored.SerializeHandler); + Assert.Null(MetadataContext.Default.CovariantBaseNotIgnored_CovariantDerivedGenericIgnoredString.SerializeHandler); + Assert.Null(MetadataContext.Default.CovariantBaseIgnored_CovariantDerivedNotIgnored.SerializeHandler); + Assert.Null(MetadataContext.Default.CovariantBaseIgnored_CovariantDerivedGenericNotIgnoredString.SerializeHandler); + Assert.Null(MetadataContext.Default.CovariantBaseIgnored_CovariantDerivedIgnored.SerializeHandler); + Assert.Null(MetadataContext.Default.CovariantBaseIgnored_CovariantDerivedGenericIgnoredString.SerializeHandler); Assert.Null(MetadataContext.Default.IgnoredPropertyBase.SerializeHandler); Assert.Null(MetadataContext.Default.NotIgnoredPropertyBase.SerializeHandler); Assert.Null(MetadataContext.Default.IgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); Assert.Null(MetadataContext.Default.IgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); Assert.Null(MetadataContext.Default.NotIgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); Assert.Null(MetadataContext.Default.NotIgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); + Assert.Null(MetadataContext.Default.IgnoredVirtualPropertyBase.SerializeHandler); + Assert.Null(MetadataContext.Default.NotIgnoredVirtualPropertyBase.SerializeHandler); + Assert.Null(MetadataContext.Default.IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived.SerializeHandler); + Assert.Null(MetadataContext.Default.IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived.SerializeHandler); + Assert.Null(MetadataContext.Default.NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived.SerializeHandler); + Assert.Null(MetadataContext.Default.NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived.SerializeHandler); } [Fact] diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MixedModeContextTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MixedModeContextTests.cs index 9a10fbff8758d..62dbd491a92a2 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MixedModeContextTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/MixedModeContextTests.cs @@ -41,14 +41,26 @@ namespace System.Text.Json.SourceGeneration.Tests [JsonSerializable(typeof(ClassWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(StructWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(PersonStruct?), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] - [JsonSerializable(typeof(CovariantDerived), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] - [JsonSerializable(typeof(CovariantDerivedGeneric), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedNotIgnored), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnored), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedIgnored), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedGenericIgnored), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedNotIgnored), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedGenericNotIgnored), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedIgnored), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedGenericIgnored), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(IgnoredPropertyBase), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(NotIgnoredPropertyBase), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(IgnoredPropertyBase_NotIgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(NotIgnoredPropertyBase_IgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(NotIgnoredPropertyBase_NotIgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(IgnoredPropertyBase_IgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived), GenerationMode = JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization)] internal partial class MixedModeContext : JsonSerializerContext, ITestContext { public JsonSourceGenerationMode JsonSourceGenerationMode => JsonSourceGenerationMode.Metadata | JsonSourceGenerationMode.Serialization; @@ -96,14 +108,26 @@ public override void EnsureFastPathGeneratedAsExpected() Assert.Throws(() => MixedModeContext.Default.StructWithBadCustomConverter.SerializeHandler); Assert.Null(MixedModeContext.Default.NullablePersonStruct.SerializeHandler); Assert.NotNull(MixedModeContext.Default.PersonStruct.SerializeHandler); - Assert.Null(MixedModeContext.Default.CovariantDerived.SerializeHandler); - Assert.Null(MixedModeContext.Default.CovariantDerivedGenericString.SerializeHandler); + Assert.Null(MixedModeContext.Default.CovariantBaseNotIgnored_CovariantDerivedNotIgnored.SerializeHandler); + Assert.Null(MixedModeContext.Default.CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnoredString.SerializeHandler); + Assert.Null(MixedModeContext.Default.CovariantBaseNotIgnored_CovariantDerivedIgnored.SerializeHandler); + Assert.Null(MixedModeContext.Default.CovariantBaseNotIgnored_CovariantDerivedGenericIgnoredString.SerializeHandler); + Assert.Null(MixedModeContext.Default.CovariantBaseIgnored_CovariantDerivedNotIgnored.SerializeHandler); + Assert.Null(MixedModeContext.Default.CovariantBaseIgnored_CovariantDerivedGenericNotIgnoredString.SerializeHandler); + Assert.Null(MixedModeContext.Default.CovariantBaseIgnored_CovariantDerivedIgnored.SerializeHandler); + Assert.Null(MixedModeContext.Default.CovariantBaseIgnored_CovariantDerivedGenericIgnoredString.SerializeHandler); Assert.NotNull(MixedModeContext.Default.IgnoredPropertyBase.SerializeHandler); Assert.NotNull(MixedModeContext.Default.NotIgnoredPropertyBase.SerializeHandler); Assert.NotNull(MixedModeContext.Default.IgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); Assert.NotNull(MixedModeContext.Default.IgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); Assert.NotNull(MixedModeContext.Default.NotIgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); Assert.NotNull(MixedModeContext.Default.NotIgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); + Assert.NotNull(MixedModeContext.Default.IgnoredVirtualPropertyBase.SerializeHandler); + Assert.NotNull(MixedModeContext.Default.NotIgnoredVirtualPropertyBase.SerializeHandler); + Assert.NotNull(MixedModeContext.Default.IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived.SerializeHandler); + Assert.NotNull(MixedModeContext.Default.IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived.SerializeHandler); + Assert.NotNull(MixedModeContext.Default.NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived.SerializeHandler); + Assert.NotNull(MixedModeContext.Default.NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived.SerializeHandler); } [Fact] diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/RealWorldContextTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/RealWorldContextTests.cs index 31a26e0e67bb3..68bed9746cbac 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/RealWorldContextTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/RealWorldContextTests.cs @@ -884,43 +884,151 @@ public virtual void NullableStruct() } [Fact] - public void JsonIgnoreForCovariantProperties() + public void JsonIgnore_CovariantBaseNotIgnored_CovariantDerivedNotIgnored() { - var derived = new CovariantDerived(); + var derived = new CovariantBaseNotIgnored_CovariantDerivedNotIgnored(); if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) { - Assert.Throws(() => JsonSerializer.Serialize(derived, DefaultContext.CovariantDerived)); + Assert.Throws(() => JsonSerializer.Serialize(derived, DefaultContext.CovariantBaseNotIgnored_CovariantDerivedNotIgnored)); } else { - string json = JsonSerializer.Serialize(derived, DefaultContext.CovariantDerived); + string json = JsonSerializer.Serialize(derived, DefaultContext.CovariantBaseNotIgnored_CovariantDerivedNotIgnored); + JsonTestHelper.AssertJsonEqual("{\"Id\":\"CovariantBaseNotIgnored_CovariantDerivedNotIgnored\"}", json); + derived = JsonSerializer.Deserialize(json, DefaultContext.CovariantBaseNotIgnored_CovariantDerivedNotIgnored); + Assert.Equal("CovariantBaseNotIgnored_CovariantDerivedNotIgnored", derived.Id); + } + } + + [Fact] + public void JsonIgnore_CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnoredString() + { + var derived = new CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnored(); + if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) + { + Assert.Throws(() => JsonSerializer.Serialize(derived, DefaultContext.CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnoredString)); + } + else + { + string json = JsonSerializer.Serialize(derived, DefaultContext.CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnoredString); + JsonTestHelper.AssertJsonEqual("{\"Id\":\"CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnored\"}", json); + + derived = JsonSerializer.Deserialize(json, DefaultContext.CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnoredString); + Assert.Equal("CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnored", derived.Id); + } + } + + [Fact] + public void JsonIgnore_CovariantBaseNotIgnored_CovariantDerivedIgnored() + { + var derived = new CovariantBaseNotIgnored_CovariantDerivedIgnored(); + + if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) + { + Assert.Throws(() => JsonSerializer.Serialize(derived, DefaultContext.CovariantBaseNotIgnored_CovariantDerivedIgnored)); + } + else + { + string json = JsonSerializer.Serialize(derived, DefaultContext.CovariantBaseNotIgnored_CovariantDerivedIgnored); + JsonTestHelper.AssertJsonEqual("{\"Id\":\"CovariantBaseNotIgnored_CovariantDerivedIgnored\"}", json); + derived = JsonSerializer.Deserialize(json, DefaultContext.CovariantBaseNotIgnored_CovariantDerivedIgnored); + Assert.Equal("CovariantBaseNotIgnored_CovariantDerivedIgnored", derived.Id); + } + } + + [Fact] + public void JsonIgnore_CovariantBaseNotIgnored_CovariantDerivedGenericIgnoredString() + { + var derived = new CovariantBaseNotIgnored_CovariantDerivedGenericIgnored(); + if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) + { + Assert.Throws(() => JsonSerializer.Serialize(derived, DefaultContext.CovariantBaseNotIgnored_CovariantDerivedGenericIgnoredString)); + } + else + { + string json = JsonSerializer.Serialize(derived, DefaultContext.CovariantBaseNotIgnored_CovariantDerivedGenericIgnoredString); + JsonTestHelper.AssertJsonEqual("{\"Id\":\"CovariantBaseNotIgnored_CovariantDerivedGenericIgnored\"}", json); + + derived = JsonSerializer.Deserialize(json, DefaultContext.CovariantBaseNotIgnored_CovariantDerivedGenericIgnoredString); + Assert.Equal("CovariantBaseNotIgnored_CovariantDerivedGenericIgnored", derived.Id); + } + } + + [Fact] + public void JsonIgnore_CovariantBaseIgnored_CovariantDerivedNotIgnored() + { + var derived = new CovariantBaseIgnored_CovariantDerivedNotIgnored(); + + if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) + { + Assert.Throws(() => JsonSerializer.Serialize(derived, DefaultContext.CovariantBaseIgnored_CovariantDerivedNotIgnored)); + } + else + { + string json = JsonSerializer.Serialize(derived, DefaultContext.CovariantBaseIgnored_CovariantDerivedNotIgnored); + JsonTestHelper.AssertJsonEqual("{\"Id\":\"CovariantBaseIgnored_CovariantDerivedNotIgnored\"}", json); + derived = JsonSerializer.Deserialize(json, DefaultContext.CovariantBaseIgnored_CovariantDerivedNotIgnored); + Assert.Equal("CovariantBaseIgnored_CovariantDerivedNotIgnored", derived.Id); + } + } + + [Fact] + public void JsonIgnore_CovariantBaseIgnored_CovariantDerivedGenericNotIgnoredString() + { + var derived = new CovariantBaseIgnored_CovariantDerivedGenericNotIgnored(); + if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) + { + Assert.Throws(() => JsonSerializer.Serialize(derived, DefaultContext.CovariantBaseIgnored_CovariantDerivedGenericNotIgnoredString)); + } + else + { + string json = JsonSerializer.Serialize(derived, DefaultContext.CovariantBaseIgnored_CovariantDerivedGenericNotIgnoredString); + JsonTestHelper.AssertJsonEqual("{\"Id\":\"CovariantBaseIgnored_CovariantDerivedGenericNotIgnored\"}", json); + + derived = JsonSerializer.Deserialize(json, DefaultContext.CovariantBaseIgnored_CovariantDerivedGenericNotIgnoredString); + Assert.Equal("CovariantBaseIgnored_CovariantDerivedGenericNotIgnored", derived.Id); + } + } + + [Fact] + public void JsonIgnore_CovariantBaseIgnored_CovariantDerivedIgnored() + { + var derived = new CovariantBaseIgnored_CovariantDerivedIgnored(); + + if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) + { + Assert.Throws(() => JsonSerializer.Serialize(derived, DefaultContext.CovariantBaseIgnored_CovariantDerivedIgnored)); + } + else + { + string json = JsonSerializer.Serialize(derived, DefaultContext.CovariantBaseIgnored_CovariantDerivedIgnored); JsonTestHelper.AssertJsonEqual(@"{}", json); - derived = JsonSerializer.Deserialize(json, DefaultContext.CovariantDerived); - Assert.Null(derived.Id); + derived = JsonSerializer.Deserialize(json, DefaultContext.CovariantBaseIgnored_CovariantDerivedIgnored); + Assert.Equal("CovariantBaseIgnored_CovariantDerivedIgnored", derived.Id); } } [Fact] - public void JsonIgnoreForCovariantGenericProperties() + public void JsonIgnore_CovariantBaseIgnored_CovariantDerivedGenericIgnoredString() { - var derived = new CovariantDerivedGeneric(); + var derived = new CovariantBaseIgnored_CovariantDerivedGenericIgnored(); if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) { - Assert.Throws(() => JsonSerializer.Serialize(derived, DefaultContext.CovariantDerivedGenericString)); + Assert.Throws(() => JsonSerializer.Serialize(derived, DefaultContext.CovariantBaseIgnored_CovariantDerivedGenericIgnoredString)); } else { - string json = JsonSerializer.Serialize(derived, DefaultContext.CovariantDerivedGenericString); + string json = JsonSerializer.Serialize(derived, DefaultContext.CovariantBaseIgnored_CovariantDerivedGenericIgnoredString); JsonTestHelper.AssertJsonEqual(@"{}", json); - derived = JsonSerializer.Deserialize(json, DefaultContext.CovariantDerivedGenericString); - Assert.Null(derived.Id); + derived = JsonSerializer.Deserialize(json, DefaultContext.CovariantBaseIgnored_CovariantDerivedGenericIgnoredString); + Assert.Equal("CovariantBaseIgnored_CovariantDerivedGenericIgnored", derived.Id); } } [Fact] - public void JsonIgnoreForHiddenProperties_IgnoredBase_NotIgnoredDerived() + public void JsonIgnore_HiddenProperties_IgnoredBase_NotIgnoredDerived() { var derived = new IgnoredPropertyBase_NotIgnoredPropertyDerived { Id = "Test" }; @@ -940,12 +1048,12 @@ public void JsonIgnoreForHiddenProperties_IgnoredBase_NotIgnoredDerived() } [Fact] - public void JsonIgnoreForHiddenProperties_NotIgnoredBase_IgnoredDerived() + public void JsonIgnore_HiddenProperties_NotIgnoredBase_IgnoredDerived() { var derived = new NotIgnoredPropertyBase_IgnoredPropertyDerived { Id = "Test" }; string json = JsonSerializer.Serialize(derived, DefaultContext.NotIgnoredPropertyBase_IgnoredPropertyDerived); - JsonTestHelper.AssertJsonEqual("{\"Id\":null}", json); + JsonTestHelper.AssertJsonEqual("{\"Id\":\"NotIgnoredPropertyBase\"}", json); if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) { @@ -955,12 +1063,12 @@ public void JsonIgnoreForHiddenProperties_NotIgnoredBase_IgnoredDerived() else { derived = JsonSerializer.Deserialize(json, DefaultContext.NotIgnoredPropertyBase_IgnoredPropertyDerived); - Assert.Null(derived.Id); + Assert.Equal("NotIgnoredPropertyBase_IgnoredPropertyDerived", derived.Id); } } [Fact] - public void JsonIgnoreForHiddenProperties_IgnoredBase_IgnoredDerived() + public void JsonIgnore_HiddenProperties_IgnoredBase_IgnoredDerived() { var derived = new IgnoredPropertyBase_IgnoredPropertyDerived { Id = "Test" }; @@ -975,12 +1083,12 @@ public void JsonIgnoreForHiddenProperties_IgnoredBase_IgnoredDerived() else { derived = JsonSerializer.Deserialize(json, DefaultContext.IgnoredPropertyBase_IgnoredPropertyDerived); - Assert.Null(derived.Id); + Assert.Equal("IgnoredPropertyBase_IgnoredPropertyDerived", derived.Id); } } [Fact] - public void JsonIgnoreForHiddenProperties_NotIgnoredBase_NotIgnoredDerived() + public void JsonIgnore_HiddenProperties_NotIgnoredBase_NotIgnoredDerived() { var derived = new NotIgnoredPropertyBase_NotIgnoredPropertyDerived { Id = "Test" }; @@ -998,5 +1106,85 @@ public void JsonIgnoreForHiddenProperties_NotIgnoredBase_NotIgnoredDerived() Assert.Equal("Test", derived.Id); } } + + [Fact] + public void JsonIgnore_OverriddenProperties_IgnoredBase_NotIgnoredDerived() + { + var derived = new IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived { Id = "Test" }; + + string json = JsonSerializer.Serialize(derived, DefaultContext.IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived); + JsonTestHelper.AssertJsonEqual("{\"Id\":\"Test\"}", json); + + if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) + { + // Deserialization not supported in fast path serialization only mode + Assert.Throws(() => JsonSerializer.Deserialize(json, DefaultContext.IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived)); + } + else + { + derived = JsonSerializer.Deserialize(json, DefaultContext.IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived); + Assert.Equal("Test", derived.Id); + } + } + + [Fact] + public void JsonIgnore_OverriddenProperties_NotIgnoredBase_IgnoredDerived() + { + var derived = new NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived { Id = "Test" }; + + string json = JsonSerializer.Serialize(derived, DefaultContext.NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived); + JsonTestHelper.AssertJsonEqual("{}", json); + + if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) + { + // Deserialization not supported in fast path serialization only mode + Assert.Throws(() => JsonSerializer.Deserialize(json, DefaultContext.NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived)); + } + else + { + derived = JsonSerializer.Deserialize(json, DefaultContext.NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived); + Assert.Equal("NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived", derived.Id); + } + } + + [Fact] + public void JsonIgnore_OverriddenProperties_IgnoredBase_IgnoredDerived() + { + var derived = new IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived { Id = "Test" }; + + string json = JsonSerializer.Serialize(derived, DefaultContext.IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived); + JsonTestHelper.AssertJsonEqual(@"{}", json); + + if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) + { + // Deserialization not supported in fast path serialization only mode + Assert.Throws(() => JsonSerializer.Deserialize(json, DefaultContext.IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived)); + } + else + { + derived = JsonSerializer.Deserialize(json, DefaultContext.IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived); + Assert.Equal("IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived", derived.Id); + } + } + + [Fact] + public void JsonIgnore_OverriddenProperties_NotIgnoredBase_NotIgnoredDerived() + { + var derived = new NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived { Id = "Test" }; + + string json = JsonSerializer.Serialize(derived, DefaultContext.NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived); + JsonTestHelper.AssertJsonEqual("{\"Id\":\"Test\"}", json); + + if (DefaultContext.JsonSourceGenerationMode == JsonSourceGenerationMode.Serialization) + { + // Deserialization not supported in fast path serialization only mode + Assert.Throws(() => JsonSerializer.Deserialize(json, DefaultContext.NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived)); + } + else + { + derived = JsonSerializer.Deserialize(json, DefaultContext.NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived); + Assert.Equal("Test", derived.Id); + } + } } } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationContextTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationContextTests.cs index dda810daea77b..78c6d2a7be02a 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationContextTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/SerializationContextTests.cs @@ -41,14 +41,26 @@ namespace System.Text.Json.SourceGeneration.Tests [JsonSerializable(typeof(ClassWithBadCustomConverter))] [JsonSerializable(typeof(StructWithBadCustomConverter))] [JsonSerializable(typeof(PersonStruct?))] - [JsonSerializable(typeof(CovariantDerived))] - [JsonSerializable(typeof(CovariantDerivedGeneric))] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedNotIgnored))] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnored))] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedIgnored))] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedGenericIgnored))] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedNotIgnored))] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedGenericNotIgnored))] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedIgnored))] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedGenericIgnored))] [JsonSerializable(typeof(IgnoredPropertyBase))] [JsonSerializable(typeof(NotIgnoredPropertyBase))] [JsonSerializable(typeof(IgnoredPropertyBase_NotIgnoredPropertyDerived))] [JsonSerializable(typeof(NotIgnoredPropertyBase_IgnoredPropertyDerived))] [JsonSerializable(typeof(NotIgnoredPropertyBase_NotIgnoredPropertyDerived))] [JsonSerializable(typeof(IgnoredPropertyBase_IgnoredPropertyDerived))] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase))] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase))] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived))] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived))] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived))] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived))] internal partial class SerializationContext : JsonSerializerContext, ITestContext { public JsonSourceGenerationMode JsonSourceGenerationMode => JsonSourceGenerationMode.Serialization; @@ -88,14 +100,26 @@ internal partial class SerializationContext : JsonSerializerContext, ITestContex [JsonSerializable(typeof(ClassWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(StructWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(PersonStruct?), GenerationMode = JsonSourceGenerationMode.Serialization)] - [JsonSerializable(typeof(CovariantDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] - [JsonSerializable(typeof(CovariantDerivedGeneric), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedNotIgnored), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnored), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedIgnored), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedGenericIgnored), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedNotIgnored), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedGenericNotIgnored), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedIgnored), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedGenericIgnored), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(IgnoredPropertyBase), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(NotIgnoredPropertyBase), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(IgnoredPropertyBase_NotIgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(NotIgnoredPropertyBase_IgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(NotIgnoredPropertyBase_NotIgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(IgnoredPropertyBase_IgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] internal partial class SerializationWithPerTypeAttributeContext : JsonSerializerContext, ITestContext { public JsonSourceGenerationMode JsonSourceGenerationMode => JsonSourceGenerationMode.Serialization; @@ -136,14 +160,26 @@ internal partial class SerializationWithPerTypeAttributeContext : JsonSerializer [JsonSerializable(typeof(ClassWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(StructWithBadCustomConverter), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(PersonStruct?), GenerationMode = JsonSourceGenerationMode.Serialization)] - [JsonSerializable(typeof(CovariantDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] - [JsonSerializable(typeof(CovariantDerivedGeneric), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedNotIgnored), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnored), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedIgnored), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseNotIgnored_CovariantDerivedGenericIgnored), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedNotIgnored), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedGenericNotIgnored), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedIgnored), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(CovariantBaseIgnored_CovariantDerivedGenericIgnored), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(IgnoredPropertyBase), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(NotIgnoredPropertyBase), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(IgnoredPropertyBase_NotIgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(NotIgnoredPropertyBase_IgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(NotIgnoredPropertyBase_NotIgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] [JsonSerializable(typeof(IgnoredPropertyBase_IgnoredPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] + [JsonSerializable(typeof(IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived), GenerationMode = JsonSourceGenerationMode.Serialization)] internal partial class SerializationContextWithCamelCase : JsonSerializerContext, ITestContext { public JsonSourceGenerationMode JsonSourceGenerationMode => JsonSourceGenerationMode.Serialization; @@ -193,14 +229,26 @@ public override void EnsureFastPathGeneratedAsExpected() Assert.Throws(() => SerializationContext.Default.StructWithBadCustomConverter.SerializeHandler); Assert.Null(SerializationContext.Default.NullablePersonStruct.SerializeHandler); Assert.NotNull(SerializationContext.Default.PersonStruct.SerializeHandler); - Assert.Null(SerializationContext.Default.CovariantDerived.SerializeHandler); - Assert.Null(SerializationContext.Default.CovariantDerivedGenericString.SerializeHandler); + Assert.Null(SerializationContext.Default.CovariantBaseNotIgnored_CovariantDerivedNotIgnored.SerializeHandler); + Assert.Null(SerializationContext.Default.CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnoredString.SerializeHandler); + Assert.Null(SerializationContext.Default.CovariantBaseNotIgnored_CovariantDerivedIgnored.SerializeHandler); + Assert.Null(SerializationContext.Default.CovariantBaseNotIgnored_CovariantDerivedGenericIgnoredString.SerializeHandler); + Assert.Null(SerializationContext.Default.CovariantBaseIgnored_CovariantDerivedNotIgnored.SerializeHandler); + Assert.Null(SerializationContext.Default.CovariantBaseIgnored_CovariantDerivedGenericNotIgnoredString.SerializeHandler); + Assert.Null(SerializationContext.Default.CovariantBaseIgnored_CovariantDerivedIgnored.SerializeHandler); + Assert.Null(SerializationContext.Default.CovariantBaseIgnored_CovariantDerivedGenericIgnoredString.SerializeHandler); Assert.NotNull(SerializationContext.Default.IgnoredPropertyBase.SerializeHandler); Assert.NotNull(SerializationContext.Default.NotIgnoredPropertyBase.SerializeHandler); Assert.NotNull(SerializationContext.Default.IgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); Assert.NotNull(SerializationContext.Default.IgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); Assert.NotNull(SerializationContext.Default.NotIgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); Assert.NotNull(SerializationContext.Default.NotIgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); + Assert.NotNull(SerializationContext.Default.IgnoredVirtualPropertyBase.SerializeHandler); + Assert.NotNull(SerializationContext.Default.NotIgnoredVirtualPropertyBase.SerializeHandler); + Assert.NotNull(SerializationContext.Default.IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived.SerializeHandler); + Assert.NotNull(SerializationContext.Default.IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived.SerializeHandler); + Assert.NotNull(SerializationContext.Default.NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived.SerializeHandler); + Assert.NotNull(SerializationContext.Default.NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived.SerializeHandler); } [Fact] @@ -524,14 +572,26 @@ public override void EnsureFastPathGeneratedAsExpected() Assert.Throws(() => SerializationWithPerTypeAttributeContext.Default.StructWithBadCustomConverter.SerializeHandler); Assert.Null(SerializationWithPerTypeAttributeContext.Default.NullablePersonStruct.SerializeHandler); Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.PersonStruct.SerializeHandler); - Assert.Null(SerializationWithPerTypeAttributeContext.Default.CovariantDerived.SerializeHandler); - Assert.Null(SerializationWithPerTypeAttributeContext.Default.CovariantDerivedGenericString.SerializeHandler); + Assert.Null(SerializationWithPerTypeAttributeContext.Default.CovariantBaseNotIgnored_CovariantDerivedNotIgnored.SerializeHandler); + Assert.Null(SerializationWithPerTypeAttributeContext.Default.CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnoredString.SerializeHandler); + Assert.Null(SerializationWithPerTypeAttributeContext.Default.CovariantBaseNotIgnored_CovariantDerivedIgnored.SerializeHandler); + Assert.Null(SerializationWithPerTypeAttributeContext.Default.CovariantBaseNotIgnored_CovariantDerivedGenericIgnoredString.SerializeHandler); + Assert.Null(SerializationWithPerTypeAttributeContext.Default.CovariantBaseIgnored_CovariantDerivedNotIgnored.SerializeHandler); + Assert.Null(SerializationWithPerTypeAttributeContext.Default.CovariantBaseIgnored_CovariantDerivedGenericNotIgnoredString.SerializeHandler); + Assert.Null(SerializationWithPerTypeAttributeContext.Default.CovariantBaseIgnored_CovariantDerivedIgnored.SerializeHandler); + Assert.Null(SerializationWithPerTypeAttributeContext.Default.CovariantBaseIgnored_CovariantDerivedGenericIgnoredString.SerializeHandler); Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.IgnoredPropertyBase.SerializeHandler); Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.NotIgnoredPropertyBase.SerializeHandler); Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.IgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.IgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.NotIgnoredPropertyBase_NotIgnoredPropertyDerived.SerializeHandler); Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.NotIgnoredPropertyBase_IgnoredPropertyDerived.SerializeHandler); + Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.IgnoredVirtualPropertyBase.SerializeHandler); + Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.NotIgnoredVirtualPropertyBase.SerializeHandler); + Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived.SerializeHandler); + Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived.SerializeHandler); + Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived.SerializeHandler); + Assert.NotNull(SerializationWithPerTypeAttributeContext.Default.NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived.SerializeHandler); } } } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs index 0e1e9b294b1c9..dec93e2c1135d 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs @@ -159,55 +159,128 @@ public struct PersonStruct public string LastName { get; set; } } - public class CovariantBase + public class CovariantBaseNotIgnored + { + public virtual object Id { get; } = nameof(CovariantBaseNotIgnored); + } + + public class CovariantBaseNotIgnored_CovariantDerivedNotIgnored : CovariantBaseNotIgnored + { + public override string Id { get; } = nameof(CovariantBaseNotIgnored_CovariantDerivedNotIgnored); + } + + public class CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnored : CovariantBaseNotIgnored + where T : class + { + public override T Id { get; } = (T)(object)nameof(CovariantBaseNotIgnored_CovariantDerivedGenericNotIgnored); + } + + public class CovariantBaseNotIgnored_CovariantDerivedIgnored : CovariantBaseNotIgnored + { + [JsonIgnore] + public override string Id { get; } = nameof(CovariantBaseNotIgnored_CovariantDerivedIgnored); + } + + public class CovariantBaseNotIgnored_CovariantDerivedGenericIgnored : CovariantBaseNotIgnored + where T : class { [JsonIgnore] - public virtual object Id { get; } + public override T Id { get; } = (T)(object)nameof(CovariantBaseNotIgnored_CovariantDerivedGenericIgnored); } - public class CovariantDerived : CovariantBase + public class CovariantBaseIgnored { [JsonIgnore] - public override string Id { get; } + public virtual object Id { get; } = nameof(CovariantBaseIgnored); + } + + public class CovariantBaseIgnored_CovariantDerivedNotIgnored : CovariantBaseIgnored + { + public override string Id { get; } = nameof(CovariantBaseIgnored_CovariantDerivedNotIgnored); } - public class CovariantDerivedGeneric : CovariantBase + public class CovariantBaseIgnored_CovariantDerivedGenericNotIgnored : CovariantBaseIgnored where T : class + { + public override T Id { get; } = (T)(object)nameof(CovariantBaseIgnored_CovariantDerivedGenericNotIgnored); + } + + public class CovariantBaseIgnored_CovariantDerivedIgnored : CovariantBaseIgnored { [JsonIgnore] - public override T Id { get; } + public override string Id { get; } = nameof(CovariantBaseIgnored_CovariantDerivedIgnored); + } + + public class CovariantBaseIgnored_CovariantDerivedGenericIgnored : CovariantBaseIgnored + where T : class + { + [JsonIgnore] + public override T Id { get; } = (T)(object)nameof(CovariantBaseIgnored_CovariantDerivedGenericIgnored); } public class NotIgnoredPropertyBase { - public string Id { get; set; } + public string Id { get; set; } = nameof(NotIgnoredPropertyBase); } public class NotIgnoredPropertyBase_NotIgnoredPropertyDerived : NotIgnoredPropertyBase { - public new string Id { get; set; } + public new string Id { get; set; } = nameof(NotIgnoredPropertyBase_NotIgnoredPropertyDerived); } public class NotIgnoredPropertyBase_IgnoredPropertyDerived : NotIgnoredPropertyBase { [JsonIgnore] - public new string Id { get; set; } + public new string Id { get; set; } = nameof(NotIgnoredPropertyBase_IgnoredPropertyDerived); } public class IgnoredPropertyBase { [JsonIgnore] - public string Id { get; set; } + public string Id { get; set; } = nameof(IgnoredPropertyBase); } public class IgnoredPropertyBase_NotIgnoredPropertyDerived : IgnoredPropertyBase { - public new string Id { get; set; } + public new string Id { get; set; } = nameof(IgnoredPropertyBase_NotIgnoredPropertyDerived); } public class IgnoredPropertyBase_IgnoredPropertyDerived : IgnoredPropertyBase { [JsonIgnore] - public new string Id { get; set; } + public new string Id { get; set; } = nameof(IgnoredPropertyBase_IgnoredPropertyDerived); + } + + public class NotIgnoredVirtualPropertyBase + { + public virtual string Id { get; set; } = nameof(NotIgnoredVirtualPropertyBase); + } + + public class NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived : NotIgnoredVirtualPropertyBase + { + public override string Id { get; set; } = nameof(NotIgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived); + } + + public class NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived : NotIgnoredVirtualPropertyBase + { + [JsonIgnore] + public override string Id { get; set; } = nameof(NotIgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived); + } + + public class IgnoredVirtualPropertyBase + { + [JsonIgnore] + public virtual string Id { get; set; } = nameof(IgnoredVirtualPropertyBase); + } + + public class IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived : IgnoredVirtualPropertyBase + { + public override string Id { get; set; } = nameof(IgnoredVirtualPropertyBase_NotIgnoredOverriddenPropertyDerived); + } + + public class IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived : IgnoredVirtualPropertyBase + { + [JsonIgnore] + public override string Id { get; set; } = nameof(IgnoredVirtualPropertyBase_IgnoredOverriddenPropertyDerived); } }