Skip to content

Commit 9cc5513

Browse files
authored
Fix Issue 16385 (#16387)
1 parent 20692aa commit 9cc5513

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

docs/generators/java.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
8585
|testOutput|Set output folder for models and APIs tests| |${project.build.directory}/generated-test-sources/openapi|
8686
|useAbstractionForFiles|Use alternative types instead of java.io.File to allow passing bytes without a file on disk. Available on resttemplate, webclient, libraries| |false|
8787
|useBeanValidation|Use BeanValidation API annotations| |false|
88+
|useEnumCaseInsensitive|Use `equalsIgnoreCase` when String for enum comparison| |false|
8889
|useGzipFeature|Send gzip-encoded requests| |false|
8990
|useJakartaEe|whether to use Jakarta EE namespace instead of javax| |false|
9091
|useOneOfDiscriminatorLookup|Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and only one match in oneOf's schemas) will be skipped. Only jersey2, jersey3, native, okhttp-gson support this option.| |false|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
9696
public static final String MICROPROFILE_DEFAULT = "default";
9797
public static final String MICROPROFILE_KUMULUZEE = "kumuluzee";
9898
public static final String WEBCLIENT_BLOCKING_OPERATIONS = "webclientBlockingOperations";
99+
public static final String USE_ENUM_CASE_INSENSITIVE = "useEnumCaseInsensitive";
99100

100101
public static final String SERIALIZATION_LIBRARY_GSON = "gson";
101102
public static final String SERIALIZATION_LIBRARY_JACKSON = "jackson";
@@ -137,6 +138,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
137138
protected boolean useSingleRequestParameter = false;
138139
protected boolean webclientBlockingOperations = false;
139140
protected boolean generateClientAsBean = false;
141+
protected boolean useEnumCaseInsensitive = false;
140142

141143
private static class MpRestClientVersion {
142144
public final String rootPackage;
@@ -225,6 +227,7 @@ public JavaClientCodegen() {
225227
cliOptions.add(CliOption.newBoolean(WEBCLIENT_BLOCKING_OPERATIONS, "Making all WebClient operations blocking(sync). Note that if on operation 'x-webclient-blocking: false' then such operation won't be sync", this.webclientBlockingOperations));
226228
cliOptions.add(CliOption.newBoolean(GENERATE_CLIENT_AS_BEAN, "For resttemplate, configure whether to create `ApiClient.java` and Apis clients as bean (with `@Component` annotation).", this.generateClientAsBean));
227229
cliOptions.add(CliOption.newBoolean(SUPPORT_URL_QUERY, "Generate toUrlQueryString in POJO (default to true). Available on `native`, `apache-httpclient` libraries."));
230+
cliOptions.add(CliOption.newBoolean(USE_ENUM_CASE_INSENSITIVE, "Use `equalsIgnoreCase` when String for enum comparison", useEnumCaseInsensitive));
228231

229232
supportedLibraries.put(JERSEY1, "HTTP client: Jersey client 1.19.x. JSON processing: Jackson 2.9.x. Enable gzip request encoding using '-DuseGzipFeature=true'. IMPORTANT NOTE: jersey 1.x is no longer actively maintained so please upgrade to 'jersey3' or other HTTP libraries instead.");
230233
supportedLibraries.put(JERSEY2, "HTTP client: Jersey client 2.25.1. JSON processing: Jackson 2.9.x");
@@ -461,6 +464,11 @@ public void processOpts() {
461464
this.setGenerateClientAsBean(convertPropertyToBooleanAndWriteBack(GENERATE_CLIENT_AS_BEAN));
462465
}
463466

467+
if (additionalProperties.containsKey(USE_ENUM_CASE_INSENSITIVE)) {
468+
this.setUseEnumCaseInsensitive(Boolean.parseBoolean(additionalProperties.get(USE_ENUM_CASE_INSENSITIVE).toString()));
469+
}
470+
writePropertyBack(USE_ENUM_CASE_INSENSITIVE, useEnumCaseInsensitive);
471+
464472
final String invokerFolder = (sourceFolder + '/' + invokerPackage).replace(".", "/");
465473
final String apiFolder = (sourceFolder + '/' + apiPackage).replace(".", "/");
466474
final String modelsFolder = (sourceFolder + File.separator + modelPackage().replace('.', File.separatorChar)).replace('/', File.separatorChar);
@@ -1232,6 +1240,10 @@ public void setGenerateClientAsBean(boolean generateClientAsBean) {
12321240
this.generateClientAsBean = generateClientAsBean;
12331241
}
12341242

1243+
public void setUseEnumCaseInsensitive(boolean useEnumCaseInsensitive) {
1244+
this.useEnumCaseInsensitive = useEnumCaseInsensitive;
1245+
}
1246+
12351247
/**
12361248
* Serialization library.
12371249
*

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import static org.openapitools.codegen.TestUtils.assertFileContains;
8484
import static org.openapitools.codegen.TestUtils.assertFileNotContains;
8585
import static org.openapitools.codegen.TestUtils.validateJavaSourceFiles;
86+
import static org.openapitools.codegen.languages.JavaClientCodegen.USE_ENUM_CASE_INSENSITIVE;
8687
import static org.testng.Assert.assertEquals;
8788
import static org.testng.Assert.assertTrue;
8889
import static org.testng.Assert.fail;
@@ -2331,4 +2332,56 @@ public void testWebClientSupportListOfStringReturnType_issue7118() throws IOExce
23312332
);
23322333
}
23332334

2335+
@Test
2336+
public void testEnumCaseInsensitive_issue8084() throws IOException {
2337+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
2338+
output.deleteOnExit();
2339+
2340+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/issue8084.yaml");
2341+
final JavaClientCodegen codegen = new JavaClientCodegen();
2342+
codegen.setOpenAPI(openAPI);
2343+
codegen.setOutputDir(output.getAbsolutePath());
2344+
codegen.additionalProperties().put(USE_ENUM_CASE_INSENSITIVE, "true");
2345+
2346+
ClientOptInput input = new ClientOptInput();
2347+
input.openAPI(openAPI);
2348+
input.config(codegen);
2349+
2350+
DefaultGenerator generator = new DefaultGenerator();
2351+
2352+
Map<String, File> files = generator.opts(input).generate().stream()
2353+
.collect(Collectors.toMap(File::getName, Function.identity()));
2354+
2355+
JavaFileAssert javaFileAssert = JavaFileAssert.assertThat(files.get("EnumTest.java"));
2356+
javaFileAssert
2357+
.assertMethod("fromValue")
2358+
.bodyContainsLines("if (b.value.equalsIgnoreCase(value)) {");
2359+
}
2360+
2361+
@Test
2362+
public void testEnumCaseSensitive_issue8084() throws IOException {
2363+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
2364+
output.deleteOnExit();
2365+
2366+
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/issue8084.yaml");
2367+
final JavaClientCodegen codegen = new JavaClientCodegen();
2368+
codegen.setOpenAPI(openAPI);
2369+
codegen.setOutputDir(output.getAbsolutePath());
2370+
codegen.additionalProperties().put(USE_ENUM_CASE_INSENSITIVE, "false");
2371+
2372+
ClientOptInput input = new ClientOptInput();
2373+
input.openAPI(openAPI);
2374+
input.config(codegen);
2375+
2376+
DefaultGenerator generator = new DefaultGenerator();
2377+
2378+
Map<String, File> files = generator.opts(input).generate().stream()
2379+
.collect(Collectors.toMap(File::getName, Function.identity()));
2380+
2381+
JavaFileAssert javaFileAssert = JavaFileAssert.assertThat(files.get("EnumTest.java"));
2382+
javaFileAssert
2383+
.assertMethod("fromValue")
2384+
.bodyContainsLines("if (b.value.equals(value)) {");
2385+
}
2386+
23342387
}

0 commit comments

Comments
 (0)