Skip to content

[BUG][java] Discriminator default for direct property enum $ref generates String.<value> #24874

Description

@mricken

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)

Describe the bug

The Java client generator produces Java that does not compile when a discriminator property is a direct $ref to an inline enum property. The generated child constructor assigns the discriminator mapping value as though the property were String:

this.category = String.ARCHIVE;

String.ARCHIVE does not exist.

Minimal OpenAPI 3.1 input

openapi: 3.1.0
info:
  title: Shared category discriminator
  version: 1.0.0
paths: {}
components:
  schemas:
    CategorySource:
      type: object
      required:
        - category
      properties:
        category:
          type: string
          enum:
            - ARCHIVE
    CategoryEvent:
      type: object
      required:
        - category
      properties:
        category:
          description: Event category
          $ref: "#/components/schemas/CategorySource/properties/category"
      discriminator:
        propertyName: category
        mapping:
          ARCHIVE: "#/components/schemas/ArchiveCategoryEvent"
    ArchiveCategoryEvent:
      allOf:
        - $ref: "#/components/schemas/CategoryEvent"
        - type: object
          properties:
            value:
              type: string

Reproduction

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar \
  generate \
  -g java \
  -i enum-discriminator.yaml \
  -o generated \
  --skip-validate-spec \
  --additional-properties modelNamePrefix=Stock,hideGenerationTimestamp=true

cd generated
mvn -q -DskipTests compile

Actual result

Latest master (b391b2b98c5926ee9842ef3bc863b57c4d824bbe, 7.26.0-SNAPSHOT) generates:

public StockArchiveCategoryEvent() {
    this.category = String.ARCHIVE;
}

Compilation fails:

error: cannot find symbol
  symbol:   variable ARCHIVE
  location: class String

The same failure occurs with OpenAPI Generator 7.24.0.

Expected result

The direct property reference should retain the enum type at the discriminator use site. The generated constructor should assign the enum member, rather than String.ARCHIVE, and the generated client should compile.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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