Repository navigation
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
My json documents contain fields with leading $-character. In general this is no issue, I can add a [JsonProperty("$something")] to the property and it's serialized correctly.
In some cases I need polymorphism. In this case the minimal API is breaking.
While the new JsonPolymorphic feature works as expected by defining some discriminator (which can have a leading
Sample:
public class Root
{
[JsonPropertyName("$more")]
public Base More { get; set; }
}
[JsonPolymorphic]
[JsonDerivedType(typeof(A), "a")]
[JsonDerivedType(typeof(B), "b")]
public class Base { }
public class A : Base
{
[JsonPropertyName("$dollar")]
public string Dollar { get; set; }
}
public class B : Base
{
[JsonPropertyName("cents")]
public string Dollar { get; set; }
}
I am using the following minimal API:
app.MapPost("/root", (HttpContext context, [FromBody] Root content) =>
{
// Do something.
}).WithName("PostReferences").WithTags("Anchor Core Service");
Deserializing the following works fine:
{ "$more": { "$type": "b", "cents": "foo" }
But this does not:
{ "$more": { "$type": "a", "$dollar": "foo" }
Here the delegate is not even called, but the endpoint returns error 500.
Whenever a property with a leading dollar sign is passed within the polymorophic class, it breaks. On the non polymorphic it is no problem (see Root)
This seems to be related to the fact that dollars are interpreted as metadata and polymorphism is using metadata for the discriminator... I found some JsonSerializerSettings that would allow to mess arround with MetadataPropertyHandling, but seems that's nothing I can set for Minimal API in .NET 7.
Any suggestions how I could still deserialize the document I get passed with dollars in this case?
Expected Behavior
Deserializing should allow dollar signs on polymorphic classes just like on non polymorphic ones.
Steps To Reproduce
No response
Exceptions (if any)
The minimal API returns error code 500 with the following message:
Internal error. System error or malformed request. (Failed to read parameter "Root content" from the request body as JSON.)
.NET Version
7
Anything else?
No response
Activity
queequac commentedon Dec 9, 2022
Seems to be related to System.Text.Json:
System.Text.Json.JsonException: Properties that start with '$' are not allowed in types that support metadata. Either escape the character or disable reference preservation and polymorphic deserialization.Turning off polymorphism is not an option, since I cannot solve my issue then... and in case of escaping I am not sure what is meant, but most likely I don't have the dollar then anymore.
gregsdennis commentedon Dec 10, 2022
Might be a more general case of #79059
eiriktsarpalis commentedon Dec 12, 2022
Yes, this is a by-design restriction of the STJ metadata reader. Further investigation is needed to determine the original motivations and whether they are still valid, but we should definitely look at relaxing this in the future.
[-][Minimal APIs] Cannot use Properties with $-character with JsonPolymorphism[/-][+]Cannot use Properties with $-character when reading System.Text.Json metadata.[/+]ckpearson commentedon Aug 5, 2024
Just ran into this issue myself, we're using
JsonPolymorphicwhich by-default uses$typeas the discriminator property, but also built an auto-versioning layer on top where a$versionproperty also gets inserted; thankfully still at a stage during development where the version property can be renamed, but I don't recall there being a warning about this behaviour in the documentation about polymorphic JSON.Allow non-metadata JSON property names with a leading dollar sign (do…