Repository navigation
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
When using discriminators in a schema with a enum descriminator property and custom mapping (because the mapping name does not match the schema name) the generated code does not compile.
- The base schema (
ContractData) containing the discriminator mapping has issues because it assigns a string to the required Enum Discriminator:
/// <summary>
/// The discriminator
/// </summary>
[JsonIgnore]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public ContractTypeEnum ContractType { get; } = "ContractData";In my opinion this class should be abstract with an abstract enum Discriminator
- in WriteProperties of
ContractDataa line does not compile regarding the discriminator:
public void WriteProperties(Utf8JsonWriter writer, ContractData contractData, JsonSerializerOptions jsonSerializerOptions)
{
if (contractData.Contract == null)
throw new ArgumentNullException(nameof(contractData.Contract), "Property is required for class ContractData.");
if (contractData.Customer == null)
throw new ArgumentNullException(nameof(contractData.Customer), "Property is required for class ContractData.");
if (contractData.Sectors == null)
throw new ArgumentNullException(nameof(contractData.Sectors), "Property is required for class ContractData.");
if (contractData.PositionsOption.IsSet && contractData.Positions == null)
throw new ArgumentNullException(nameof(contractData.Positions), "Property is required for class ContractData.");
writer.WriteString("contractType", ); // <-- This line does not compileIf the Base Schema is made abstract, WriteProperties and ReadProperties could be omitted in my opinion, which would solve that issue automatically. If it is not removed, in ReadProperties it would need to decide returning CarContractData or PropertyContractData based on the derived type. In WriteProperties it would have to return the overwritten value of the Discriminator Enum of the derived classes.
- The derived classes are ignoring the names for the mappings:
/// <summary>
/// The discriminator
/// </summary>
[JsonIgnore]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public new ContractTypeEnum ContractType { get; } = (ContractTypeEnum)Enum.Parse(typeof(ContractTypeEnum), "CarContractData");It tries to parse "CarContractData" into ContractTypeEnum but instead it should try to parse "CAR". Same problem exists for the PropertyContractData
openapi-generator version
7.9.0 and latest main branch
OpenAPI declaration file content or url
https://gist.github.com/rima1098/2800239f70774d0ca48c61bbf2f4893a
Generation Details
generate -i swagger.yaml -g csharp -c openapi.config.yaml
with the following config contents:
targetFramework: net8.0
library: generichost
netCoreProjectFile: true
sourceFolder: _generated
useNewtonsoft: false
nullableReferenceTypes: true
useDateTimeOffset: true
useOneOfDiscriminatorLookup: trueSteps to reproduce
Use the provided configuration file and swagger.yaml to generate the code. Compile the generated csharp project.
Related issues/PRs
#19691
This issue describes a similar problem (1. and 2. of my Problem description). The difference is that the Enum does contain the schema names and therefore the 3. problem of my description does not occur.
Activity