Open EelcoLos opened 8 months ago
Is the following the property that you want to assign the discriminator value to?
public EDocumentItemType DocumentItemType { get; }
System.Text.Json, by design, cannot map discriminator values to runtime properties.
There is an open proposal, however.
The System.Text.Json docs explicitly state:
Avoid using a JsonPolymorphicAttribute.TypeDiscriminatorPropertyName that conflicts with a property in your type hierarchy.
Effectively this issue is blocked until dotnet/runtime#91274 is implemented. Until then enum-valued type discriminators with System.Text.Json are not possible.
Is the following the property that you want to assign the discriminator value to?
public EDocumentItemType DocumentItemType { get; }
System.Text.Json, by design, cannot map discriminator values to runtime properties.
There is an open proposal, however.
The System.Text.Json docs explicitly state:
Avoid using a JsonPolymorphicAttribute.TypeDiscriminatorPropertyName that conflicts with a property in your type hierarchy.
Effectively this issue is blocked until dotnet/runtime#91274 is implemented. Until then enum-valued type discriminators with System.Text.Json are not possible.
Thanks @schnerring for replying 🙏
In NSwag13, and in .Net 7 this is supported. I do agree on your reading that the TypeDiscriminatorPropertyName
should not have the property in type hierarchy, though why make this possible then, with no Roslyn warning whatsoever.
We moved to this opportunity in .Net7, seeing it was available and working for us. We're using this polymorphism in a storage solution where that solution is now expecting a number to be stored, refering the enum
. Hence the DerivedType needs to be a number for the storage solution to work.
Here (https://github.com/RicoSuter/NJsonSchema/blob/3585d60e949e43284601e0bea16c33de4c6c21f5/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonInheritanceTests.cs#L79) it is only tested that the JsonDerivedType
is a string. The integer option is not tested
The other solution is trying to revert back to our custom deserializer middleware.
In NSwag13, and in .Net 7 this is supported. I do agree on your reading that the
TypeDiscriminatorPropertyName
should not have the property in type hierarchy, though why make this possible then, with no Roslyn warning whatsoever.
That would probably be a good issue. ;)
it is only tested that the JsonDerivedType is a string. The integer option is not tested
I'll have to rebase my STJ PR https://github.com/RicoSuter/NJsonSchema/pull/1595, and will have a look if it's possible to support integers, too.
and will have a look if it's possible to support integers, too.
I'm not 100% sure, but I think we could support integer type discriminators using OpenAPI specification extensions, which is how it's currently done for enums when using Newtonsoft.Json.
By default, the discriminator mapping field is of type Map[string, string]
.
In NSwag13, and in .Net 7 this is supported. I do agree on your reading that the
TypeDiscriminatorPropertyName
should not have the property in type hierarchy, though why make this possible then, with no Roslyn warning whatsoever.That would probably be a good issue. ;)
it is only tested that the JsonDerivedType is a string. The integer option is not tested
I'll have to rebase my STJ PR #1595, and will have a look if it's possible to support integers, too.
since you've created a new PR, did you check it in https://github.com/RicoSuter/NJsonSchema/pull/1675 too?
No, it was out of scope for that PR.
just tested this with 14.1.0/11.0.2 and this problem nswag version and this problem persists.
Expected Behavior
A derived class with a custom
TypeDiscriminator
property name, which is based on anenum
(therefore the cast will be of typeint
) will be cast properly in nswagin NSwag 13.2.0, this is generated properly:
Actual Behavior
when this is trying to be generated with nswag 14, this is resulting in the following error:
Steps to Reproduce the Problem
the test here (https://github.com/RicoSuter/NJsonSchema/blob/3585d60e949e43284601e0bea16c33de4c6c21f5/src/NJsonSchema.Tests/Generation/SystemTextJson/SystemTextJsonInheritanceTests.cs#L80) notes the test being with string derived types, while enums generally are with integers
Specifications