Skip to content

Commit a18bdda

Browse files
github-actions[bot]hbelmiroricardozanini
authored
[quarkus2] Added support for custom templates (#445)
* Added support for custom templates (#439) * Added support for custom templates Signed-off-by: Helber Belmiro <[email protected]> * Update README.md Co-authored-by: Ricardo Zanini <[email protected]> --------- Signed-off-by: Helber Belmiro <[email protected]> Co-authored-by: Ricardo Zanini <[email protected]> * Replaced jakarta with javax Fixed version --------- Signed-off-by: Helber Belmiro <[email protected]> Co-authored-by: Helber Belmiro <[email protected]> Co-authored-by: Ricardo Zanini <[email protected]>
1 parent 83c89d4 commit a18bdda

30 files changed

+683
-4
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,18 @@ It's also possible to only use a type mapping with a fully qualified name, for i
730730

731731
See the module [type-mapping](integration-tests/type-mapping) for an example of how to use this feature.
732732

733+
## Template Customization
734+
735+
You have the option to swap out the [templates used by this extension](deployment/src/main/resources/templates/libraries/microprofile) with your customized versions. To achieve this, place your custom templates under the `resources/templates` directory. It's crucial that the filename of each custom template matches that of the original template.
736+
737+
You can find an example of using customized templates in [integration-tests/custom-templates](integration-tests/custom-templates).
738+
739+
### **⚠️** Important
740+
741+
While the option to replace templates exists, it's essential to exercise caution and consider this as a final resort. Prior to altering templates, exhaust all possibilities of achieving your goals through configuration settings. Modifying templates could have broader implications for the extension's functionality and may introduce complexities. Only resort to template replacement when configuration adjustments prove insufficient for your requirements.
742+
743+
Furthermore, be aware that customizing templates increases the risk of compatibility issues during future upgrades. Therefore, exercise discretion and weigh the benefits against the potential risks before opting for template customization.
744+
733745
## Known Limitations
734746

735747
These are the known limitations of this pre-release version:

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorCodeGenBase.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
107107
}
108108

109109
try (Stream<Path> openApiFilesPaths = Files.walk(openApiDir)) {
110+
Path templateDir = context.workDir().resolve("classes").resolve("templates");
110111
openApiFilesPaths
111112
.filter(Files::isRegularFile)
112113
.filter(path -> {
@@ -115,7 +116,8 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
115116
&& !filesToExclude.contains(fileName)
116117
&& (filesToInclude.isEmpty() || filesToInclude.contains(fileName));
117118
})
118-
.forEach(openApiFilePath -> generate(context.config(), openApiFilePath, outDir, isRestEasyReactive));
119+
.forEach(openApiFilePath -> generate(context.config(), openApiFilePath, outDir, templateDir,
120+
isRestEasyReactive));
119121
} catch (IOException e) {
120122
throw new CodeGenException("Failed to generate java files from OpenApi files in " + openApiDir.toAbsolutePath(),
121123
e);
@@ -149,8 +151,7 @@ private boolean isJacksonReactiveClient(ResolvedDependency resolvedDependency) {
149151

150152
// TODO: do not generate if the output dir has generated files and the openapi file has the same checksum of the previous run
151153
protected void generate(final Config config, final Path openApiFilePath, final Path outDir,
152-
boolean isRestEasyReactive) {
153-
154+
Path templateDir, boolean isRestEasyReactive) {
154155
final String basePackage = getBasePackage(config, openApiFilePath);
155156
final Boolean verbose = config.getOptionalValue(getGlobalConfigName(CodegenConfig.ConfigName.VERBOSE), Boolean.class)
156157
.orElse(false);
@@ -161,6 +162,8 @@ protected void generate(final Config config, final Path openApiFilePath, final P
161162
final OpenApiClientGeneratorWrapper generator = createGeneratorWrapper(openApiFilePath, outDir, isRestEasyReactive,
162163
verbose, validateSpec);
163164

165+
generator.withTemplateDir(templateDir);
166+
164167
generator.withClassesCodeGenConfig(ClassCodegenConfigParser.parse(config, basePackage))
165168
.withCircuitBreakerConfig(CircuitBreakerConfigurationParser.parse(
166169
config));

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorStreamCodeGen.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
7474
StandardOpenOption.CREATE)) {
7575
outChannel.transferFrom(inChannel, 0, Integer.MAX_VALUE);
7676
LOGGER.debug("Saved OpenAPI spec input model in {}", openApiFilePath);
77-
this.generate(this.mergeConfig(context, inputModel), openApiFilePath, outDir, isRestEasyReactive);
77+
this.generate(this.mergeConfig(context, inputModel), openApiFilePath, outDir,
78+
context.workDir().resolve("classes").resolve("templates"), isRestEasyReactive);
7879
generated = true;
7980
}
8081
} catch (IOException e) {

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/OpenApiClientGeneratorWrapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ public OpenApiClientGeneratorWrapper withAdditionalApiTypeAnnotationsConfig(fina
175175
return this;
176176
}
177177

178+
public void withTemplateDir(Path templateDir) {
179+
this.configurator.addAdditionalProperty("templateDir", templateDir.toString());
180+
}
181+
178182
public List<File> generate(final String basePackage) {
179183
this.basePackage = basePackage;
180184
this.consolidatePackageNames();

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/QuarkusJavaClientCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public void processOpts() {
4848
this.projectTestFolder = "";
4949
this.sourceFolder = "";
5050
this.testFolder = "";
51+
this.embeddedTemplateDir = "templates";
5152

5253
this.replaceWithQuarkusTemplateFiles();
5354
}

0 commit comments

Comments
 (0)