Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserializing JSON with properties that differ in casing #109768

Closed
MartyIX opened this issue Nov 13, 2024 · 6 comments · Fixed by #109786
Closed

Deserializing JSON with properties that differ in casing #109768

MartyIX opened this issue Nov 13, 2024 · 6 comments · Fixed by #109786
Assignees
Labels
area-System.Text.Json bug in-pr There is an active PR which will close this issue when it is merged
Milestone

Comments

@MartyIX
Copy link
Contributor

MartyIX commented Nov 13, 2024

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.

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 13, 2024
Copy link
Contributor

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

@eiriktsarpalis
Copy link
Member

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

@eiriktsarpalis eiriktsarpalis added bug and removed untriaged New issue has not been triaged by the area owner labels Nov 13, 2024
@eiriktsarpalis eiriktsarpalis added this to the 9.0.x milestone Nov 13, 2024
@eiriktsarpalis eiriktsarpalis self-assigned this Nov 13, 2024
@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Nov 13, 2024
@omni-htg
Copy link

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!

@eiriktsarpalis
Copy link
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.

@MartyIX
Copy link
Contributor Author

MartyIX commented Dec 12, 2024

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

@eiriktsarpalis
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Text.Json bug in-pr There is an active PR which will close this issue when it is merged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants