Skip to content

[Java] Fix #24875: Add missing import for direct enum property reference in parameters - #24918

Open
Harshavardhan2951 wants to merge 1 commit into
OpenAPITools:masterfrom
Harshavardhan2951:fix/24875-enum-parameter-import
Open

Harshavardhan2951 wants to merge 1 commit into
OpenAPITools:masterfrom
Harshavardhan2951:fix/24875-enum-parameter-import

Conversation

@Harshavardhan2951

@Harshavardhan2951 Harshavardhan2951 commented Sep 10, 2026

Copy link
Copy Markdown

PR Title

[Java] Fix #24875: Add missing import for direct enum property reference in parameters

Description of the change

Fixes #24875

When a query parameter references a direct enum property via
$ref: "#/components/schemas/Component/properties/field", the required
import statement was missing from the generated API class, causing
compilation failures.

This fix updates DefaultCodegen#fromParameter to add the resolved enum
type to the operation's import set, with a guard against composed/generic
types (containing & or <) that are not valid single-type imports.

PR Checklist

  • Read the contribution guidelines.
  • Pull Request title conforms to conventions.
  • Ran mvn clean install to ensure all unit tests pass.
  • Added unit test in DefaultCodegenTest.java verifying enum imports
    are included when a parameter directly references an enum property.
  • Generated updated samples using ./bin/generate-samples.sh.

Summary by cubic

Fixes generated code missing enum imports when a query parameter directly references an enum property via $ref (issue #24875). Previously the generated API class failed to compile because the resolved enum type wasn't imported; now fromParameter adds it to the operation's imports, skipping composed/generic type strings containing & or <.

Side effect: regenerated JavaScript samples now include import [String] from '../model/[String]' lines, which are invalid JavaScript imports and may break the javascript-apollo, javascript-es6, and javascript-promise-es6 generators.

Written for commit 3ba2cb1. Summary will update on new commits.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 9 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="samples/client/petstore/javascript-apollo/src/api/PetApi.js">

<violation number="1" location="samples/client/petstore/javascript-apollo/src/api/PetApi.js:19">
P1: This generated import line is invalid JavaScript: `import [String] ...` throws `SyntaxError: Unexpected token '['`, and there is no `../model/[String]` module to load. It appears for array-of-enum parameters (here `status` in `findPetsByStatus`) because the new `imports.add(codegenParameter.dataType)` guard in `DefaultCodegen.fromParameter` only rejects types containing `&` or `<`, but the array dataType is the bracket-wrapped `[String]`, which passes the guard. The guard must also exclude container types (square brackets), or only add the import when `codegenProperty` is a scalar enum ref and not an array.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

import ApiClient from "../ApiClient";
import ApiResponse from '../model/ApiResponse';
import Pet from '../model/Pet';
import [String] from '../model/[String]';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: This generated import line is invalid JavaScript: import [String] ... throws SyntaxError: Unexpected token '[', and there is no ../model/[String] module to load. It appears for array-of-enum parameters (here status in findPetsByStatus) because the new imports.add(codegenParameter.dataType) guard in DefaultCodegen.fromParameter only rejects types containing & or <, but the array dataType is the bracket-wrapped [String], which passes the guard. The guard must also exclude container types (square brackets), or only add the import when codegenProperty is a scalar enum ref and not an array.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/client/petstore/javascript-apollo/src/api/PetApi.js, line 19:

<comment>This generated import line is invalid JavaScript: `import [String] ...` throws `SyntaxError: Unexpected token '['`, and there is no `../model/[String]` module to load. It appears for array-of-enum parameters (here `status` in `findPetsByStatus`) because the new `imports.add(codegenParameter.dataType)` guard in `DefaultCodegen.fromParameter` only rejects types containing `&` or `<`, but the array dataType is the bracket-wrapped `[String]`, which passes the guard. The guard must also exclude container types (square brackets), or only add the import when `codegenProperty` is a scalar enum ref and not an array.</comment>

<file context>
@@ -16,6 +16,7 @@
 import ApiClient from "../ApiClient";
 import ApiResponse from '../model/ApiResponse';
 import Pet from '../model/Pet';
+import [String] from '../model/[String]';
 
 /**
</file context>

@Harshavardhan2951
Harshavardhan2951 force-pushed the fix/24875-enum-parameter-import branch from 6659404 to 9fb3426 Compare September 10, 2026 03:46
…eference in parameters

DefaultCodegen#fromParameter handles imports for array items but not
for direct scalar enum references. This adds the generated enum
model type to operation imports, guarding against composed/generic
type strings (containing '&' or '<') that aren't valid import targets.

Fixes OpenAPITools#24875
@Harshavardhan2951
Harshavardhan2951 force-pushed the fix/24875-enum-parameter-import branch from 9fb3426 to 3ba2cb1 Compare September 10, 2026 03:58
@Harshavardhan2951

Copy link
Copy Markdown
Author

The JavaScript CI failure is a pre-existing issue on master,
unrelated to this Java fix. The same 6 JS files change when
running ./bin/generate-samples.sh on master branch as well.

@Harshavardhan2951

Copy link
Copy Markdown
Author

@cubic review

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

@cubic review

@Harshavardhan2951 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 3 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java">

<violation number="1" location="modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java:5434">
P3: The new test only covers the positive enum path; it never exercises the `&`/`<` guard the fix adds (neither for `codegenParameter.dataType` nor `items.dataType`). Since the PR description already reports invalid imports leaking into generated samples, extend this test with a composed/generic parameter case asserting no import is added, so regressions in the new guard are caught.</violation>

<violation number="2" location="modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java:5435">
P3: This added statement exceeds the repository's 100-character Checkstyle limit. Wrap the resource path onto a continuation line.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

}

@Test
public void testFromParameterEnumImports() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: The new test only covers the positive enum path; it never exercises the &/< guard the fix adds (neither for codegenParameter.dataType nor items.dataType). Since the PR description already reports invalid imports leaking into generated samples, extend this test with a composed/generic parameter case asserting no import is added, so regressions in the new guard are caught.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java, line 5434:

<comment>The new test only covers the positive enum path; it never exercises the `&`/`<` guard the fix adds (neither for `codegenParameter.dataType` nor `items.dataType`). Since the PR description already reports invalid imports leaking into generated samples, extend this test with a composed/generic parameter case asserting no import is added, so regressions in the new guard are caught.</comment>

<file context>
@@ -5427,4 +5429,23 @@ public void splitOperationsByContentTypeIsAGlobalOption() {
     }
+
+    @Test
+    public void testFromParameterEnumImports() {
+        OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/issue24875-enum-parameter-import.yaml");
+        DefaultCodegen codegen = new DefaultCodegen();
</file context>


@Test
public void testFromParameterEnumImports() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/issue24875-enum-parameter-import.yaml");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: This added statement exceeds the repository's 100-character Checkstyle limit. Wrap the resource path onto a continuation line.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java, line 5435:

<comment>This added statement exceeds the repository's 100-character Checkstyle limit. Wrap the resource path onto a continuation line.</comment>

<file context>
@@ -5427,4 +5429,23 @@ public void splitOperationsByContentTypeIsAGlobalOption() {
+
+    @Test
+    public void testFromParameterEnumImports() {
+        OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/issue24875-enum-parameter-import.yaml");
+        DefaultCodegen codegen = new DefaultCodegen();
+        codegen.setOpenAPI(openAPI);
</file context>
Suggested change
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/issue24875-enum-parameter-import.yaml");
OpenAPI openAPI = TestUtils.parseSpec(
"src/test/resources/3_1/issue24875-enum-parameter-import.yaml");

@wing328

wing328 commented Sep 15, 2026

Copy link
Copy Markdown
Member

thanks for the PR

cc @OpenAPITools/generator-core-team

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants