Skip to content

Commit

Permalink
Added support for custom templates (#439)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
hbelmiro and ricardozanini authored Aug 16, 2023
1 parent bd23c86 commit 64151bc
Show file tree
Hide file tree
Showing 30 changed files with 683 additions and 4 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,18 @@ It's also possible to only use a type mapping with a fully qualified name, for i

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

## Template Customization

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.

You can find an example of using customized templates in [integration-tests/custom-templates](integration-tests/custom-templates).

### **⚠️** Important

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.

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.

## Known Limitations

These are the known limitations of this pre-release version:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
}

try (Stream<Path> openApiFilesPaths = Files.walk(openApiDir)) {
Path templateDir = context.workDir().resolve("classes").resolve("templates");
openApiFilesPaths
.filter(Files::isRegularFile)
.filter(path -> {
Expand All @@ -114,7 +115,8 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
&& !filesToExclude.contains(fileName)
&& (filesToInclude.isEmpty() || filesToInclude.contains(fileName));
})
.forEach(openApiFilePath -> generate(context.config(), openApiFilePath, outDir, isRestEasyReactive));
.forEach(openApiFilePath -> generate(context.config(), openApiFilePath, outDir, templateDir,
isRestEasyReactive));
} catch (IOException e) {
throw new CodeGenException("Failed to generate java files from OpenApi files in " + openApiDir.toAbsolutePath(),
e);
Expand Down Expand Up @@ -146,8 +148,7 @@ private static boolean isExtensionCapabilityPresent(CodeGenContext context, Stri

// TODO: do not generate if the output dir has generated files and the openapi file has the same checksum of the previous run
protected void generate(final Config config, final Path openApiFilePath, final Path outDir,
boolean isRestEasyReactive) {

Path templateDir, boolean isRestEasyReactive) {
final String basePackage = getBasePackage(config, openApiFilePath);
final Boolean verbose = config.getOptionalValue(getGlobalConfigName(CodegenConfig.ConfigName.VERBOSE), Boolean.class)
.orElse(false);
Expand All @@ -158,6 +159,8 @@ protected void generate(final Config config, final Path openApiFilePath, final P
final OpenApiClientGeneratorWrapper generator = createGeneratorWrapper(openApiFilePath, outDir, isRestEasyReactive,
verbose, validateSpec);

generator.withTemplateDir(templateDir);

generator.withClassesCodeGenConfig(ClassCodegenConfigParser.parse(config, basePackage))
.withCircuitBreakerConfig(CircuitBreakerConfigurationParser.parse(
config));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
StandardOpenOption.CREATE)) {
outChannel.transferFrom(inChannel, 0, Integer.MAX_VALUE);
LOGGER.debug("Saved OpenAPI spec input model in {}", openApiFilePath);
this.generate(this.mergeConfig(context, inputModel), openApiFilePath, outDir, isRestEasyReactive);
this.generate(this.mergeConfig(context, inputModel), openApiFilePath, outDir,
context.workDir().resolve("classes").resolve("templates"), isRestEasyReactive);
generated = true;
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ public OpenApiClientGeneratorWrapper withAdditionalApiTypeAnnotationsConfig(fina
return this;
}

public void withTemplateDir(Path templateDir) {
this.configurator.addAdditionalProperty("templateDir", templateDir.toString());
}

public List<File> generate(final String basePackage) {
this.basePackage = basePackage;
this.consolidatePackageNames();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public void processOpts() {
this.projectTestFolder = "";
this.sourceFolder = "";
this.testFolder = "";
this.embeddedTemplateDir = "templates";

this.replaceWithQuarkusTemplateFiles();
}
Expand Down
90 changes: 90 additions & 0 deletions integration-tests/custom-templates/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>quarkus-openapi-generator-integration-tests</artifactId>
<groupId>io.quarkiverse.openapi.generator</groupId>
<version>3.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-openapi-generator-it-custom-templates</artifactId>
<name>Quarkus - Openapi Generator - Integration Tests - Custom Templates</name>
<description>Example project with custom templates</description>

<dependencies>
<dependency>
<groupId>io.quarkiverse.openapi.generator</groupId>
<artifactId>quarkus-openapi-generator</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native-image</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${native.surefire.skip}</skipTests>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>

</project>
Loading

0 comments on commit 64151bc

Please sign in to comment.