-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Serializing type which hides inherited property with both properties ignored throws ArgumentException #66900
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsDescriptionWith System.Text.Json when trying to serialize a class that is inheriting from a class and is using the new keyword for a property and the new property and the hidden property return different types that has the same name (e.g. "Class1") and both properties are set to [JsonIgnore] an exception is thrown with the following message: An item with the same key has already been added. Key: class1 It looks like there are actual two problems: First that two types cannot have the same name apparently. Second that it only looks at both types when both properties are set to [JsonIgnore]. All other combinations seems to work. I have identified the issue in both .Net 6 and .Net 7 Preview 2. Reproduction StepsRun following code as a Console app in .Net 6 or 7: `using System.Text.Json.Serialization; Console.WriteLine("Hello, World!"); MyNamespace.Class2 class2 = new MyNamespace.Class2(); string jsonText; try Console.WriteLine(jsonText); namespace MyNamespace
} namespace MyNamespaceBase
}` Expected behaviorShould not throw exception. Should serialize the object. Actual behaviorFollowing exception is thrown: System.ArgumentException: An item with the same key has already been added. Key: class1 Regression?I have identified the issue in both .Net 6 and .Net 7 Preview 2. I have not tested other versions. Known WorkaroundsNo response ConfigurationNo response Other informationNo response
|
I'm putting it in 7.0.0 milestone for now so it doesn't disappear from our radar. I do not promise it will be fixed in this timeline though. We have a set of issues related to JsonIgnore and this is another one. |
Description
With System.Text.Json when trying to serialize a class that is inheriting from a class and is using the new keyword for a property and the new property and the hidden property return different types that has the same name (e.g. "Class1") and both properties are set to [JsonIgnore] an exception is thrown with the following message:
An item with the same key has already been added. Key: class1
It looks like there are actual two problems:
First that two types cannot have the same name apparently.
Second that it only looks at both types when both properties are set to [JsonIgnore]. All other combinations seems to work.
I have identified the issue in both .Net 6 and .Net 7 Preview 2.
Reproduction Steps
Run following code as a Console app in .Net 6 or 7:
Expected behavior
Should not throw exception. Should serialize the object.
Actual behavior
Following exception is thrown:
Regression?
I have identified the issue in both .Net 6 and .Net 7 Preview 2. I have not tested other versions.
Known Workarounds
No response
Configuration
No response
Other information
No response
EDIT @krwq: fixed code formatting and call stack
The text was updated successfully, but these errors were encountered: