Skip to content

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

Description

@NishchhalTenics

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


Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions