From 6c265cdf17555576883305d9b15497f89cb2e7a6 Mon Sep 17 00:00:00 2001 From: Subham Ashok Date: Wed, 19 Aug 2026 09:30:33 +0530 Subject: [PATCH 1/3] [core] fix missing imports for schemaMapping with importMapping in API interfaces (#24232) --- .../openapitools/codegen/DefaultCodegen.java | 14 +++++++ .../java/spring/SpringCodegenTest.java | 28 +++++++++++++ .../src/test/resources/bugs/issue_24232.yaml | 42 +++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 modules/openapi-generator/src/test/resources/bugs/issue_24232.yaml diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 6d6c631aec1e..f9706c88eca3 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -4888,6 +4888,8 @@ public CodegenOperation fromOperation(String path, imports.add(r.baseType); } + addImportMappedType(imports, r.dataType); + if ("set".equals(r.containerType) && typeMapping.containsKey(r.containerType)) { op.uniqueItems = true; imports.add(typeMapping.get(r.containerType)); @@ -5132,6 +5134,17 @@ public CodegenOperation fromOperation(String path, return op; } + /** + * Helper method to add an import for a data type if it exists in the importMapping. + * @param imports The set of imports to add to. + * @param dataType The data type to check for a mapping. + */ + protected void addImportMappedType(Set imports, String dataType) { + if (importMapping.containsKey(dataType)) { + imports.add(dataType); + } + } + public void SortParametersByRequiredFlag(List parameters) { Collections.sort(parameters, new Comparator() { @Override @@ -5728,6 +5741,7 @@ public CodegenParameter fromParameter(Parameter parameter, Set imports) if (codegenProperty.complexType != null) { imports.add(codegenProperty.complexType); } + addImportMappedType(imports, codegenParameter.dataType); codegenParameter.pattern = toRegularExpression(parameterSchema.getPattern()); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java index e5c1ad926aa6..621c711ee4f2 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java @@ -9163,4 +9163,32 @@ public void testReactiveSpringHttpInterfaceSupportListOfStringReturnTypeNoRespon "Mono> getUserIdSet" ); } + + @Test + public void testSchemaMappingAddsImport_issue24232() throws IOException { + Map properties = new HashMap<>(); + properties.put(CodegenConstants.API_PACKAGE, "xyz.abcdef.api"); + properties.put(INTERFACE_ONLY, true); + + File output = Files.createTempDirectory("test").toFile(); + output.deleteOnExit(); + + final CodegenConfigurator configurator = new CodegenConfigurator() + .setGeneratorName("spring") + .setAdditionalProperties(properties) + .setSchemaMappings(Map.of("MyKey", "MyCustomKey")) + .setImportMappings(Map.of("MyCustomKey", "org.myorg.MyCustomKey")) + .setInputSpec("src/test/resources/bugs/issue_24232.yaml") + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); + + DefaultGenerator generator = new DefaultGenerator(); + List files = generator.opts(configurator.toClientOptInput()).generate(); + files.forEach(File::deleteOnExit); + + validateJavaSourceFiles(files); + + File testApi = new File(output, "src/main/java/xyz/abcdef/api/SomeApi.java"); + + JavaFileAssert.assertThat(testApi).fileContains("import org.myorg.MyCustomKey;"); + } } diff --git a/modules/openapi-generator/src/test/resources/bugs/issue_24232.yaml b/modules/openapi-generator/src/test/resources/bugs/issue_24232.yaml new file mode 100644 index 000000000000..39304752e1e5 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/bugs/issue_24232.yaml @@ -0,0 +1,42 @@ +openapi: 3.0.0 +info: + version: "1.0.0" + title: mapping +paths: + /some/dummy/endpoint/{id}: + get: + operationId: getDummy + parameters: + - name: id + in: path + required: true + description: My Custom Id + schema: + $ref: '#/components/schemas/MyId' + - name: key + in: query + required: true + description: filter on key + schema: + $ref: '#/components/schemas/MyKey' + responses: + 200: + description: Successfully created reverse listings for retail + content: + application/json: + schema: + $ref: '#/components/schemas/Dummy' +components: + schemas: + Dummy: + type: object + properties: + id: + $ref: '#/components/schemas/MyId' + key: + $ref: '#/components/schemas/MyKey' + MyId: + type: string + format: custom + MyKey: + type: string From b06732a3cf4d8aa984dd06b4ded4f3e93e3e5145 Mon Sep 17 00:00:00 2001 From: Subham Ashok Date: Wed, 19 Aug 2026 16:39:07 +0530 Subject: [PATCH 2/3] trigger CI From aef2be1f4734ee230bad794dd8351442d9cbe876 Mon Sep 17 00:00:00 2001 From: Subham Ashok Date: Thu, 20 Aug 2026 01:17:33 +0530 Subject: [PATCH 3/3] [core] support parameter and response schemaMapping imports and refactor test --- .../openapitools/codegen/DefaultCodegen.java | 7 +++- .../java/spring/SpringCodegenTest.java | 39 ++++++++----------- .../{bugs => 3_0/spring}/issue_24232.yaml | 0 3 files changed, 21 insertions(+), 25 deletions(-) rename modules/openapi-generator/src/test/resources/{bugs => 3_0/spring}/issue_24232.yaml (100%) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index f9706c88eca3..085f1d981597 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -4889,6 +4889,7 @@ public CodegenOperation fromOperation(String path, } addImportMappedType(imports, r.dataType); + addImportMappedType(imports, r.baseType); if ("set".equals(r.containerType) && typeMapping.containsKey(r.containerType)) { op.uniqueItems = true; @@ -5136,11 +5137,12 @@ public CodegenOperation fromOperation(String path, /** * Helper method to add an import for a data type if it exists in the importMapping. - * @param imports The set of imports to add to. + * + * @param imports The set of imports to add to. * @param dataType The data type to check for a mapping. */ protected void addImportMappedType(Set imports, String dataType) { - if (importMapping.containsKey(dataType)) { + if (imports != null && dataType != null && importMapping.containsKey(dataType)) { imports.add(dataType); } } @@ -5742,6 +5744,7 @@ public CodegenParameter fromParameter(Parameter parameter, Set imports) imports.add(codegenProperty.complexType); } addImportMappedType(imports, codegenParameter.dataType); + addImportMappedType(imports, codegenParameter.baseType); codegenParameter.pattern = toRegularExpression(parameterSchema.getPattern()); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java index 621c711ee4f2..27a06876c100 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java @@ -9165,30 +9165,23 @@ public void testReactiveSpringHttpInterfaceSupportListOfStringReturnTypeNoRespon } @Test - public void testSchemaMappingAddsImport_issue24232() throws IOException { - Map properties = new HashMap<>(); - properties.put(CodegenConstants.API_PACKAGE, "xyz.abcdef.api"); - properties.put(INTERFACE_ONLY, true); - - File output = Files.createTempDirectory("test").toFile(); - output.deleteOnExit(); - - final CodegenConfigurator configurator = new CodegenConfigurator() - .setGeneratorName("spring") - .setAdditionalProperties(properties) - .setSchemaMappings(Map.of("MyKey", "MyCustomKey")) - .setImportMappings(Map.of("MyCustomKey", "org.myorg.MyCustomKey")) - .setInputSpec("src/test/resources/bugs/issue_24232.yaml") - .setOutputDir(output.getAbsolutePath().replace("\\", "/")); - - DefaultGenerator generator = new DefaultGenerator(); - List files = generator.opts(configurator.toClientOptInput()).generate(); - files.forEach(File::deleteOnExit); + public void issue_24232() throws IOException { + Map files = generateFromContract( + "src/test/resources/3_0/spring/issue_24232.yaml", SPRING_BOOT, + Map.of(USE_SPRING_BOOT4, true), + codegenConfigurator -> + codegenConfigurator + .addTypeMapping("string+custom", "MyCustomId") + .addSchemaMapping("MyKey", "MyCustomKey") + .addImportMapping("MyCustomId", "org.myorg.MyCustomId") + .addImportMapping("MyCustomKey", "org.myorg.MyCustomKey")); - validateJavaSourceFiles(files); + JavaFileAssert.assertThat(files.get("SomeApi.java")) + .assertMethod("getDummy", "MyCustomId", "MyCustomKey") + .toFileAssert() + .fileContains("import org.myorg.MyCustomId;", "import org.myorg.MyCustomKey;"); - File testApi = new File(output, "src/main/java/xyz/abcdef/api/SomeApi.java"); - - JavaFileAssert.assertThat(testApi).fileContains("import org.myorg.MyCustomKey;"); + JavaFileAssert.assertThat(files.get("Dummy.java")) + .fileContains("import org.myorg.MyCustomId;", "import org.myorg.MyCustomKey;"); } } diff --git a/modules/openapi-generator/src/test/resources/bugs/issue_24232.yaml b/modules/openapi-generator/src/test/resources/3_0/spring/issue_24232.yaml similarity index 100% rename from modules/openapi-generator/src/test/resources/bugs/issue_24232.yaml rename to modules/openapi-generator/src/test/resources/3_0/spring/issue_24232.yaml