Repository navigation
Labels
Milestone
Description
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
dotnet-policy-service commentedon Nov 13, 2024
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.
eiriktsarpalis commentedon Nov 13, 2024
Thanks, I can confirm this is a regression introduced by 455f540. We will be backporting a fix as soon as possible.
omni-htg commentedon Nov 26, 2024
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 commentedon Nov 26, 2024
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 commentedon Dec 12, 2024
Why is this issue assigned to 9.0.x milestone and not 9.0.1 or 9.0.2?
eiriktsarpalis commentedon Dec 12, 2024
We don't track separate milestones for each servicing release.
MartyIX commentedon Dec 12, 2024
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 :)
MartyIX commentedon Dec 12, 2024
To clarify that, there are:
So it looks to me that there are milestones for patch releases.
eiriktsarpalis commentedon Dec 13, 2024
As mentioned, it will be part of the 9.0.1 update.