OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] Direct property enum reference used by parameter omits generated model import

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

The Java client generator produces uncompilable code when a query parameter schema directly references an inline enum property of a component schema.

The generator promotes the inline enum to a model enum (StockState), but the generated API class uses that type without importing it.

Actual result

The generator creates:

src/main/java/org/openapitools/client/model/StockState.java

but generates DefaultApi.java with unqualified uses of StockState:

public okhttp3.Call listArchivesCall(
    @javax.annotation.Nullable StockState state,
    final ApiCallback _callback) throws ApiException {

DefaultApi.java does not contain:

import org.openapitools.client.model.StockState;

Compilation fails five times with:

error: cannot find symbol
  symbol:   class StockState
  location: class DefaultApi

Expected result

The generated Java client should compile. DefaultApi.java should import StockState, or use its fully qualified name.

openapi-generator version

7.26.0-SNAPSHOT, built from current master commit b391b2b98c5926ee9842ef3bc863b57c4d824bbe.

Also happens in 7.24.0.

This has not been bisected to identify the first affected version.

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  title: Enum parameter import
  version: 1.0.0
paths:
  /archives:
    get:
      operationId: listArchives
      parameters:
        - name: state
          in: query
          required: false
          schema:
            $ref: "#/components/schemas/Archive/properties/state"
      responses:
        "200":
          description: Success
components:
  schemas:
    Archive:
      type: object
      properties:
        state:
          type: string
          enum:
            - ACTIVE
Generation Details
java -jar openapi-generator-cli.jar generate \
  -g java \
  -i enum-not-imported.yaml \
  -o generated \
  --skip-validate-spec \
  --additional-properties modelNamePrefix=Stock,hideGenerationTimestamp=true
Steps to reproduce
  1. Save the OpenAPI declaration above as enum-not-imported.yaml.

  2. Validate it:

    java -jar openapi-generator-cli.jar validate -i enum-not-imported.yaml
    
  3. Generate the Java client with the command above.

  4. Compile the generated client:

    cd generated
    mvn -q -DskipTests compile
    
Related issues/PRs
  • did not find any
Suggest a fix

DefaultCodegen#fromParameter handles imports specially for array parameter items, but the direct scalar enum reference in this reproducer is not added to the API operation's import set.

When a parameter's resolved schema produces a generated enum model type, add that type to the operation imports so the Java API template emits:

import org.openapitools.client.model.StockState;

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 at DefaultCodegen#fromParameter and reproduce with enum-not-imported.yaml using the listed generation command. Inspect how direct scalar enum references populate operation imports and compare generated DefaultApi.java with StockState.java. Done means the generated Maven client compiles and DefaultApi.java imports org.openapitools.client.model.StockState.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
74/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.