Skip to content

Cannot use Properties with $-character when reading System.Text.Json metadata. #79482

Closed

Description

@queequac

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 $), here any further property using a leading $-character cannot be used.

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

  1. queequac commented on Dec 9, 2022

    @queequac
    Author

    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.

  2. ghost added
    untriaged
    on Dec 10, 2022
  3. gregsdennis commented on Dec 10, 2022

    @gregsdennis
    Contributor

    Might be a more general case of #79059

  4. eiriktsarpalis commented on Dec 12, 2022

    @eiriktsarpalis
    Member

    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.

  5. added
    enhancement
    and removed
    untriaged
    on Dec 12, 2022
  6. changed the title [-][Minimal APIs] Cannot use Properties with $-character with JsonPolymorphism[/-] [+]Cannot use Properties with $-character when reading System.Text.Json metadata.[/+] on Dec 12, 2022
  7. added this to the Future milestone on Dec 12, 2022
  8. ckpearson commented on Aug 5, 2024

    @ckpearson

    Just ran into this issue myself, we're using JsonPolymorphic which by-default uses $type as the discriminator property, but also built an auto-versioning layer on top where a $version property 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.

  9. added
    in-pr
    on Oct 14, 2026
  10. added a commit that references this issue on Oct 21, 2026
    6828eb2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.Jsonenhancementin-pr

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions