Skip to content

Commit c531df4

Browse files
committed
refactor: replace Collectors.toMap with custom collector for case-insensitive file name ordering to simplify lookup in the table while debugging tests.
1 parent 73dcdd6 commit c531df4

14 files changed

Lines changed: 139 additions & 130 deletions

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.swagger.v3.oas.models.media.Schema;
1616
import io.swagger.v3.oas.models.servers.Server;
1717
import io.swagger.v3.parser.core.models.ParseOptions;
18+
import org.jetbrains.annotations.NotNull;
1819
import org.openapitools.codegen.java.assertions.JavaFileAssert;
1920
import org.openapitools.codegen.model.ModelMap;
2021
import org.openapitools.codegen.model.ModelsMap;
@@ -24,12 +25,12 @@
2425
import java.io.IOException;
2526
import java.nio.file.Files;
2627
import java.nio.file.Path;
27-
import java.util.ArrayList;
28-
import java.util.Collections;
29-
import java.util.List;
30-
import java.util.Map;
28+
import java.util.*;
29+
import java.util.function.Function;
3130
import java.util.regex.Matcher;
3231
import java.util.regex.Pattern;
32+
import java.util.stream.Collector;
33+
import java.util.stream.Collectors;
3334

3435
import static org.testng.Assert.*;
3536

@@ -352,4 +353,22 @@ public static Path newTempFolder() {
352353

353354
return tempDir;
354355
}
356+
357+
358+
/**
359+
* Returns a collector that collects files into a {@link TreeMap} sorted by file name,
360+
* using case-insensitive ordering (but accepting both keys - e.g. "application" and "Application").
361+
* This is used to have files sorted by name (case-insensitive) to simplify lookup during test debugging.
362+
*/
363+
@NotNull
364+
public static Collector<File, ?, TreeMap<String, File>> collectToCaseInsensitiveOrderedCaseSensitiveKeyMap() {
365+
return Collectors.toMap(
366+
File::getName,
367+
Function.identity(),
368+
(existing, replacement) -> {
369+
throw new IllegalStateException("Duplicate key: " + existing);
370+
},
371+
() -> new TreeMap<>(Comparator.<String, String>comparing(s -> s.toLowerCase(Locale.ROOT))
372+
.thenComparing(Comparator.naturalOrder())));
373+
}
355374
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
import java.nio.file.Files;
1616
import java.nio.file.Paths;
1717
import java.util.Map;
18-
import java.util.function.Function;
19-
import java.util.stream.Collectors;
2018

19+
import static org.openapitools.codegen.TestUtils.collectToCaseInsensitiveOrderedCaseSensitiveKeyMap;
2120
import static org.openapitools.codegen.languages.SpringCodegen.*;
2221

