OpenAPITools / OpenAPITools/openapi-generator

[BUG][CSHARP][genericHost] Discriminator Mapping is ignored by Generator when using custom Mapping Names

Open
#20,059 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

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.

  1. 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

  1. in WriteProperties of ContractData a 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 compile

If 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.

  1. 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: true
Steps to reproduce

Use the provided configuration file and swagger.yaml to generate the code. Compile the generated csharp project.

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/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.

Suggest a fix

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the generated C# project with the linked swagger.yaml, openapi.config.yaml, and the genericHost configuration. Inspect the generated ContractData discriminator, WriteProperties, ReadProperties, and derived classes to trace how custom mappings are handled. Done means the generated project compiles and uses the configured mapping values such as "CAR" rather than schema names.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.