OpenAPITools / OpenAPITools/openapi-generator

[BUG][CSHARP] JsonConverter for oneOf WriteProperties is empty

Open
#23,693 7 comments 0 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

Description

oneOf generates no code in the WriteProperties method of the JsonConverter class.

Read method is fine, and populates the properties correctly. Read/Write code generated for other classes appears correct.

It appears to be related to the (optional) discriminator in the oneOf section. When the discriminator is specified, the write code is generated, but when it is commented out, the write methods do not serialise the items.

As I understand it, the discriminator property is optional.

openapi-generator version

7.22.0

OpenAPI declaration file
openapi: 3.0.3
info:
  title: OneOf Bug
  version: 1.0.0

paths:
  /resources:
    post:
      summary: Create a resource
      operationId: createResource
      responses:
        '201':
          description: Resource created successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Results'
        '400':
          description: Invalid request

components:
  schemas:
    Results:
      type: object
      required:
        - id
        - entry
      properties:
        id:
          type: string
        entry:
          type: array
          items:
            oneOf:
              - $ref: '#/components/schemas/Diagnostic'
              - $ref: '#/components/schemas/Observation'
              - $ref: '#/components/schemas/Communication'
            # discriminator:
            #   propertyName: resourceType
            #   mapping:
            #     Diagnostic: '#/components/schemas/Diagnostic'
            #     Observation: '#/components/schemas/Observation'
            #     Communication: '#/components/schemas/Communication'
            minItems: 3
            maxItems: 3

    Diagnostic:
      type: object
      required:
        - resourceType
        - id
      properties:
        resourceType:
          type: string
          enum: [Diagnostic]
        id:
          type: string
          format: uuid

    Observation:
      type: object
      required:
        - resourceType
        - id
      properties:
        resourceType:
          type: string
          enum: [Observation]
        id:
          type: string
          format: uuid

    Communication:
      type: object
      required:
        - resourceType
        - id
      properties:
        resourceType:
          type: string
          enum: [Communication]
        id:
          type: string
          format: uuid

Generation Details

npx @openapitools/openapi-generator-cli generate

openapitools.json

{
  "$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
  "spaces": 2,
  "generator-cli": {
    "version": "7.22.0",
    "generators": {
      "oneOfBug": {
        "generatorName": "csharp",
        "inputSpec": "oneOfBug.yml",
        "output": "generated/",
        "globalProperty": {
          "models": "",
          "modelDocs": "false",
          "modelTests": "false",
          "apis": "false",
          "apiDocs": "false",
          "apiTests": "false"
        },
        "additionalProperties": {
          "packageName": "Models",
          "targetFramework": "net10.0",
          "library": "generichost",
          "useSystemTextJson": true,
          "nullableReferenceTypes": true,
          "validatable": true,
          "useDataAnnotations": true,
          "useCollection": false,
          "returnICollection": false,
          "dateFormat": "yyyy-MM-dd",
          "dateTimeFormat": "O",
          "equatable": false,
          "generateApiExceptions": false,
          "useDateTimeOffset": false,
          "useOneOfDiscriminatorLookup": true,
          "disallowAdditionalPropertiesIfNotPresent": false
        }
      }
    }
  }
}

Example output when discriminator is missing:

        public override void Write(Utf8JsonWriter writer, ResultsEntryInner resultsEntryInner, JsonSerializerOptions jsonSerializerOptions)
        {
            writer.WriteStartObject();

            WriteProperties(writer, resultsEntryInner, jsonSerializerOptions);
            writer.WriteEndObject();
        }

Example output when discriminator is present:

        public override void Write(Utf8JsonWriter writer, ResultsEntryInner resultsEntryInner, JsonSerializerOptions jsonSerializerOptions)
        {
            writer.WriteStartObject();

            if (resultsEntryInner.Diagnostic != null)
            {
                DiagnosticJsonConverter diagnosticJsonConverter = (DiagnosticJsonConverter) jsonSerializerOptions.Converters.First(c => c.CanConvert(resultsEntryInner.Diagnostic.GetType()));
                diagnosticJsonConverter.WriteProperties(writer, resultsEntryInner.Diagnostic, jsonSerializerOptions);
            }

            if (resultsEntryInner.Observation != null)
            {
                ObservationJsonConverter observationJsonConverter = (ObservationJsonConverter) jsonSerializerOptions.Converters.First(c => c.CanConvert(resultsEntryInner.Observation.GetType()));
                observationJsonConverter.WriteProperties(writer, resultsEntryInner.Observation, jsonSerializerOptions);
            }

            if (resultsEntryInner.Communication != null)
            {
                CommunicationJsonConverter communicationJsonConverter = (CommunicationJsonConverter) jsonSerializerOptions.Converters.First(c => c.CanConvert(resultsEntryInner.Communication.GetType()));
                communicationJsonConverter.WriteProperties(writer, resultsEntryInner.Communication, jsonSerializerOptions);
            }

            WriteProperties(writer, resultsEntryInner, jsonSerializerOptions);
            writer.WriteEndObject();
        }

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/23529

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 running the generation command from openapitools.json with the supplied oneOfBug.yml declaration, then inspect the generated C# JsonConverter for ResultsEntryInner. Compare output with and without the discriminator; done means the no-discriminator case serializes the oneOf members in WriteProperties and a regression check covers the reported schema.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.