Skip to content

Property name conflicts with discriminator even when property is [JsonIgnore] #123431

Open

Description

@stephentoub

Description

When polymorphism is being used, if the type discriminator and a property on the C# type have the same name, even if that property is [JsonIgnore], deserialization fails.

Reproduction Steps

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

Console.WriteLine(JsonSerializer.Deserialize<Base>(
    """{"type":"derived"}""", 
    JsonSerializerOptions.Web));

[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
[JsonDerivedType(typeof(Derived), "derived")]
abstract class Base
{
    [JsonPropertyName("type")]
    [JsonIgnore]
    public abstract string Type { get; }
}

class Derived : Base
{
    public override string Type => "derived";
}

Expected behavior

Succeeds

Actual behavior

Unhandled exception. System.InvalidOperationException: The type 'Derived' 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.GetTypeInfoForRootType(Type type, Boolean fallBackToNearestAncestorType)
   at System.Text.Json.JsonSerializer.GetTypeInfo[T](JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
   at Program.<Main>$(String[] args)

Regression?

No

Known Workarounds

No workarounds known, other than choosing a different C# property name.

Configuration

.NET 10

Other information

No response

Activity

  1. added
    untriaged
    on Jan 21, 2026
  2. added and removed
    untriaged
    on Jan 21, 2026
  3. dotnet-policy-service commented on Jan 21, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-text-json
    See info in area-owners.md if you want to be subscribed.

  4. stephentoub commented on Jan 21, 2026

    @stephentoub
    MemberAuthor

    I just realized this is another inheritance issue. If I put [JsonIgnore] on all the overrides, then it succeeds.

  5. added this to the Future milestone on Jan 21, 2026
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions