Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5711,6 +5711,9 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
if (codegenProperty.isEnum) {
codegenParameter.datatypeWithEnum = codegenProperty.datatypeWithEnum;
codegenParameter.enumName = codegenProperty.enumName;
if(codegenParameter.dataType != null && !codegenParameter.dataType.contains("&") && !codegenParameter.dataType.contains("<")) {
imports.add(codegenParameter.dataType);
}
if (codegenProperty.defaultValue != null) {
codegenParameter.enumDefaultValue = codegenProperty.defaultValue.replace(codegenProperty.enumName + ".", "");
}
Expand All @@ -5721,6 +5724,9 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
codegenParameter.enumName = codegenProperty.enumName;
codegenParameter.items = codegenProperty.items;
codegenParameter.mostInnerItems = codegenProperty.mostInnerItems;
if(codegenParameter.items.dataType != null && !codegenParameter.items.dataType.contains("&") && !codegenParameter.items.dataType.contains("<")) {
imports.add(codegenParameter.items.dataType);
}
}

codegenParameter.collectionFormat = collectionFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.headers.Header;
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.QueryParameter;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
Expand All @@ -57,6 +58,7 @@
import java.util.concurrent.*;
import java.util.stream.Collectors;

import static com.fasterxml.jackson.databind.util.ClassUtil.name;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -5427,4 +5429,23 @@ public void splitOperationsByContentTypeIsAGlobalOption() {
off.processOpts();
assertThat(off.splitOperationsByContentType).isFalse();
}

@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>

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");

DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);

Operation operation = openAPI.getPaths().get("/archives").getGet();
Parameter parameter = operation.getParameters().get(0);

Set<String> imports = new HashSet<>();
codegen.fromParameter(parameter, imports);

// Assert enum type is present in imports
Assert.assertTrue(
imports.contains("State"),
"Expected enum 'State' to be present in imports but got: " + imports
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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
Loading