2322
public class MergedSpecBuilderTest {
@@ -66,7 +65,7 @@ private void assertFilesFromMergedSpec(String mergedSpec) throws IOException {
6665

6766
DefaultGenerator generator = new DefaultGenerator();
6867
Map<String, File> files = generator.opts(input).generate().stream()
69-
.collect(Collectors.toMap(File::getName, Function.identity()));
68+
.collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
7069

7170
JavaFileAssert.assertThat(files.get("Spec1Api.java"))
7271
.assertMethod("spec1Operation").hasReturnType("ResponseEntity<Spec1Model>")

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

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import java.nio.file.Paths;
5959
import java.util.*;
6060
import java.util.function.Consumer;
61-
import java.util.function.Function;
6261
import java.util.function.Predicate;
6362
import java.util.regex.Matcher;
6463
import java.util.regex.Pattern;
@@ -1160,7 +1159,7 @@ public void shouldGenerateBlockingAndNoBlockingOperationsForWebClient() {
11601159

11611160
DefaultGenerator generator = new DefaultGenerator();
11621161
Map<String, File> files = generator.opts(configurator.toClientOptInput()).generate().stream()
1163-
.collect(Collectors.toMap(File::getName, Function.identity()));
1162+
.collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
11641163

11651164
JavaFileAssert.assertThat(files.get("StoreApi.java"))
11661165
.assertMethod("getInventory")
@@ -1579,7 +1578,7 @@ public void testMicroprofileGenerateCorrectJsonbCreator_issue12622() {
15791578
.setOutputDir(output.toString().replace("\\", "/"));
15801579

15811580
Map<String, File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate()
1582-
.stream().collect(Collectors.toMap(File::getName, Function.identity()));
1581+
.stream().collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
15831582

15841583
JavaFileAssert.assertThat(files.get("Foo.java"))
15851584
.assertConstructor("String", "Integer")
@@ -1613,7 +1612,7 @@ public void testMicroprofileGenerateCorrectJacksonGenerator_issue18336() throws
16131612
final ClientOptInput clientOptInput = configurator.toClientOptInput();
16141613
DefaultGenerator generator = new DefaultGenerator();
16151614
Map<String, File> files = generator.opts(clientOptInput).generate().stream()
1616-
.collect(Collectors.toMap(File::getName, Function.identity()));
1615+
.collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
16171616

16181617
JavaFileAssert.assertThat(files.get("Pet.java"))
16191618
.assertConstructor("String")
@@ -1636,7 +1635,7 @@ public void testJavaClientDefaultValues_issueNoNumber() {
16361635
.setInputSpec("src/test/resources/bugs/java-codegen-empty-array-as-default-value/issue_wrong-default.yaml");
16371636

16381637
Map<String, File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate()
1639-
.stream().collect(Collectors.toMap(File::getName, Function.identity()));
1638+
.stream().collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
16401639

16411640
JavaFileAssert.assertThat(files.get("DefaultValuesType.java"))
16421641
.assertProperty("stringDefault")
@@ -1660,7 +1659,7 @@ public void testWebClientJsonCreatorWithNullable_issue12790() {
16601659
.setOutputDir(output.toString().replace("\\", "/"));
16611660

16621661
Map<String, File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate().stream()
1663-
.collect(Collectors.toMap(File::getName, Function.identity()));
1662+
.collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
16641663

16651664
JavaFileAssert.assertThat(files.get("TestObject.java"))
16661665
.printFileContent()
@@ -1727,7 +1726,7 @@ public void testReferencedHeader2() {
17271726
.setOutputDir(output.toString().replace("\\", "/"));
17281727

17291728
Map<String, File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate().stream()
1730-
.collect(Collectors.toMap(File::getName, Function.identity()));
1729+
.collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
17311730

17321731
JavaFileAssert.assertThat(files.get("DefaultApi.java"))
17331732
.assertMethod("operationWithHttpInfo")
@@ -2044,7 +2043,7 @@ private static Map<String, File> generateFromContract(
20442043
.setOutputDir(output.toString());
20452044
consumer.accept(configurator);
20462045
return new DefaultGenerator().opts(configurator.toClientOptInput()).generate()
2047-
.stream().collect(Collectors.toMap(File::getName, Function.identity()));
2046+
.stream().collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
20482047
}
20492048

20502049
@Test
@@ -2200,7 +2199,7 @@ public void shouldNotAddAdditionalModelAnnotationsToAbstractOpenApiSchema_issue1
22002199
.setOutputDir(output.toString().replace("\\", "/"));
22012200

22022201
Map<String, File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate().stream()
2203-
.collect(Collectors.toMap(File::getName, Function.identity()));
2202+
.collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
22042203

22052204
JavaFileAssert.assertThat(files.get("AbstractOpenApiSchema.java"))
22062205
.assertTypeAnnotations()
@@ -2371,7 +2370,7 @@ public void testEnumCaseInsensitive_issue8084() {
23712370
codegen.additionalProperties().put(USE_ENUM_CASE_INSENSITIVE, "true");
23722371

23732372
Map<String, File> files = new DefaultGenerator().opts(new ClientOptInput().openAPI(openAPI).config(codegen))
2374-
.generate().stream().collect(Collectors.toMap(File::getName, Function.identity()));
2373+
.generate().stream().collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
23752374

23762375
JavaFileAssert.assertThat(files.get("EnumTest.java"))
23772376
.assertMethod("fromValue")
@@ -2387,7 +2386,7 @@ public void testEnumCaseSensitive_issue8084() {
23872386
codegen.additionalProperties().put(USE_ENUM_CASE_INSENSITIVE, "false");
23882387

23892388
Map<String, File> files = new DefaultGenerator().opts(new ClientOptInput().openAPI(openAPI).config(codegen))
2390-
.generate().stream().collect(Collectors.toMap(File::getName, Function.identity()));
2389+
.generate().stream().collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
23912390

23922391
JavaFileAssert.assertThat(files.get("EnumTest.java"))
23932392
.assertMethod("fromValue")
@@ -2402,7 +2401,7 @@ public void testMapOfInnerEnum_issue19393() {
24022401
codegen.setOutputDir(newTempFolder().toString());
24032402

24042403
Map<String, File> files = new DefaultGenerator().opts(new ClientOptInput().openAPI(openAPI).config(codegen))
2405-
.generate().stream().collect(Collectors.toMap(File::getName, Function.identity()));
2404+
.generate().stream().collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
24062405

24072406
JavaFileAssert.assertThat(files.get("EmployeeWithMapOfEnum.java"))
24082407
.assertProperty("projectRole")
@@ -2444,7 +2443,7 @@ public void testHandleConstantParams() {
24442443
codegen.setAutosetConstants(true);
24452444

24462445
Map<String, File> files = new DefaultGenerator().opts(new ClientOptInput().openAPI(openAPI).config(codegen))
2447-
.generate().stream().collect(Collectors.toMap(File::getName, Function.identity()));
2446+
.generate().stream().collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
24482447

24492448
File apiFile = files.get("HelloExampleApi.java");
24502449
Assertions.assertNotNull(apiFile);
@@ -2462,7 +2461,7 @@ public void testAllOfWithSinglePrimitiveTypeRef() {
24622461
codegen.setAutosetConstants(true);
24632462

24642463
Map<String, File> files = new DefaultGenerator().opts(new ClientOptInput().openAPI(openAPI).config(codegen))
2465-
.generate().stream().collect(Collectors.toMap(File::getName, Function.identity()));
2464+
.generate().stream().collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
24662465

24672466
assertNull(files.get("AllOfDatetime.java"));
24682467
}
@@ -2478,7 +2477,7 @@ public void testOpenapiGeneratorIgnoreListOption() {
24782477
codegen.openapiGeneratorIgnoreList().add("pom.xml");
24792478

24802479
Map<String, File> files = new DefaultGenerator().opts(new ClientOptInput().openAPI(openAPI).config(codegen))
2481-
.generate().stream().collect(Collectors.toMap(File::getName, Function.identity()));
2480+
.generate().stream().collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
24822481

24832482
// make sure README.md and pom.xml are not generated
24842483
assertNull(files.get("README.md"));
@@ -2494,7 +2493,7 @@ public void testEnumDiscriminatorDefaultValueIsNotString() {
24942493
codegen.setOutputDir(output.toString());
24952494

24962495
Map<String, File> files = new DefaultGenerator().opts(new ClientOptInput().openAPI(openAPI).config(codegen))
2497-
.generate().stream().collect(Collectors.toMap(File::getName, Function.identity()));
2496+
.generate().stream().collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
24982497

24992498
Map<String, String> expectedContents = Map.of(
25002499
"Cat", "this.petType = PetTypeEnum.CATTY",
@@ -2579,7 +2578,7 @@ private void testHandleURIEnum(String library, String[] expectedInnerEnumLines,
25792578
.setOutputDir(output.toString().replace("\\", "/"));
25802579

25812580
Map<String, File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate()
2582-
.stream().collect(Collectors.toMap(File::getName, Function.identity()));
2581+
.stream().collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
25832582

25842583
// enum
25852584
File modelFile = files.get("Metadata.java");
@@ -2756,7 +2755,7 @@ public void testRestClientJsonCreatorWithNullable_issue12790() {
27562755
.setOutputDir(output.toString().replace("\\", "/"));
27572756

27582757
Map<String, File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate().stream()
2759-
.collect(Collectors.toMap(File::getName, Function.identity()));
2758+
.collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
27602759

27612760
JavaFileAssert.assertThat(files.get("TestObject.java"))
27622761
.printFileContent()
@@ -3241,7 +3240,7 @@ public void testRestTemplateWithCustomUserAgent() {
32413240
.setOutputDir(output.toString().replace("\\", "/"));
32423241

32433242
final Map<String, File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate()
3244-
.stream().collect(Collectors.toMap(File::getName, Function.identity()));
3243+
.stream().collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
32453244

32463245
final JavaFileAssert apiClient = JavaFileAssert.assertThat(files.get("ApiClient.java"))
32473246
.printFileContent();
@@ -3531,7 +3530,7 @@ public void callNativeServiceWithEmptyResponseSync() throws IOException {
35313530
DefaultGenerator generator = new DefaultGenerator();
35323531

35333532
Map<String, File> files = generator.opts(clientOptInput).generate().stream()
3534-
.collect(Collectors.toMap(File::getName, Function.identity()));
3533+
.collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
35353534

35363535
File apiFile = files.get("DefaultApi.java");
35373536
assertNotNull(apiFile);
@@ -3563,7 +3562,7 @@ public void annotationLibraryDoesNotCauseImportConflicts() throws IOException {
35633562
DefaultGenerator generator = new DefaultGenerator();
35643563

35653564
Map<String, File> files = generator.opts(clientOptInput).generate().stream()
3566-
.collect(Collectors.toMap(File::getName, Function.identity()));
3565+
.collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
35673566

35683567
File apiFile = files.get("Schema.java");
35693568
assertNotNull(apiFile);
@@ -3592,7 +3591,7 @@ public void annotationLibraryGeneratesCorrectImports() throws IOException {
35923591
DefaultGenerator generator = new DefaultGenerator();
35933592

35943593
Map<String, File> files = generator.opts(clientOptInput).generate().stream()
3595-
.collect(Collectors.toMap(File::getName, Function.identity()));
3594+
.collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
35963595

35973596
File apiFile = files.get("Schema.java");
35983597
assertNotNull(apiFile);
@@ -3627,7 +3626,7 @@ public void callNativeServiceWithEmptyResponseAsync() throws IOException {
36273626
DefaultGenerator generator = new DefaultGenerator();
36283627

36293628
Map<String, File> files = generator.opts(clientOptInput).generate().stream()
3630-
.collect(Collectors.toMap(File::getName, Function.identity()));
3629+
.collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
36313630

36323631
File apiFile = files.get("DefaultApi.java");
36333632
assertNotNull(apiFile);
@@ -3648,7 +3647,7 @@ public void testEnumWithImplements() {
36483647
codegen.setOutputDir(output.toString());
36493648

36503649
Map<String, File> files = new DefaultGenerator().opts(new ClientOptInput().openAPI(openAPI).config(codegen))
3651-
.generate().stream().collect(Collectors.toMap(File::getName, Function.identity()));
3650+
.generate().stream().collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
36523651

36533652
JavaFileAssert.assertThat(files.get("Type.java")).fileContains("Type implements java.io.Serializable {");
36543653
}
@@ -3672,7 +3671,7 @@ public void testClassesAreValidJavaJersey2() {
36723671
.setOutputDir(output.toString().replace("\\", "/"));
36733672

36743673
Map<String, File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate().stream()
3675-
.collect(Collectors.toMap(File::getName, Function.identity()));
3674+
.collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
36763675

36773676
File oneOfFile = files.get("ResultObjectOneOf.java");
36783677
assertNotNull(oneOfFile);
@@ -3709,7 +3708,7 @@ public void testClassesAreValidJavaJersey3() {
37093708
.setOutputDir(output.toString().replace("\\", "/"));
37103709

37113710
Map<String, File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate().stream()
3712-
.collect(Collectors.toMap(File::getName, Function.identity()));
3711+
.collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
37133712

37143713
File oneOfFile = files.get("ResultObjectOneOf.java");
37153714
assertNotNull(oneOfFile);
@@ -3746,7 +3745,7 @@ public void testClassesAreValidJavaOkHttpGson() {
37463745
.setOutputDir(output.toString().replace("\\", "/"));
37473746

37483747
Map<String, File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate().stream()
3749-
.collect(Collectors.toMap(File::getName, Function.identity()));
3748+
.collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
37503749

37513750
File oneOfFile = files.get("ResultObjectOneOf.java");
37523751
assertNotNull(oneOfFile);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
import java.util.Collections;
3535
import java.util.Map;
3636
import java.util.function.Consumer;
37-
import java.util.function.Function;
38-
import java.util.stream.Collectors;
37+
38+
import static org.openapitools.codegen.TestUtils.collectToCaseInsensitiveOrderedCaseSensitiveKeyMap;
3939

4040
public class JavaValidationArrayPrimitivesTest {
4141
private static Consumer<Map<String, File>> assertWithValidationWithoutJsonNullable() {
@@ -225,7 +225,7 @@ public void shouldAddValidAnnotationIntoCollectionWhenBeanValidationIsEnabled_is
225225

226226
final DefaultGenerator generator = new DefaultGenerator();
227227
final Map<String, File> files = generator.opts(input).generate().stream()
228-
.collect(Collectors.toMap(File::getName, Function.identity()));
228+
.collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
229229

230230
asserts.accept(files);
231231
}
@@ -408,7 +408,7 @@ public void shouldNotAddValidAnnotationIntoCollectionWhenBeanValidationIsNotEnab
408408

409409
final DefaultGenerator generator = new DefaultGenerator();
410410
final Map<String, File> files = generator.opts(input).generate().stream()
411-
.collect(Collectors.toMap(File::getName, Function.identity()));
411+
.collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
412412

413413
asserts.accept(files);
414414
}
@@ -445,7 +445,7 @@ public void typeMappingsForCollections(Map<String, String> typeMappings, String
445445
input.config(codegen);
446446
final DefaultGenerator generator = new DefaultGenerator();
447447
Map<String, File> files = generator.opts(input).generate().stream()
448-
.collect(Collectors.toMap(File::getName, Function.identity()));
448+
.collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
449449

450450
String arrayMapping = typeMappings.getOrDefault("array", "List");
451451
// @Valid@Size(min = 5) is not nice, but not related to this fix

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import java.io.File;
1616
import java.nio.file.Files;
1717
import java.util.Map;
18-
import java.util.function.Function;
19-
import java.util.stream.Collectors;
18+
19+
import static org.openapitools.codegen.TestUtils.collectToCaseInsensitiveOrderedCaseSensitiveKeyMap;
2020

2121
public class JavaJAXRSCXFCDIServerCodegenTest extends JavaJaxrsBaseTest {
2222

@@ -42,7 +42,7 @@ public void testHandleDefaultValue_issue8535() throws Exception {
4242

4343
DefaultGenerator generator = new DefaultGenerator();
4444
Map<String, File> files = generator.opts(input).generate().stream()
45-
.collect(Collectors.toMap(File::getName, Function.identity()));
45+
.collect(collectToCaseInsensitiveOrderedCaseSensitiveKeyMap());
4646

4747
JavaFileAssert.assertThat(files.get("TestHeadersApi.java"))
4848
.assertMethod("headersTest")

0 commit comments

Comments
 (0)