Repository navigation
Description
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
jeffhandley commentedon Jul 25, 2025
Thank you for submitting this, @martincostello. I'm marking it as a regression blocking the release.
eiriktsarpalis commentedon Jul 30, 2025
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.
eiriktsarpalis commentedon Jul 30, 2025
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.eiriktsarpalis commentedon Jul 30, 2025
Cosing in favor of #73255.