-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Attribute foo
used in [JsonPolymorphic(TypeDiscriminatorPropertyName = "foo")]
does not get deserialized
#79933
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionI'm deserializing the followed JSON structure:
For this I have build my polymorphic objects as follows: [JsonPolymorphic(TypeDiscriminatorPropertyName = "resultType")]
[JsonDerivedType(typeof(Data))]
[JsonDerivedType(typeof(MatrixData), typeDiscriminator: "matrix")]
[JsonDerivedType(typeof(VectorData), typeDiscriminator: "vector")]
public class Data
{
[JsonPropertyName("resultType")]
public string resultType { get; set; }
}
public class VectorData : Data
{
// ...
}
public class MatrixData : Data
{
// ...
} The deserialization including polymorphism all works great, what does not work is the property So, I'm wondering if its use in Reproduction StepsSee above Expected behaviorAny field that is being used for JsonPolymorphic should still be able to be deserialized Actual behaviorRegression?No response Known WorkaroundsNo response Configuration.NET SDK: Runtime Environment: Other informationNo response
|
This came up recently but I cannot find the issue. We had a similar problem. The fact it has a setter implies you can change the underlying type by changing the value, which wouldn't make sense. We were using the property for introspection without a type-check and originally resolved it by putting JsonIgnore on the property (to prevent double serialization) and turned it into an abstract, get-only auto-property that returned the expected value by type, which is a variation on what was recommended by dotnet folks. We ended up just dropping the property (which was entirely a convenience anyways) and just perform the type check ourself now. |
yeah its not a super hard requirement for me to have this, either, but it just felt like a bug that it's always null. If that is indeed the desired behavior, it should at least be documented on |
… and metadata free algorithms.
This is by design, the type discriminator identifies types and so cannot be mapped to a run time property value. |
Description
I'm deserializing the followed JSON structure:
For this I have build my polymorphic objects as follows:
The deserialization including polymorphism all works great, what does not work is the property
resultType
. It is alwaysnull
.So, I'm wondering if its use in
[JsonPolymorphic(TypeDiscriminatorPropertyName = "resultType")]
is messing with the deserialization of that field?!Reproduction Steps
See above
Expected behavior
Any field that is being used for JsonPolymorphic should still be able to be deserialized
Actual behavior
Field is null
Regression?
No response
Known Workarounds
No response
Configuration
.NET SDK:
Version: 7.0.101
Commit: bb24aafa11
Runtime Environment:
OS Name: Windows
OS Version: 10.0.22623
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.101\
Other information
No response
The text was updated successfully, but these errors were encountered: