Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
Expand Down Expand Up @@ -1078,14 +1078,14 @@ public async Task NestedPolymorphicClassesIncreaseReadAndWriteStackWhenNeeded()

[JsonDerivedType(typeof(TestNodeList), "NodeList")]
[JsonDerivedType(typeof(TestLeaf), "Leaf")]
abstract class TestNode
public abstract class TestNode
{
public string Name { get; set; }

public abstract void AssertEqualTo(TestNode other);
}

class TestNodeList : TestNode
public class TestNodeList : TestNode
{
public string Info { get; set; }

Expand Down Expand Up @@ -1118,7 +1118,7 @@ public override void AssertEqualTo(TestNode other)
}
}

class TestLeaf : TestNode
public class TestLeaf : TestNode
{
public string? Test { get; set; }

Expand Down Expand Up @@ -2480,6 +2480,12 @@ public class PolymorphicClassWithoutDerivedTypeAttribute
[Fact]
public async Task PolymorphicClassWithNullDerivedTypeAttribute_ThrowsInvalidOperationException()
{
// Generator NRE: registering this type triggers an unhandled NullReferenceException
// in JsonSourceGenerator.Parser.cs (the JsonDerivedType ctor arg is dereferenced
// without a null check). No SYSLIB diagnostic is emitted, so the SG path cannot
// reach the runtime InvalidOperationException. Validated under reflection only
// pending a generator fix.
if (Serializer.IsSourceGeneratedSerializer) return;
var value = new PolymorphicClassWithNullDerivedTypeAttribute();
await Assert.ThrowsAsync<InvalidOperationException>(() => Serializer.SerializeWrapper(value));
}
Expand Down Expand Up @@ -2659,6 +2665,8 @@ public class DerivedClass : PolymorphicGenericClass<T>
[Fact]
public async Task PolymorphicDerivedGenericClass_ThrowsInvalidOperationException()
{
// Source generator cannot emit a usable JsonTypeInfo for this invalid polymorphic configuration (build-time SYSLIB diagnostic or generator-side rejection), so the runtime InvalidOperationException is validated under reflection only.
if (Serializer.IsSourceGeneratedSerializer) return;
Comment thread
eiriktsarpalis marked this conversation as resolved.
Resolved
PolymorphicDerivedGenericClass value = new PolymorphicDerivedGenericClass.DerivedClass<int>();
await Assert.ThrowsAsync<InvalidOperationException>(() => Serializer.SerializeWrapper(value));
}
Expand Down Expand Up @@ -2845,6 +2853,8 @@ public async Task OpenGenericDerivedType_DifferentTypeArguments_ProduceDifferent
[Fact]
public async Task OpenGenericDerivedType_NonGenericBase_ThrowsInvalidOperationException()
{
// Source generator cannot emit a usable JsonTypeInfo for this invalid polymorphic configuration (build-time SYSLIB diagnostic or generator-side rejection), so the runtime InvalidOperationException is validated under reflection only.
if (Serializer.IsSourceGeneratedSerializer) return;
var value = new NonGenericBaseWithOpenGenericDerived();
await Assert.ThrowsAsync<InvalidOperationException>(() => Serializer.SerializeWrapper(value));
}
Expand All @@ -2858,6 +2868,8 @@ public class OpenDerived<T> : NonGenericBaseWithOpenGenericDerived;
[Fact]
public async Task OpenGenericDerivedType_TypeArgsNotResolvable_ThrowsInvalidOperationException()
{
// Source generator cannot emit a usable JsonTypeInfo for this invalid polymorphic configuration (build-time SYSLIB diagnostic or generator-side rejection), so the runtime InvalidOperationException is validated under reflection only.
if (Serializer.IsSourceGeneratedSerializer) return;
// Derived<T> : Base<int> - T cannot be determined from Base<int>
var value = new OpenGenericBase_Unresolvable<int>();
await Assert.ThrowsAsync<InvalidOperationException>(() => Serializer.SerializeWrapper(value));
Expand All @@ -2874,6 +2886,8 @@ public class OpenGenericDerived_Unresolvable<T> : OpenGenericBase_Unresolvable<i
[Fact]
public async Task OpenGenericDerivedType_GroundMismatchAgainstClosedBase_ThrowsInvalidOperationException()
{
// Source generator cannot emit a usable JsonTypeInfo for this invalid polymorphic configuration (build-time SYSLIB diagnostic or generator-side rejection), so the runtime InvalidOperationException is validated under reflection only.
if (Serializer.IsSourceGeneratedSerializer) return;
// OpenGenericDerived_GroundMismatch<T> : OpenGenericBase_GroundMismatch<T, int>
// registered on OpenGenericBase_GroundMismatch<int, string>.
// Position 0 (T) unifies with int, but position 1 (concrete int in derived's base
Expand Down Expand Up @@ -3177,6 +3191,8 @@ public class OpenGenericDerived_Tuple<T1, T2> : OpenGenericBase_Tuple<(T1, T2)>
[Fact]
public async Task OpenGenericDerivedType_AmbiguousInterfaceMatch_ThrowsInvalidOperationException()
{
// Source generator cannot emit a usable JsonTypeInfo for this invalid polymorphic configuration (build-time SYSLIB diagnostic or generator-side rejection), so the runtime InvalidOperationException is validated under reflection only.
if (Serializer.IsSourceGeneratedSerializer) return;
// Impl<T> : IBase<T>, IBase<List<T>> registered on IBase<List<int>>.
// Both ancestors unify (T=List<int> via the first interface, T=int via the second).
// Result: ambiguous, throws.
Expand All @@ -3192,6 +3208,8 @@ public class OpenGenericImpl_Ambiguous<T> : IOpenGenericBase_Ambiguous<T>, IOpen
[Fact]
public async Task OpenGenericDerivedType_UnboundParameter_ThrowsInvalidOperationException()
{
// Source generator cannot emit a usable JsonTypeInfo for this invalid polymorphic configuration (build-time SYSLIB diagnostic or generator-side rejection), so the runtime InvalidOperationException is validated under reflection only.
if (Serializer.IsSourceGeneratedSerializer) return;
// Derived<T1, T2> : Base<T1> — T2 is unspeakable (not bound by the base type's args).
var value = new OpenGenericBase_Unbound<int>();
await Assert.ThrowsAsync<InvalidOperationException>(() => Serializer.SerializeWrapper(value));
Expand All @@ -3205,6 +3223,8 @@ public class OpenGenericDerived_Unbound<T1, T2> : OpenGenericBase_Unbound<T1>;
[Fact]
public async Task OpenGenericDerivedType_ConstraintViolation_ThrowsInvalidOperationException()
{
// Source generator cannot emit a usable JsonTypeInfo for this invalid polymorphic configuration (build-time SYSLIB diagnostic or generator-side rejection), so the runtime InvalidOperationException is validated under reflection only.
if (Serializer.IsSourceGeneratedSerializer) return;
// Derived<T> : Base<T> where T : struct, registered on Base<string>.
// Constraint fails → InvalidOperationException.
var value = new OpenGenericBase_StructConstraint<string>();
Expand Down Expand Up @@ -3242,6 +3262,8 @@ public async Task OpenGenericDerivedType_DuplicateClosedAndOpenRegistration_Thro
// Derived<> registration. The open form closes to Derived<int>, producing a
// duplicate derived-type registration. The existing dup-detection in
// PolymorphicTypeResolver must surface this as InvalidOperationException.
// (Source generator accepts the configuration without warning; the runtime resolver
// catches the duplicate, so this scenario validates the same runtime path under both engines.)
OpenGenericBase_DuplicateDerivedRegistrations<int> value = new OpenGenericDerived_DuplicateDerivedRegistrations<int>();
await Assert.ThrowsAsync<InvalidOperationException>(() => Serializer.SerializeWrapper(value));
}
Expand Down Expand Up @@ -3494,6 +3516,8 @@ public async Task Variance_BivariantInterface_BothViaVariance_DefaultThrows()
[Fact]
public async Task NestedGeneric_EnclosingMismatch_ThrowsInvalidOperationException()
{
// Source generator cannot emit a usable JsonTypeInfo for this invalid polymorphic configuration (build-time SYSLIB diagnostic or generator-side rejection), so the runtime InvalidOperationException is validated under reflection only.
if (Serializer.IsSourceGeneratedSerializer) return;
// Pattern: NestedDerivedEnclosingMismatch<T> : NestedBase<NestedOuter<int>.NestedBox<T>>.
// Target: NestedBase<NestedOuter<string>.NestedBox<int>>.
// The enclosing argument differs (int vs string) so unification MUST fail. The
Expand Down Expand Up @@ -3788,9 +3812,10 @@ public bool Equals(TBaseType? left, TBaseType? right)
public int GetHashCode(TBaseType _) => throw new NotImplementedException();
}

public class CustomPolymorphismResolver : DefaultJsonTypeInfoResolver
public class CustomPolymorphismResolver : IJsonTypeInfoResolver
{
private List<JsonDerivedType> _jsonDerivedTypes = new();
private readonly DefaultJsonTypeInfoResolver _inner = new();
private readonly List<JsonDerivedType> _jsonDerivedTypes = new();

public CustomPolymorphismResolver(Type baseType)
{
Expand All @@ -3808,10 +3833,10 @@ public CustomPolymorphismResolver WithDerivedType(JsonDerivedType jsonDerivedTyp
return this;
}

public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
public JsonTypeInfo? GetTypeInfo(Type type, JsonSerializerOptions options)
{
JsonTypeInfo jsonTypeInfo = base.GetTypeInfo(type, options);
if (jsonTypeInfo.Type == BaseType)
JsonTypeInfo? jsonTypeInfo = _inner.GetTypeInfo(type, options);
if (jsonTypeInfo is not null && jsonTypeInfo.Type == BaseType)
{
jsonTypeInfo.PolymorphismOptions = new()
{
Expand Down
Loading
Loading