Created by: KrzysztofLabus-RDX
This PR fixes two issues with csharp code generator:
- When
enum
type is used as discriminator property autogenerated constructors of derived types use string representation of discriminators rather concrete enum value. - Polymorphic Json.NET serialization configures
JsonSubtypes
converter with CLR property name discriminator value rather JSON property name.
Sample Schema
MyDiscriminatorType:
type: string
enum:
- Abc
- Def
MyBaseType:
type: object
required:
- some_discriminator
properties:
some_discriminator:
$ref: '#/components/schemas/MyDiscriminatorType'
discriminator:
propertyName: some_discriminator
mapping:
# NOTE: These need to match MyDiscriminatorType options above
Abc: '#/components/schemas/MyAbc'
Def: '#/components/schemas/MyDef'
MyAbc:
allOf:
- $ref: '#/components/schemas/MyBaseType'
- type: object
required:
- my_abc_prop
properties:
my_abc_prop:
type: string
MyDef:
allOf:
- $ref: '#/components/schemas/MyBaseType'
- type: object
required:
- my_def_prop
properties:
my_def_prop:
type: boolean
Generated Types (simplified)
// all good
public enum MyDiscriminatorType
{
Abc = 1,
Def = 2,
}
[JsonConverter(typeof(JsonSubtypes), "SomeDiscriminator")] // some_discriminator expected
[JsonSubtypes.KnownSubType(typeof(MyAbc), "Abc")]
[JsonSubtypes.KnownSubType(typeof(MyDef), "Def")]
[JsonSubtypes.KnownSubType(typeof(MyAbc), "MyAbc")]
[JsonSubtypes.KnownSubType(typeof(MyDef), "MyDef")]
public partial class MyBaseType : IEquatable<MyBaseType>
{
// all good
public MyBaseType(MyDiscriminatorType someDiscriminator = default(MyDiscriminatorType))
{
// ...
}
}
[DataContract(Name = "MyAbc")]
[JsonConverter(typeof(JsonSubtypes), "SomeDiscriminator")] // some_discriminator expected
[JsonSubtypes.KnownSubType(typeof(MyAbc), "Abc")]
[JsonSubtypes.KnownSubType(typeof(MyDef), "Def")]
public partial class MyAbc : MyBaseType, IEquatable<MyAbc>
{
// compile-time error, MyDiscriminatorType someDiscriminator = MyDiscriminatorType.Abc expected
public MyAbc(string myAbcProp = default(string), MyDiscriminatorType someDiscriminator = "MyAbc") : base(someDiscriminator)
{
// ...
}
}
@mandrean @frankyjuang @shibayan @Blackclaws @lucamazzanti