Skip to content

System.Text.Json throws InvalidOperationException deserializing a polymorphic type which overrides an ignored property #118061

Closed

Description

@martincostello

Description

I have a test suite that uses polymorphic deserialization to handle various JSON payloads that can be returned for AWS CloudWatch log events.

The base class I use has an abstract property named Type marked with [JsonIgnore] that is used for the type discriminator. The derived classes then override this property to return the string associated with the specific concrete types.

In .NET 9, these classes can be deserialized.

In .NET 10 preview 6 a InvalidOperationException is thrown instead and deserialization fails:

Unhandled exception. System.InvalidOperationException: The type 'PlatformInitEvent' contains property 'type' that conflicts with an existing metadata property name. Consider either renaming it or ignoring it with JsonIgnoreAttribute.
   at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_PropertyConflictsWithMetadataPropertyName(Type type, String propertyName)
   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.GetTypeInfo(Type type)

Reproduction Steps

Run the following program using the .NET 10 preview 6 SDK:

#:property TargetFramework=net10.0

using System.Text.Json;
using System.Text.Json.Serialization;

string json = """
{
  "type": "platform.initStart",
  "time": "2023-10-01T12:00:00Z"
}
""";

var model = JsonSerializer.Deserialize(json, PlatformJsonSerializationContext.Default.PlatformEvent);

Console.WriteLine($"Type: {model?.Type}");

internal sealed class PlatformInitEvent : PlatformEvent
{
    public override string Type => "platform.initStart";
}

[JsonDerivedType(typeof(PlatformInitEvent), "platform.initStart")]
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
internal abstract class PlatformEvent
{
    [JsonPropertyName("time")]
    public DateTime Timestamp { get; set; }

    [JsonIgnore]
    public abstract string Type { get; }
}

[JsonSerializable(typeof(PlatformEvent))]
[JsonSourceGenerationOptions(AllowOutOfOrderMetadataProperties = true, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
internal sealed partial class PlatformJsonSerializationContext : JsonSerializerContext;

Expected behavior

The program outputs Type: platform.initStart, as .NET 9 does.

Actual behavior

The program throws an InvalidOperationException.

Regression?

From .NET 9, yes.

Known Workarounds

Add [JsonIgnore] to all the overridden properties.

Configuration

.NET SDK 10.0.100-preview.6.25358.103

Other information

No response

Activity

  1. added
    untriaged
    on Jul 25, 2025
  2. jeffhandley commented on Jul 25, 2025

    @jeffhandley
    Member

    Thank you for submitting this, @martincostello. I'm marking it as a regression blocking the release.

  3. added this to the 10.0.0 milestone on Jul 25, 2025
  4. removed
    untriaged
    on Jul 25, 2025
  5. eiriktsarpalis commented on Jul 30, 2025

    @eiriktsarpalis
    Member

    This is an intentional breaking change that got introduced by #106460. I think we could tweak this so that it doesn't throw in cases where the conflicting property is being ignored.

  6. eiriktsarpalis commented on Jul 30, 2025

    @eiriktsarpalis
    Member

    So, I dug a bit further and it seems that the change is already accounting for ignored properties. However, your particular repro is hitting an unrelated bug related to attributing virtual properties. You can work around the issue by applying a [JsonIgnore] on the overrridden property as well.

  7. eiriktsarpalis commented on Jul 30, 2025

    @eiriktsarpalis
    Member

    Cosing in favor of #73255.

  8. locked and limited conversation to collaborators on Aug 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions