Skip to content

Commit eedb9df

Browse files
eiriktsarpalisCopilotCopilot
committed
Consolidate STJ polymorphism tests into a shared abstract suite (#129755)
Test-only refactoring to expand test coverage ahead of #129041 being implemented. Moves `PolymorphicTests.CustomTypeHierarchies.cs` and `PolymorphicTests.TypeClassifier.cs` from `System.Text.Json.Tests/Serialization/` into `tests/Common/` so they run under both reflection (11 wrappers) and source-gen (4 wrappers) via the `SerializerWrapper` pattern, removes the duplicate `PolymorphismTests.cs` in the source-gen test project, and wires the consolidated suite through a new context file under `System.Text.Json.SourceGeneration.Tests/Serialization/PolymorphicTests.cs`. The test-only nature should make this straightforward to review. --------- Co-authored-by: eiriktsarpalis <eiriktsarpalis@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 988f8c9 commit eedb9df

9 files changed

Lines changed: 1591 additions & 1110 deletions

File tree

src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/PolymorphicTests.CustomTypeHierarchies.cs renamed to src/libraries/System.Text.Json/tests/Common/PolymorphicTests.CustomTypeHierarchies.cs

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections;
@@ -9,6 +9,7 @@
99
using System.Text.Encodings.Web;
1010
using System.Text.Json.Serialization.Metadata;
1111
using System.Threading.Tasks;
12+
using Microsoft.DotNet.XUnitExtensions;
1213
using Xunit;
1314

1415
namespace System.Text.Json.Serialization.Tests
@@ -1078,14 +1079,14 @@ public async Task NestedPolymorphicClassesIncreaseReadAndWriteStackWhenNeeded()
10781079

10791080
[JsonDerivedType(typeof(TestNodeList), "NodeList")]
10801081
[JsonDerivedType(typeof(TestLeaf), "Leaf")]
1081-
abstract class TestNode
1082+
public abstract class TestNode
10821083
{
10831084
public string Name { get; set; }
10841085

10851086
public abstract void AssertEqualTo(TestNode other);
10861087
}
10871088

1088-
class TestNodeList : TestNode
1089+
public class TestNodeList : TestNode
10891090
{
10901091
public string Info { get; set; }
10911092

@@ -1118,7 +1119,7 @@ public override void AssertEqualTo(TestNode other)
11181119
}
11191120
}
11201121

1121-
class TestLeaf : TestNode
1122+
public class TestLeaf : TestNode
11221123
{
11231124
public string? Test { get; set; }
11241125

@@ -2477,9 +2478,18 @@ public class PolymorphicClassWithoutDerivedTypeAttribute
24772478
{
24782479
}
24792480

2480-
[Fact]
2481+
[ConditionalFact]
24812482
public async Task PolymorphicClassWithNullDerivedTypeAttribute_ThrowsInvalidOperationException()
24822483
{
2484+
if (Serializer.IsSourceGeneratedSerializer)
2485+
{
2486+
// The JsonDerivedType ctor arg is dereferenced without a null check in
2487+
// JsonSourceGenerator.Parser.cs, so the generator throws NRE rather than emitting
2488+
// a SYSLIB diagnostic. Pending a generator fix, the runtime InvalidOperationException
2489+
// is validated under reflection only.
2490+
throw new SkipTestException("Generator NRE on [JsonDerivedType(null)] prevents reaching the runtime path.");
2491+
}
2492+
24832493
var value = new PolymorphicClassWithNullDerivedTypeAttribute();
24842494
await Assert.ThrowsAsync<InvalidOperationException>(() => Serializer.SerializeWrapper(value));
24852495
}
@@ -2656,9 +2666,14 @@ public class DerivedClass : PolymorphicGenericClass<T>
26562666
}
26572667
}
26582668

2659-
[Fact]
2669+
[ConditionalFact]
26602670
public async Task PolymorphicDerivedGenericClass_ThrowsInvalidOperationException()
26612671
{
2672+
if (Serializer.IsSourceGeneratedSerializer)
2673+
{
2674+
throw new SkipTestException("Source generator rejects this invalid polymorphic configuration at build time (SYSLIB diagnostic); the runtime InvalidOperationException is validated under reflection only.");
2675+
}
2676+
26622677
PolymorphicDerivedGenericClass value = new PolymorphicDerivedGenericClass.DerivedClass<int>();
26632678
await Assert.ThrowsAsync<InvalidOperationException>(() => Serializer.SerializeWrapper(value));
26642679
}
@@ -2842,9 +2857,14 @@ public async Task OpenGenericDerivedType_DifferentTypeArguments_ProduceDifferent
28422857
JsonTestHelper.AssertJsonEqual("""{"$type":"derived","Value":"hello"}""", strJson);
28432858
}
28442859

2845-
[Fact]
2860+
[ConditionalFact]
28462861
public async Task OpenGenericDerivedType_NonGenericBase_ThrowsInvalidOperationException()
28472862
{
2863+
if (Serializer.IsSourceGeneratedSerializer)
2864+
{
2865+
throw new SkipTestException("Source generator rejects this invalid polymorphic configuration at build time (SYSLIB diagnostic); the runtime InvalidOperationException is validated under reflection only.");
2866+
}
2867+
28482868
var value = new NonGenericBaseWithOpenGenericDerived();
28492869
await Assert.ThrowsAsync<InvalidOperationException>(() => Serializer.SerializeWrapper(value));
28502870
}
@@ -2855,9 +2875,14 @@ public class NonGenericBaseWithOpenGenericDerived
28552875
public class OpenDerived<T> : NonGenericBaseWithOpenGenericDerived;
28562876
}
28572877

2858-
[Fact]
2878+
[ConditionalFact]
28592879
public async Task OpenGenericDerivedType_TypeArgsNotResolvable_ThrowsInvalidOperationException()
28602880
{
2881+
if (Serializer.IsSourceGeneratedSerializer)
2882+
{
2883+
throw new SkipTestException("Source generator rejects this invalid polymorphic configuration at build time (SYSLIB diagnostic); the runtime InvalidOperationException is validated under reflection only.");
2884+
}
2885+
28612886
// Derived<T> : Base<int> - T cannot be determined from Base<int>
28622887
var value = new OpenGenericBase_Unresolvable<int>();
28632888
await Assert.ThrowsAsync<InvalidOperationException>(() => Serializer.SerializeWrapper(value));
@@ -2871,9 +2896,14 @@ public class OpenGenericBase_Unresolvable<T>
28712896

28722897
public class OpenGenericDerived_Unresolvable<T> : OpenGenericBase_Unresolvable<int>;
28732898

2874-
[Fact]
2899+
[ConditionalFact]
28752900
public async Task OpenGenericDerivedType_GroundMismatchAgainstClosedBase_ThrowsInvalidOperationException()
28762901
{
2902+
if (Serializer.IsSourceGeneratedSerializer)
2903+
{
2904+
throw new SkipTestException("Source generator rejects this invalid polymorphic configuration at build time (SYSLIB diagnostic); the runtime InvalidOperationException is validated under reflection only.");
2905+
}
2906+
28772907
// OpenGenericDerived_GroundMismatch<T> : OpenGenericBase_GroundMismatch<T, int>
28782908
// registered on OpenGenericBase_GroundMismatch<int, string>.
28792909
// Position 0 (T) unifies with int, but position 1 (concrete int in derived's base
@@ -3174,9 +3204,14 @@ public class OpenGenericDerived_Tuple<T1, T2> : OpenGenericBase_Tuple<(T1, T2)>
31743204
public (T1, T2) Pair { get; set; }
31753205
}
31763206

3177-
[Fact]
3207+
[ConditionalFact]
31783208
public async Task OpenGenericDerivedType_AmbiguousInterfaceMatch_ThrowsInvalidOperationException()
31793209
{
3210+
if (Serializer.IsSourceGeneratedSerializer)
3211+
{
3212+
throw new SkipTestException("Source generator rejects this invalid polymorphic configuration at build time (SYSLIB diagnostic); the runtime InvalidOperationException is validated under reflection only.");
3213+
}
3214+
31803215
// Impl<T> : IBase<T>, IBase<List<T>> registered on IBase<List<int>>.
31813216
// Both ancestors unify (T=List<int> via the first interface, T=int via the second).
31823217
// Result: ambiguous, throws.
@@ -3189,9 +3224,14 @@ public interface IOpenGenericBase_Ambiguous<T>;
31893224

31903225
public class OpenGenericImpl_Ambiguous<T> : IOpenGenericBase_Ambiguous<T>, IOpenGenericBase_Ambiguous<List<T>>;
31913226

3192-
[Fact]
3227+
[ConditionalFact]
31933228
public async Task OpenGenericDerivedType_UnboundParameter_ThrowsInvalidOperationException()
31943229
{
3230+
if (Serializer.IsSourceGeneratedSerializer)
3231+
{
3232+
throw new SkipTestException("Source generator rejects this invalid polymorphic configuration at build time (SYSLIB diagnostic); the runtime InvalidOperationException is validated under reflection only.");
3233+
}
3234+
31953235
// Derived<T1, T2> : Base<T1> — T2 is unspeakable (not bound by the base type's args).
31963236
var value = new OpenGenericBase_Unbound<int>();
31973237
await Assert.ThrowsAsync<InvalidOperationException>(() => Serializer.SerializeWrapper(value));
@@ -3202,9 +3242,14 @@ public class OpenGenericBase_Unbound<T>;
32023242

32033243
public class OpenGenericDerived_Unbound<T1, T2> : OpenGenericBase_Unbound<T1>;
32043244

3205-
[Fact]
3245+
[ConditionalFact]
32063246
public async Task OpenGenericDerivedType_ConstraintViolation_ThrowsInvalidOperationException()
32073247
{
3248+
if (Serializer.IsSourceGeneratedSerializer)
3249+
{
3250+
throw new SkipTestException("Source generator rejects this invalid polymorphic configuration at build time (SYSLIB diagnostic); the runtime InvalidOperationException is validated under reflection only.");
3251+
}
3252+
32083253
// Derived<T> : Base<T> where T : struct, registered on Base<string>.
32093254
// Constraint fails → InvalidOperationException.
32103255
var value = new OpenGenericBase_StructConstraint<string>();
@@ -3242,6 +3287,8 @@ public async Task OpenGenericDerivedType_DuplicateClosedAndOpenRegistration_Thro
32423287
// Derived<> registration. The open form closes to Derived<int>, producing a
32433288
// duplicate derived-type registration. The existing dup-detection in
32443289
// PolymorphicTypeResolver must surface this as InvalidOperationException.
3290+
// (Source generator accepts the configuration without warning; the runtime resolver
3291+
// catches the duplicate, so this scenario validates the same runtime path under both engines.)
32453292
OpenGenericBase_DuplicateDerivedRegistrations<int> value = new OpenGenericDerived_DuplicateDerivedRegistrations<int>();
32463293
await Assert.ThrowsAsync<InvalidOperationException>(() => Serializer.SerializeWrapper(value));
32473294
}
@@ -3491,9 +3538,14 @@ public async Task Variance_BivariantInterface_BothViaVariance_DefaultThrows()
34913538
// behavior. The reflection side has always handled these cases correctly because
34923539
// Type.GetGenericArguments() returns enclosing+leaf args together.
34933540

3494-
[Fact]
3541+
[ConditionalFact]
34953542
public async Task NestedGeneric_EnclosingMismatch_ThrowsInvalidOperationException()
34963543
{
3544+
if (Serializer.IsSourceGeneratedSerializer)
3545+
{
3546+
throw new SkipTestException("Source generator rejects this invalid polymorphic configuration at build time (SYSLIB diagnostic); the runtime InvalidOperationException is validated under reflection only.");
3547+
}
3548+
34973549
// Pattern: NestedDerivedEnclosingMismatch<T> : NestedBase<NestedOuter<int>.NestedBox<T>>.
34983550
// Target: NestedBase<NestedOuter<string>.NestedBox<int>>.
34993551
// The enclosing argument differs (int vs string) so unification MUST fail. The
@@ -3788,9 +3840,10 @@ public bool Equals(TBaseType? left, TBaseType? right)
37883840
public int GetHashCode(TBaseType _) => throw new NotImplementedException();
37893841
}
37903842

3791-
public class CustomPolymorphismResolver : DefaultJsonTypeInfoResolver
3843+
public class CustomPolymorphismResolver : IJsonTypeInfoResolver
37923844
{
3793-
private List<JsonDerivedType> _jsonDerivedTypes = new();
3845+
private readonly DefaultJsonTypeInfoResolver _inner = new();
3846+
private readonly List<JsonDerivedType> _jsonDerivedTypes = new();
37943847

37953848
public CustomPolymorphismResolver(Type baseType)
37963849
{
@@ -3808,10 +3861,10 @@ public CustomPolymorphismResolver WithDerivedType(JsonDerivedType jsonDerivedTyp
38083861
return this;
38093862
}
38103863

3811-
public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
3864+
public JsonTypeInfo? GetTypeInfo(Type type, JsonSerializerOptions options)
38123865
{
3813-
JsonTypeInfo jsonTypeInfo = base.GetTypeInfo(type, options);
3814-
if (jsonTypeInfo.Type == BaseType)
3866+
JsonTypeInfo? jsonTypeInfo = _inner.GetTypeInfo(type, options);
3867+
if (jsonTypeInfo is not null && jsonTypeInfo.Type == BaseType)
38153868
{
38163869
jsonTypeInfo.PolymorphismOptions = new()
38173870
{

0 commit comments

Comments
 (0)