Skip to content

[BUG][Kotlin] Empty (invalid) data class generated when using discriminator and kotlinx.serialization #22349

@diastremskii

Description

@diastremskii

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?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When using kotlinx.serialization as serialization library and having an object without any properties other than discriminator itself, the outputted class is invalid - generator outputs an empty data class and makes Kotlin compilation fail.

openapi-generator version

v7.17.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Test Empty Model
  version: 1.0.0
paths: {}
components:
  schemas:
    Response:
      type: "object"
      properties:
        result_or_error:
          nullable: true
          discriminator:
            propertyName: "_type"
            mapping:
              result: "#/components/schemas/EmptyModel"
              error: "#/components/schemas/Error"
          oneOf:
            - $ref: "#/components/schemas/EmptyModel"
            - $ref: "#/components/schemas/Error"
    EmptyModel:
      type: object
      properties:
        _type:
          required:
            - "_type"
          type: string
          default: "result"
      additionalProperties: false
    Error:
      type: object
      required:
        - "code"
      properties:
        code:
          type: integer
        _type:
          required:
            - "_type"
          type: string
          default: "error"
      additionalProperties: false
Generation Details

kotlinx.serialization should be used.

Steps to reproduce
  1. Use the schema provided as an example
  2. Set serialization library to kotlinx.serialization
  3. Generate the Kotlin client
  4. Try to compile the client
Related issues/PRs

#14710
#19993

Suggest a fix

Going to open a PR in a bit.

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