Skip to content

Commit 7f34602

Browse files
Merge pull request #31 from quarkiverse/issue-28
Fix #28 - Add Enum Outer Class template and values cache
2 parents a9fab50 + 5ba0176 commit 7f34602

File tree

8 files changed

+1972
-20
lines changed

8 files changed

+1972
-20
lines changed

deployment/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@
102102
<artifactId>quarkus-junit5-internal</artifactId>
103103
<scope>test</scope>
104104
</dependency>
105+
<dependency>
106+
<groupId>com.github.javaparser</groupId>
107+
<artifactId>javaparser-core</artifactId>
108+
<scope>test</scope>
109+
</dependency>
110+
<dependency>
111+
<groupId>org.assertj</groupId>
112+
<artifactId>assertj-core</artifactId>
113+
<scope>test</scope>
114+
</dependency>
105115
</dependencies>
106116
<build>
107117
<plugins>

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.quarkiverse.openapi.generator.deployment.codegen;
22

3-
import static io.quarkiverse.openapi.generator.deployment.SpecConfig.API_PKG_SUFFIX;
4-
import static io.quarkiverse.openapi.generator.deployment.SpecConfig.MODEL_PKG_SUFFIX;
53
import static io.quarkiverse.openapi.generator.deployment.SpecConfig.getResolvedBasePackageProperty;
64

75
import java.io.IOException;
@@ -26,8 +24,6 @@ public abstract class OpenApiGeneratorCodeGenBase implements CodeGenProvider {
2624
static final String YML = ".yml";
2725
static final String JSON = ".json";
2826

29-
private static final String DEFAULT_PACKAGE = "org.openapi.quarkus";
30-
3127
@Override
3228
public String inputDirectory() {
3329
return "openapi";
@@ -45,14 +41,11 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
4541
.map(Path::toString)
4642
.filter(s -> s.endsWith(this.inputExtension()))
4743
.map(Path::of).forEach(openApiFilePath -> {
48-
final String basePackage = context.config()
49-
.getOptionalValue(getResolvedBasePackageProperty(openApiFilePath), String.class)
50-
.orElse(DEFAULT_PACKAGE);
5144
final OpenApiClientGeneratorWrapper generator = new OpenApiClientGeneratorWrapper(
52-
openApiFilePath.normalize(), outDir)
53-
.withApiPackage(basePackage + API_PKG_SUFFIX)
54-
.withModelPackage(basePackage + MODEL_PKG_SUFFIX)
55-
.withBasePackage(basePackage);
45+
openApiFilePath.normalize(), outDir);
46+
context.config()
47+
.getOptionalValue(getResolvedBasePackageProperty(openApiFilePath), String.class)
48+
.ifPresent(generator::withBasePackage);
5649
generator.generate();
5750
});
5851
} catch (IOException e) {

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import org.openapitools.codegen.DefaultGenerator;
1212
import org.openapitools.codegen.config.GlobalSettings;
1313

14+
import io.quarkiverse.openapi.generator.deployment.SpecConfig;
15+
1416
/**
1517
* Wrapper for the OpenAPIGen tool.
1618
* This is the same as calling the Maven plugin or the CLI.
@@ -22,10 +24,15 @@ public class OpenApiClientGeneratorWrapper {
2224

2325
private static final String VERBOSE = "verbose";
2426
private static final String ONCE_LOGGER = "org.openapitools.codegen.utils.oncelogger.enabled";
27+
private static final String DEFAULT_PACKAGE = "org.openapi.quarkus";
2528

2629
private final QuarkusCodegenConfigurator configurator;
2730
private final DefaultGenerator generator;
2831

32+
private String basePackage = DEFAULT_PACKAGE;
33+
private String apiPackage = "";
34+
private String modelPackage = "";
35+
2936
public OpenApiClientGeneratorWrapper(final Path specFilePath, final Path outputDir) {
3037
// do not generate docs nor tests
3138
GlobalSettings.setProperty(CodegenConstants.API_DOCS, FALSE.toString());
@@ -47,23 +54,39 @@ public OpenApiClientGeneratorWrapper(final Path specFilePath, final Path outputD
4754
}
4855

4956
public OpenApiClientGeneratorWrapper withApiPackage(final String pkg) {
50-
this.configurator.setApiPackage(pkg);
51-
this.configurator.setInvokerPackage(pkg);
57+
this.apiPackage = pkg;
5258
return this;
5359
}
5460

5561
public OpenApiClientGeneratorWrapper withModelPackage(final String pkg) {
56-
this.configurator.setModelPackage(pkg);
62+
this.modelPackage = pkg;
5763
return this;
5864
}
5965

6066
public OpenApiClientGeneratorWrapper withBasePackage(final String pkg) {
61-
this.configurator.setPackageName(pkg);
67+
this.basePackage = pkg;
6268
return this;
6369
}
6470

6571
public List<File> generate() {
72+
this.consolidatePackageNames();
6673
return generator.opts(configurator.toClientOptInput()).generate();
6774
}
6875

76+
private void consolidatePackageNames() {
77+
if (basePackage.isEmpty()) {
78+
basePackage = DEFAULT_PACKAGE;
79+
}
80+
if (apiPackage.isEmpty()) {
81+
this.apiPackage = basePackage.concat(SpecConfig.API_PKG_SUFFIX);
82+
}
83+
if (modelPackage.isEmpty()) {
84+
this.modelPackage = basePackage.concat(SpecConfig.MODEL_PKG_SUFFIX);
85+
}
86+
this.configurator.setPackageName(basePackage);
87+
this.configurator.setApiPackage(apiPackage);
88+
this.configurator.setModelPackage(modelPackage);
89+
this.configurator.setInvokerPackage(apiPackage);
90+
}
91+
6992
}

deployment/src/main/resources/templates/enumClass.qute

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
{/if}
1212
{/if}
1313

14+
// caching enum access
15+
private static final java.util.EnumSet<{e.datatypeWithEnum}> values = java.util.EnumSet.allOf({e.datatypeWithEnum}.class);
16+
1417
{e.dataType} value;
1518

1619
{e.datatypeWithEnum} ({e.dataType} v) {
@@ -26,14 +29,12 @@
2629
return String.valueOf(value);
2730
}
2831

29-
{#if e.withXml}
30-
public static {e.datatypeWithEnum} fromValue(String, v) {
31-
for ({#if e.datatypeWithEnum}{e.datatypeWithEnum}{#else}{e.classname}{/if} b : {#if e.datatypeWithEnum}{e.datatypeWithEnum}{#else}{e.classname}{/if}.values()) {
32+
public static {e.datatypeWithEnum} fromValue(String v) {
33+
for ({#if e.datatypeWithEnum}{e.datatypeWithEnum}{#else}{e.classname}{/if} b : values) {
3234
if (String.valueOf(b.value).equals(v)) {
3335
return b;
3436
}
3537
}
3638
{#if e.useNullForUnknownEnumValue}return null;{#else}throw new IllegalArgumentException("Unexpected value '" + v + "'");{/if}
3739
}
38-
{/if}
3940
}

deployment/src/main/resources/templates/enumOuterClass.qute

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import com.fasterxml.jackson.annotation.JsonCreator;
22
import com.fasterxml.jackson.annotation.JsonValue;
33

4+
import java.util.EnumSet;
5+
46
/**
57
* {#insert e.description}Gets or Sets {e.name}{/}{#if e.description}{description}{/}
68
*/
79
@JsonIgnoreProperties(ignoreUnknown = true)
810
{#include additionalEnumTypeAnnotations.qute e=e/}public enum {#if e.datatypeWithEnum}{e.datatypeWithEnum}{#else}{e.classname}{/if} {
11+
{#if e.allowableValues}
12+
{#for v in e.allowableValues.enumVars}{v.name}({e.dataType}.valueOf({v.value})){#if v_hasNext}, {#else};{/if}{/for}
13+
{/if}
14+
15+
// caching enum access
16+
private static final EnumSet<{#if e.datatypeWithEnum}{e.datatypeWithEnum}{#else}{e.classname}{/if}> values = EnumSet.allOf({#if e.datatypeWithEnum}{e.datatypeWithEnum}{#else}{e.classname}{/if}.class);
17+
918
private {e.dataType} value;
1019

1120
{#if e.datatypeWithEnum}{e.datatypeWithEnum}{#else}{e.classname}{/if}({e.dataType} value){
@@ -20,7 +29,7 @@ import com.fasterxml.jackson.annotation.JsonValue;
2029

2130
@JsonCreator
2231
public static {#if e.datatypeWithEnum}{e.datatypeWithEnum}{#else}{e.classname}{/if} fromValue(String text) {
23-
for ({#if e.datatypeWithEnum}{e.datatypeWithEnum}{#else}{e.classname}{/if} b : {#if e.datatypeWithEnum}{e.datatypeWithEnum}{#else}{e.classname}{/if}.values()) {
32+
for ({#if e.datatypeWithEnum}{e.datatypeWithEnum}{#else}{e.classname}{/if} b : values) {
2433
if (String.valueOf(b.value).equals(text)) {
2534
return b;
2635
}

deployment/src/test/java/io/quarkiverse/openapi/generator/deployment/wrapper/OpenApiClientGeneratorWrapperTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
package io.quarkiverse.openapi.generator.deployment.wrapper;
22

33
import static java.util.Objects.requireNonNull;
4+
import static org.assertj.core.api.Assertions.assertThat;
45
import static org.junit.jupiter.api.Assertions.assertFalse;
56
import static org.junit.jupiter.api.Assertions.assertNotNull;
67
import static org.junit.jupiter.api.Assertions.assertTrue;
78

89
import java.io.File;
10+
import java.io.FileNotFoundException;
911
import java.net.URISyntaxException;
1012
import java.nio.file.Path;
1113
import java.nio.file.Paths;
1214
import java.util.List;
15+
import java.util.Optional;
1316

1417
import org.junit.jupiter.api.Test;
1518

19+
import com.github.javaparser.StaticJavaParser;
20+
import com.github.javaparser.ast.CompilationUnit;
21+
import com.github.javaparser.ast.body.EnumConstantDeclaration;
22+
1623
public class OpenApiClientGeneratorWrapperTest {
1724

1825
@Test
@@ -46,6 +53,27 @@ void verifyAuthBearerGenerated() throws URISyntaxException {
4653
assertTrue(generatedFiles.stream().anyMatch(f -> f.getName().equals("CompositeAuthenticationProvider.java")));
4754
}
4855

56+
@Test
57+
void verifyEnumGeneration() throws URISyntaxException, FileNotFoundException {
58+
final Path issue28Path = Path
59+
.of(requireNonNull(this.getClass().getResource("/openapi/issue-28.yaml")).toURI());
60+
final Path targetPath = Paths.get(getTargetDir(), "openapi-gen");
61+
final OpenApiClientGeneratorWrapper generatorWrapper = new OpenApiClientGeneratorWrapper(issue28Path, targetPath)
62+
.withBasePackage("org.issue28");
63+
64+
final List<File> generatedFiles = generatorWrapper.generate();
65+
final Optional<File> enumFile = generatedFiles.stream()
66+
.filter(f -> f.getName().endsWith("ConnectorNamespaceState.java")).findFirst();
67+
assertThat(enumFile).isPresent();
68+
69+
final CompilationUnit cu = StaticJavaParser.parse(enumFile.orElseThrow());
70+
final List<EnumConstantDeclaration> constants = cu.findAll(EnumConstantDeclaration.class);
71+
assertThat(constants)
72+
.hasSize(3)
73+
.extracting(EnumConstantDeclaration::getNameAsString)
74+
.containsExactlyInAnyOrder("DISCONNECTED", "READY", "DELETING");
75+
}
76+
4977
private String getTargetDir() throws URISyntaxException {
5078
return Paths.get(requireNonNull(getClass().getResource("/")).toURI()).getParent().toString();
5179
}

0 commit comments

Comments
 (0)