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
System.Text.Json: DefaultJsonTypeInfoResolver throws on CreateJsonTypeInfo
for interfaces with duplicate properties
#101673
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
The contract customization angle is a red herring in this case, the error surfaces from the fact that JsonSerializer.Serialize<IA>(new CA());
public interface IA1 { string A { get; set; } }
public interface IA2 { string A { get; set; } }
public interface IA : IA1, IA2 { }
public class CA : IA { public string A { get; set; } = "!"; } STJ requires that you disambiguate between the two properties, either by changing the name for one of them, changing it via a |
But if name and type matches, why not just allow that definition? Imho that'd be totally fine. If they don't it could still throw that exception. |
Because they could be pointing to two separate implementations. This is valid for instance: public class CA : IA
{
string IA1.A { get; set; } = "foo";
string IA2.A { get; set; } = "bar";
} Should the resultant JSON property have value |
Ok, valid argument. You even needed to compare the value making things way too complicated / untransparent. But why is my work-around with a |
Because in this case the new |
Description
In my scenario I need to deserialize interface types. I use some kind of mapping what default type gets used for what interface. Now I get an exception, if several interfaces define the same property. Imho this should be fine, as long as types etc. match.
Even if not, some kind of handler (via override) would be helpful. Right now I'd need to implement a whole new
IJsonTypeInfoResolver
.Reproduction Steps
Expected behavior
Make it work on some type. Or allow some kind of error handling. (Btw. this is working with Json.Net out of the box. But not sure how this case exactly is handled internal).
Actual behavior
Throws an
ThrowInvalidOperationException_SerializerPropertyNameConflict
.Regression?
No response
Known Workarounds
Repeat the property on the top-most interface:
public interface IA : IA1, IA2 { new string A { get; set; } }
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: