Skip to content

JsonSerializer throws NotSupportedException when previously threw InvalidOperationException for duplicate JsonPolymorphismOptions,DerivedTypes #121333

@ericstj

Description

@ericstj

Description

A type was registered twice, or a type was registered with the same discriminator as some other type.

The same type is also missing from source gen registration and JsonSerializerIsReflectionEnabledByDefault is set to false.

Previously the InvalidOperationException would be thrown.

Reproduction Steps

jsonRepro.zip

Run this project in net10.0 and net9.0

Expected behavior

Same exception as before:

Unhandled exception. System.InvalidOperationException: The polymorphic type 'Microsoft.Extensions.AI.AIContent' has already specified derived type 'DerivedAIContent'.
   at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_DerivedTypeIsAlreadySpecified(Type baseType, Type derivedType)
   at System.Text.Json.Serialization.Metadata.PolymorphicTypeResolver..ctor(JsonSerializerOptions options, JsonPolymorphismOptions polymorphismOptions, Type baseType, Boolean converterCanHaveMetadata)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.Configure()
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.<EnsureConfigured>g__ConfigureSynchronized|172_0()
   at System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type type, Boolean ensureConfigured, Nullable`1 ensureNotNull, Boolean resolveIfMutable, Boolean fallBackToNearestAncestorType)
   at System.Text.Json.JsonSerializerOptions.GetTypeInfoForRootType(Type type, Boolean fallBackToNearestAncestorType)
   at System.Text.Json.JsonSerializer.GetTypeInfo[T](JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
   at Program.<Main>$(String[] args) in C:\scratch\jsonRepro\Program.cs:line 10

Actual behavior

Failure due to missing metadata

Unhandled exception. System.NotSupportedException: JsonTypeInfo metadata for type 'DerivedAIContent' was not provided by TypeInfoResolver of type 'System.Text.Json.Serialization.Metadata.JsonTypeInfoResolverWithAddedModifiers'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException_NoMetadataForType(Type type, IJsonTypeInfoResolver resolver)
   at System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type type, Boolean ensureConfigured, Nullable`1 ensureNotNull, Boolean resolveIfMutable, Boolean fallBackToNearestAncestorType)
   at System.Text.Json.Serialization.Metadata.PolymorphicTypeResolver..ctor(JsonSerializerOptions options, JsonPolymorphismOptions polymorphismOptions, Type baseType, Boolean converterCanHaveMetadata)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.Configure()
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.<EnsureConfigured>g__ConfigureSynchronized|174_0()
   at System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type type, Boolean ensureConfigured, Nullable`1 ensureNotNull, Boolean resolveIfMutable, Boolean fallBackToNearestAncestorType)
   at System.Text.Json.JsonSerializerOptions.GetTypeInfoForRootType(Type type, Boolean fallBackToNearestAncestorType)
   at System.Text.Json.JsonSerializer.GetTypeInfo[T](JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
   at Program.<Main>$(String[] args) in C:\scratch\jsonRepro\Program.cs:line 11

Regression?

Yes, though it's just exception ordering. The other exception would have been hit.

It was regressed in this change -- 60fc78e#diff-c0c6a6581f2a19df70177a8be2af804b0c09915cde3ab47841281fdf6aeadf77R45 cc @eiriktsarpalis

This was caught by a test in dotnet/extensions#6995 (comment)

Known Workarounds

Register all types that you want to use for serialization.

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions