OpenAPITools / OpenAPITools/openapi-generator

[BUG][CSHARP][GENERICHOST] oneOf Serialization: Empty WriteProperties

Open
#24,398 1 comment 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

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • 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?
Description

When generating a C# client using OpenAPI Generator (v7.24), there is a critical issue with oneOf schemas:

Empty WriteProperties Methods: The WriteProperties methods in the JsonConverter<T> classes for oneOf schemas are empty, causing serialization to produce incorrect JSON (e.g., {}).

Impact (Major Blocker)
  • Serialization produces incorrect output: The Write method calls WriteProperties, but the latter contains no logic to serialize the active oneOf member.
  • Affects multiple oneOf variants: observed with primitive types (number/string), inline string enums, and object references ($ref).

openapi-generator version

v7.24.0

OpenAPI declaration file content or url
openapi: 3.1.2
info:
  title: Simple API
  version: 1.0.0

paths:
  /hello:
    post:
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HelloResponse'

components:
  schemas:
    HelloResponse:
      type: object
      properties:
        firstCustomProperty:
          oneOf:
            - type: number
            - type: string
        secondCustomProperty:
          oneOf:
            - type: number
            - type: string
              enum: [TEST1, TEST2, TEST3]
        thirdCustomProperty:
          oneOf:
            - $ref: '#/components/schemas/FirstSub'
            - $ref: '#/components/schemas/SecondSub'
    FirstSub:
      type: object
      properties:
        id: { type: string }
        name: { type: string }
    SecondSub:
      type: object
      properties:
        id: { type: number }
        name: { type: string }
        age: { type: number }
Generation Details

Use this config.json:

{
  "packageName": "Tenics.OneOfDemo.Sdk",
  "packageVersion": "1.0.0",
  "targetFramework": "net10.0",
  "library": "generichost",
  "nullableReferenceTypes": true,
  "modelPropertyNaming": "PascalCase"
}

Use this to generate client:

openapi-generator-cli generate \
  -i spec.yaml \
  -g csharp \
  -c config.json \
  -o ./generated

Expected Behavior

The generated JsonConverter<T> should serialize whichever oneOf branch is populated.

For example:

public void Write(Utf8JsonWriter writer, HelloResponseSecondCustomProperty value, JsonSerializerOptions options)
{
    if (value.Double.HasValue)
    {
        writer.WriteNumberValue(value.Double.Value);
    }
    else if (value.String != null)
    {
        writer.WriteStringValue(value.String);
    }
}

Actual Behavior

The generated file:
HelloResponseSecondCustomPropertyJsonConverter.cs

contains:

  • The WriteProperties method is empty and lacks serialization logic:
        public override void Write(Utf8JsonWriter writer, HelloResponseSecondCustomProperty helloResponseSecondCustomProperty, JsonSerializerOptions jsonSerializerOptions)
        {
            writer.WriteStartObject();
    
            WriteProperties(writer, helloResponseSecondCustomProperty, jsonSerializerOptions);
            writer.WriteEndObject();
        }
    
    public void WriteProperties(Utf8JsonWriter writer, HelloResponseSecondCustomProperty value, JsonSerializerOptions options)
    {
        // Missing logic to serialize Double or String
    }
    

Related Tickets


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

Reproduce the issue with spec.yaml, config.json, and the provided openapi-generator-cli command. Inspect the generated HelloResponseSecondCustomPropertyJsonConverter.cs and compare its empty WriteProperties method with the expected oneOf behavior; read related issue #23693 for context. Done means populated primitive, enum, and referenced oneOf branches serialize to their JSON values.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.