diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java index 159f848084e5..b2faed874176 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java @@ -850,7 +850,13 @@ public void processOpts() { additionalProperties.put("feign-okhttp", "true"); } else if (isLibrary(FEIGN_HC5)) { additionalProperties.put("feign-hc5", "true"); - setTemplateDir(FEIGN); + // Only fall back to the built-in "feign" template directory when the user has not + // provided a custom template directory. super.processOpts() already wrote any + // user-supplied templateDir back into additionalProperties, so checking for the + // key's presence reliably distinguishes "user provided" from "not provided". + if (!additionalProperties.containsKey(CodegenConstants.TEMPLATE_DIR)) { + setTemplateDir(FEIGN); + } setLibrary(FEIGN); } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java index 8f64f07e2c38..35bf0bdfe05c 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java @@ -4180,4 +4180,35 @@ public void testOneOfInterfaceWithEnumDiscriminatorHavingCustomDescription3_1() .fileContains("public FruitType getFruitType()"); } + /** + * Regression: without a user-provided template dir, feign-hc5 should still resolve + * built-in templates from the "feign" folder (same behaviour as before the fix). + */ + @Test + public void testFeignHc5TemplateDirDefaultsToFeign() { + final JavaClientCodegen codegen = new JavaClientCodegen(); + codegen.setLibrary(FEIGN_HC5); + codegen.processOpts(); + + assertEquals(codegen.templateDir(), FEIGN, + "feign-hc5 without a custom templateDir should use the 'feign' built-in template directory"); + } + + /** + * Bug fix: a user-provided templateDir must not be overwritten when library=feign-hc5. + * Previously setTemplateDir(FEIGN) was called unconditionally and silently replaced the + * user's path. + */ + @Test + public void testFeignHc5CustomTemplateDirIsPreserved() { + final String customTemplateDir = "/custom/templates"; + final JavaClientCodegen codegen = new JavaClientCodegen(); + codegen.setLibrary(FEIGN_HC5); + codegen.additionalProperties().put(CodegenConstants.TEMPLATE_DIR, customTemplateDir); + codegen.processOpts(); + + assertEquals(codegen.templateDir(), customTemplateDir, + "feign-hc5 must preserve a user-provided templateDir and not overwrite it with 'feign'"); + } + }