Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.servers.Server;
import io.swagger.v3.parser.core.models.ParseOptions;
import org.jetbrains.annotations.NotNull;
import org.openapitools.codegen.java.assertions.JavaFileAssert;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.ModelsMap;
Expand All @@ -24,12 +25,12 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import static org.testng.Assert.*;

Expand Down Expand Up @@ -352,4 +353,22 @@ public static Path newTempFolder() {

return tempDir;
}


/**
* Returns a collector that collects files into a {@link TreeMap} sorted by file name,
* using case-insensitive ordering (but accepting both keys - e.g. "application" and "Application").
* This is used to have files sorted by name (case-insensitive) to simplify lookup during test debugging.
*/
@NotNull
public static Collector<File, ?, TreeMap<String, File>> collectToCaseInsensitiveOrderedCaseSensitiveKeyMap() {
return Collectors.toMap(
File::getName,
Function.identity(),
(existing, replacement) -> {
throw new IllegalStateException("Duplicate key: " + existing);
},
() -> new TreeMap<>(Comparator.<String, String>comparing(s -> s.toLowerCase(Locale.ROOT))
.thenComparing(Comparator.naturalOrder())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.openapitools.codegen.TestUtils.collectToCaseInsensitiveOrderedCaseSensitiveKeyMap;
import static org.openapitools.codegen.languages.SpringCodegen.*;

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

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

JavaFileAssert.assertThat(files.get("Spec1Api.java"))
.assertMethod("spec1Operation").hasReturnType("ResponseEntity<Spec1Model>")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -1160,7 +1159,7 @@ public void shouldGenerateBlockingAndNoBlockingOperationsForWebClient() {

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

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

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

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

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

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

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

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

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

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

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

@Test
Expand Down Expand Up @@ -2200,7 +2199,7 @@ public void shouldNotAddAdditionalModelAnnotationsToAbstractOpenApiSchema_issue1
.setOutputDir(output.toString().replace("\\", "/"));

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

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

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

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

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

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

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

JavaFileAssert.assertThat(files.get("EmployeeWithMapOfEnum.java"))
.assertProperty("projectRole")
Expand Down Expand Up @@ -2444,7 +2443,7 @@ public void testHandleConstantParams() {
codegen.setAutosetConstants(true);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

File oneOfFile = files.get("ResultObjectOneOf.java");
assertNotNull(oneOfFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import java.util.Collections;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.openapitools.codegen.TestUtils.collectToCaseInsensitiveOrderedCaseSensitiveKeyMap;

public class JavaValidationArrayPrimitivesTest {
private static Consumer<Map<String, File>> assertWithValidationWithoutJsonNullable() {
Expand Down Expand Up @@ -225,7 +225,7 @@ public void shouldAddValidAnnotationIntoCollectionWhenBeanValidationIsEnabled_is

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

asserts.accept(files);
}
Expand Down Expand Up @@ -408,7 +408,7 @@ public void shouldNotAddValidAnnotationIntoCollectionWhenBeanValidationIsNotEnab

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

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

String arrayMapping = typeMappings.getOrDefault("array", "List");
// @Valid@Size(min = 5) is not nice, but not related to this fix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import java.io.File;
import java.nio.file.Files;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.openapitools.codegen.TestUtils.collectToCaseInsensitiveOrderedCaseSensitiveKeyMap;

public class JavaJAXRSCXFCDIServerCodegenTest extends JavaJaxrsBaseTest {

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

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

JavaFileAssert.assertThat(files.get("TestHeadersApi.java"))
.assertMethod("headersTest")
Expand Down
Loading
Loading