Skip to content

Deserializing JSON with properties that differ in casing #109768

Description

@MartyIX

Description

It appears there is a regression in STJ when one migrates from .NET 8 to .NET 9 when one wants to deserialize JSON where there are two properties that differ in casing (e.g. [JsonPropertyName("a")] and [JsonPropertyName("A")]).

Reproduction Steps

Put the following console app code in Program.cs and run it using dotnet run:

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

internal class Program
{
    private static void Main(string[] args)
    {
        JsonSerializerOptions options = new() {
            PropertyNameCaseInsensitive  = false,
        };

        var box = JsonSerializer.Deserialize<Box>("""{"a": "Texas", "A": "California"}""", options);
    }
}

public class Box 
{
    [JsonPropertyName("a")]
    public string From { get; set; }

    [JsonPropertyName("A")]
    public string To { get; set; }

    [JsonConstructor]
    public Box(string from, string to)
    {
        From = from;
        To = to;
    }
}

Expected behavior

No exception should be thrown as in .NET 8.

Actual behavior

Unhandled exception. System.InvalidOperationException: Members 'A' and 'a' on type 'Box' cannot both bind with parameter 'to' in the deserialization constructor.
   at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_MultiplePropertiesBindToConstructorParameters(Type parentType, String parameterName, String firstMatchName, String secondMatchName)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.ConfigureConstructorParameters()
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.Configure()
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.<EnsureConfigured>g__ConfigureSynchronized|172_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) in /Users/Net9StjBug/Program.cs:line 13

Regression?

Yes. This works well in .NET 8.

Known Workarounds

I couldn't find any.

Configuration

.NET 9 GA

The issue does not appear to be platform specific.

Other information

I'm not sure if this issue is related to #108790 or not. It's the most similar issue I could find.

Activity

  1. added
    untriaged
    on Nov 13, 2024
  2. dotnet-policy-service commented on Nov 13, 2024

    @dotnet-policy-service
    Contributor

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

  3. eiriktsarpalis commented on Nov 13, 2024

    @eiriktsarpalis
    Member

    Thanks, I can confirm this is a regression introduced by 455f540. We will be backporting a fix as soon as possible.

  4. added and removed
    untriaged
    on Nov 13, 2024
  5. added this to the 9.0.x milestone on Nov 13, 2024
  6. added
    in-pr
    on Nov 13, 2024
  7. omni-htg commented on Nov 26, 2024

    @omni-htg

    Hello team!
    Out of curiosity, what is going to happen now? Is it necessary to wait for the next point release for 9.0?
    If that is the case, what happens with the 9.0.0 NuGet package? Does it get invalidated somehow, or could it get replaced in-place with the fixed version?
    Thanks!

  8. eiriktsarpalis commented on Nov 26, 2024

    @eiriktsarpalis
    Member

    The fix will be included in the upcoming servicing release which I believe happens in January. An updated 9.0.1 NuGet package will also be pushed on the same date.

  9. MartyIX commented on Dec 12, 2024

    @MartyIX
    ContributorAuthor

    Why is this issue assigned to 9.0.x milestone and not 9.0.1 or 9.0.2?

  10. eiriktsarpalis commented on Dec 12, 2024

    @eiriktsarpalis
    Member

    We don't track separate milestones for each servicing release.

  11. MartyIX commented on Dec 12, 2024

    @MartyIX
    ContributorAuthor

    Unfortunately, I don't understand what it practically means. My main question is if this issue will be fixed in 9.0.1, 9.0.2 or later :)

  12. MartyIX commented on Dec 12, 2024

    @MartyIX
    ContributorAuthor

    To clarify that, there are:

    So it looks to me that there are milestones for patch releases.

  13. eiriktsarpalis commented on Dec 13, 2024

    @eiriktsarpalis
    Member

    As mentioned, it will be part of the 9.0.1 update.

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

Metadata

Metadata

Labels

area-System.Text.Jsonbugin-pr

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions