-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
Description
When using the java generator with library=feign-hc5, any user-provided templateResourcePath (or templateDir / -t) is silently ignored. The same configuration works correctly when using library=feign.
The root cause is in JavaClientCodegen.processOpts(), where the feign-hc5 branch unconditionally overwrites the template directory:
} else if (isLibrary(FEIGN_HC5)) {
additionalProperties.put("feign-hc5", "true");
setTemplateDir(FEIGN); // Overwrites any user-provided templateDir/templateResourcePath
setLibrary(FEIGN);
}Lines 842 to 844 in e6304a8
| additionalProperties.put("feign-hc5", "true"); | |
| setTemplateDir(FEIGN); | |
| setLibrary(FEIGN); |
The setTemplateDir(FEIGN) call runs after the user's templateResourcePath/templateDir has already been set on the configurator, so it unconditionally replaces the user's custom template path with "feign". This means any custom templates the user provides are never picked up.
With library=feign, the if (isLibrary(FEIGN)) branch only sets additionalProperties.put("feign-okhttp", "true") and does not call setTemplateDir(), so the user's custom template path is preserved.
openapi-generator version
Latest master (commit e6304a8db2930fd3dd54a22dd220ac3bfc69e03f). The issue has existed since feign-hc5 was introduced.
OpenAPI declaration file content or url
Any valid OpenAPI spec will reproduce this. For example, the petstore spec:
openapi: "3.0.0"
info:
title: Sample API
version: "1.0.0"
paths:
/pets:
get:
operationId: listPets
summary: List all pets
responses:
'200':
description: A list of pets
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
components:
schemas:
Pet:
type: object
properties:
id:
type: integer
format: int64
name:
type: stringGeneration Details
Maven plugin example (reproduces the bug):
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
<generatorName>java</generatorName>
<library>feign-hc5</library>
<templateResourcePath>my-custom-templates/Java</templateResourcePath>
</configuration>
</plugin>CLI equivalent:
openapi-generator-cli generate \
-g java \
--library feign-hc5 \
-t my-custom-templates/Java \
-i api.yaml \
-o outputSteps to reproduce
- Create a custom template directory (e.g.
my-custom-templates/Java/libraries/feign/) with a modified template file (e.g. a customizedapi.mustache). - Configure the generator with
generatorName=java,library=feign-hc5, andtemplateResourcePath(or-t) pointing tomy-custom-templates/Java. - Run the generator.
- Observe: The custom templates are ignored; the built-in embedded templates are used instead.
- Change the library to
feign(keeping everything else the same). - Run the generator again.
- Observe: The custom templates are now correctly picked up and used.
Actual output: With feign-hc5, the generator uses the built-in embedded templates, ignoring the user-provided templateResourcePath/templateDir.
Expected output: With feign-hc5, the generator should respect the user-provided templateResourcePath/templateDir and use custom templates, just as it does with the feign library.
Related issues/PRs
No directly related issues found.
Suggest a fix
The fix should be in JavaClientCodegen.java at line 842-844.
The feign-hc5 branch should not call setTemplateDir(FEIGN) unconditionally. Instead, it should only set the template directory if the user has not already provided a custom one. For example:
} else if (isLibrary(FEIGN_HC5)) {
additionalProperties.put("feign-hc5", "true");
// Only override templateDir if no custom template directory was provided by the user
if (!isUserProvidedTemplateDir()) {
setTemplateDir(FEIGN);
}
setLibrary(FEIGN);
}Alternatively, the feign-hc5 library could be restructured to avoid calling setTemplateDir() entirely — for instance, by creating a libraries/feign-hc5/ resource directory (even if it just symlinks or delegates to the feign templates), so the standard library template resolution mechanism works without needing to override the template directory at the code level.