Skip to content

Commit 64151bc

Browse files
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]>
1 parent bd23c86 commit 64151bc

30 files changed

+683
-4
lines changed

README.md

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

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

732+
## Template Customization
733+
734+
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.
735+
736+
You can find an example of using customized templates in [integration-tests/custom-templates](integration-tests/custom-templates).
737+
738+
### **⚠️** Important
739+
740+
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.
741+
742+
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.
743+
732744
## Known Limitations
733745

734746
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
@@ -106,6 +106,7 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
106106
}
107107

108108
try (Stream<Path> openApiFilesPaths = Files.walk(openApiDir)) {
109+
Path templateDir = context.workDir().resolve("classes").resolve("templates");
109110
openApiFilesPaths
110111
.filter(Files::isRegularFile)
111112
.filter(path -> {
@@ -114,7 +115,8 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
114115
&& !filesToExclude.contains(fileName)
115116
&& (filesToInclude.isEmpty() || filesToInclude.contains(fileName));
116117
})
117-
.forEach(openApiFilePath -> generate(context.config(), openApiFilePath, outDir, isRestEasyReactive));
118+
.forEach(openApiFilePath -> generate(context.config(), openApiFilePath, outDir, templateDir,
119+
isRestEasyReactive));
118120
} catch (IOException e) {
119121
throw new CodeGenException("Failed to generate java files from OpenApi files in " + openApiDir.toAbsolutePath(),
120122
e);
@@ -146,8 +148,7 @@ private static boolean isExtensionCapabilityPresent(CodeGenContext context, Stri
146148

147149
// TODO: do not generate if the output dir has generated files and the openapi file has the same checksum of the previous run
148150
protected void generate(final Config config, final Path openApiFilePath, final Path outDir,
149-
boolean isRestEasyReactive) {
150-
151+
Path templateDir, boolean isRestEasyReactive) {
151152
final String basePackage = getBasePackage(config, openApiFilePath);
152153
final Boolean verbose = config.getOptionalValue(getGlobalConfigName(CodegenConfig.ConfigName.VERBOSE), Boolean.class)
153154
.orElse(false);
@@ -158,6 +159,8 @@ protected void generate(final Config config, final Path openApiFilePath, final P
158159
final OpenApiClientGeneratorWrapper generator = createGeneratorWrapper(openApiFilePath, outDir, isRestEasyReactive,
159160
verbose, validateSpec);
160161

162+
generator.withTemplateDir(templateDir);
163+
161164
generator.withClassesCodeGenConfig(ClassCodegenConfigParser.parse(config, basePackage))
162165
.withCircuitBreakerConfig(CircuitBreakerConfigurationParser.parse(
163166
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)