Skip to content

Commit a8069b0

Browse files
committed
Fix #28 - Add Enum Outer Class template and values cache
Signed-off-by: Ricardo Zanini <[email protected]>
1 parent a9fab50 commit a8069b0

File tree

5 files changed

+1905
-6
lines changed

5 files changed

+1905
-6
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55

66
import java.io.File;
77
import java.nio.file.Path;
8+
import java.util.EnumSet;
89
import java.util.List;
910

1011
import org.openapitools.codegen.CodegenConstants;
1112
import org.openapitools.codegen.DefaultGenerator;
1213
import org.openapitools.codegen.config.GlobalSettings;
1314

15+
import io.quarkiverse.openapi.generator.providers.ApiKeyIn;
16+
1417
/**
1518
* Wrapper for the OpenAPIGen tool.
1619
* This is the same as calling the Maven plugin or the CLI.
@@ -39,7 +42,7 @@ public OpenApiClientGeneratorWrapper(final Path specFilePath, final Path outputD
3942
// logging
4043
GlobalSettings.setProperty(VERBOSE, FALSE.toString());
4144
GlobalSettings.setProperty(ONCE_LOGGER, TRUE.toString());
42-
45+
EnumSet<ApiKeyIn> values = EnumSet.allOf(ApiKeyIn.class);
4346
this.configurator = new QuarkusCodegenConfigurator();
4447
this.configurator.setInputSpec(specFilePath.toString());
4548
this.configurator.setOutputDir(outputDir.toString());

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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ void verifyAuthBearerGenerated() throws URISyntaxException {
4646
assertTrue(generatedFiles.stream().anyMatch(f -> f.getName().equals("CompositeAuthenticationProvider.java")));
4747
}
4848

49+
@Test
50+
void verifyEnumGeneration() throws URISyntaxException {
51+
final Path petstoreOpenApi = Path
52+
.of(requireNonNull(this.getClass().getResource("/openapi/issue-28.yaml")).toURI());
53+
final Path targetPath = Paths.get(getTargetDir(), "openapi-gen");
54+
final OpenApiClientGeneratorWrapper generatorWrapper = new OpenApiClientGeneratorWrapper(petstoreOpenApi, targetPath)
55+
.withBasePackage("org.issue28")
56+
.withApiPackage("org.issue28.api")
57+
.withModelPackage("org.issue28.package");
58+
final List<File> generatedFiles = generatorWrapper.generate();
59+
60+
// TODO: add verification via java parser
61+
}
62+
4963
private String getTargetDir() throws URISyntaxException {
5064
return Paths.get(requireNonNull(getClass().getResource("/")).toURI()).getParent().toString();
5165
}

0 commit comments

Comments
 (0)