Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -639,6 +639,7 @@ internal JsonTypeInfo? AncestorPolymorphicType
get
{
Debug.Assert(IsConfigured);
Debug.Assert(Type != typeof(object));

if (!_isAncestorPolymorphicTypeResolved)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public static bool IsSupportedDerivedType(Type baseType, Type? derivedType) =>
// First, walk up the class hierarchy for any supported types.
for (Type? candidate = typeInfo.Type.BaseType; candidate != null; candidate = candidate.BaseType)
{
JsonTypeInfo? candidateInfo = typeInfo.Options.GetTypeInfoInternal(candidate, ensureNotNull: null);
JsonTypeInfo? candidateInfo = ResolveAncestorTypeInfo(candidate, typeInfo.Options);
if (candidateInfo?.PolymorphismOptions != null)
{
// stop on the first ancestor that has a match
Expand All @@ -264,7 +264,7 @@ public static bool IsSupportedDerivedType(Type baseType, Type? derivedType) =>
// Now, walk the interface hierarchy for any polymorphic interface declarations.
foreach (Type interfaceType in typeInfo.Type.GetInterfaces())
{
JsonTypeInfo? candidateInfo = typeInfo.Options.GetTypeInfoInternal(interfaceType, ensureNotNull: null);
JsonTypeInfo? candidateInfo = ResolveAncestorTypeInfo(interfaceType, typeInfo.Options);
if (candidateInfo?.PolymorphismOptions != null)
{
if (matchingResult != null)
Expand Down Expand Up @@ -294,6 +294,20 @@ public static bool IsSupportedDerivedType(Type baseType, Type? derivedType) =>
}

return matchingResult;

static JsonTypeInfo? ResolveAncestorTypeInfo(Type type, JsonSerializerOptions options)
{
try
{
return options.GetTypeInfoInternal(type, ensureNotNull: null);
}
catch
{
// The resolver produced an exception when resolving the ancestor type.
// Eat the exception and report no result instead.
return null;
}
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,36 @@ public async Task AnonymousType()
Assert.Equal(Expected, json);
}

[Fact]
public async Task CustomResolverWithFailingAncestorType_DoesNotSurfaceException()
{
var options = new JsonSerializerOptions
{
TypeInfoResolver = new DefaultJsonTypeInfoResolver
{
Modifiers =
{
static typeInfo =>
{
if (typeInfo.Type == typeof(MyThing) ||
typeInfo.Type == typeof(IList))
{
throw new InvalidOperationException("some latent custom resolution bug");
}
}
}
}
};

object value = new MyDerivedThing { Number = 42 };
string json = await Serializer.SerializeWrapper(value, options);
Assert.Equal("""{"Number":42}""", json);

value = new int[] { 1, 2, 3 };
json = await Serializer.SerializeWrapper(value, options);
Assert.Equal("[1,2,3]", json);
}

class MyClass
{
public string Value { get; set; }
Expand All @@ -535,6 +565,10 @@ class MyThing : IThing
public int Number { get; set; }
}

class MyDerivedThing : MyThing
{
}

class MyThingCollection : List<IThing> { }

class MyThingDictionary : Dictionary<string, IThing> { }
Expand Down