Skip to content

Commit 61c6a9a

Browse files
authored
Fix missing imports for schemaMapping in API interfaces (#24672)
* [core] fix missing imports for schemaMapping with importMapping in API interfaces (#24232) * trigger CI * [core] support parameter and response schemaMapping imports and refactor test
1 parent bb7c33b commit 61c6a9a

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4888,6 +4888,9 @@ public CodegenOperation fromOperation(String path,
48884888
imports.add(r.baseType);
48894889
}
48904890

4891+
addImportMappedType(imports, r.dataType);
4892+
addImportMappedType(imports, r.baseType);
4893+
48914894
if ("set".equals(r.containerType) && typeMapping.containsKey(r.containerType)) {
48924895
op.uniqueItems = true;
48934896
imports.add(typeMapping.get(r.containerType));
@@ -5132,6 +5135,18 @@ public CodegenOperation fromOperation(String path,
51325135
return op;
51335136
}
51345137

5138+
/**
5139+
* Helper method to add an import for a data type if it exists in the importMapping.
5140+
*
5141+
* @param imports The set of imports to add to.
5142+
* @param dataType The data type to check for a mapping.
5143+
*/
5144+
protected void addImportMappedType(Set<String> imports, String dataType) {
5145+
if (imports != null && dataType != null && importMapping.containsKey(dataType)) {
5146+
imports.add(dataType);
5147+
}
5148+
}
5149+
51355150
public void SortParametersByRequiredFlag(List<CodegenParameter> parameters) {
51365151
Collections.sort(parameters, new Comparator<CodegenParameter>() {
51375152
@Override
@@ -5728,6 +5743,8 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
57285743
if (codegenProperty.complexType != null) {
57295744
imports.add(codegenProperty.complexType);
57305745
}
5746+
addImportMappedType(imports, codegenParameter.dataType);
5747+
addImportMappedType(imports, codegenParameter.baseType);
57315748

57325749
codegenParameter.pattern = toRegularExpression(parameterSchema.getPattern());
57335750

‎modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9314,4 +9314,25 @@ public void testReactiveSpringHttpInterfaceSupportListOfStringReturnTypeNoRespon
93149314
"Mono<Set<String>> getUserIdSet"
93159315
);
93169316
}
9317+
9318+
@Test
9319+
public void issue_24232() throws IOException {
9320+
Map<String, File> files = generateFromContract(
9321+
"src/test/resources/3_0/spring/issue_24232.yaml", SPRING_BOOT,
9322+
Map.of(USE_SPRING_BOOT4, true),
9323+
codegenConfigurator ->
9324+
codegenConfigurator
9325+
.addTypeMapping("string+custom", "MyCustomId")
9326+
.addSchemaMapping("MyKey", "MyCustomKey")
9327+
.addImportMapping("MyCustomId", "org.myorg.MyCustomId")
9328+
.addImportMapping("MyCustomKey", "org.myorg.MyCustomKey"));
9329+
9330+
JavaFileAssert.assertThat(files.get("SomeApi.java"))
9331+
.assertMethod("getDummy", "MyCustomId", "MyCustomKey")
9332+
.toFileAssert()
9333+
.fileContains("import org.myorg.MyCustomId;", "import org.myorg.MyCustomKey;");
9334+
9335+
JavaFileAssert.assertThat(files.get("Dummy.java"))
9336+
.fileContains("import org.myorg.MyCustomId;", "import org.myorg.MyCustomKey;");
9337+
}
93179338
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
openapi: 3.0.0
2+
info:
3+
version: "1.0.0"
4+
title: mapping
5+
paths:
6+
/some/dummy/endpoint/{id}:
7+
get:
8+
operationId: getDummy
9+
parameters:
10+
- name: id
11+
in: path
12+
required: true
13+
description: My Custom Id
14+
schema:
15+
$ref: '#/components/schemas/MyId'
16+
- name: key
17+
in: query
18+
required: true
19+
description: filter on key
20+
schema:
21+
$ref: '#/components/schemas/MyKey'
22+
responses:
23+
200:
24+
description: Successfully created reverse listings for retail
25+
content:
26+
application/json:
27+
schema:
28+
$ref: '#/components/schemas/Dummy'
29+
components:
30+
schemas:
31+
Dummy:
32+
type: object
33+
properties:
34+
id:
35+
$ref: '#/components/schemas/MyId'
36+
key:
37+
$ref: '#/components/schemas/MyKey'
38+
MyId:
39+
type: string
40+
format: custom
41+
MyKey:
42+
type: string

0 commit comments

Comments
 (0)