Skip to content

Commit a6e4d9b

Browse files
Add unit tests for bug in imports for oneOf schemas with additional properties set to true #21587
1 parent 7c1dce4 commit a6e4d9b

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import io.swagger.v3.oas.models.media.Schema;
88
import io.swagger.v3.oas.models.media.StringSchema;
99
import java.util.Collections;
10+
import java.util.Locale;
11+
import java.util.stream.Stream;
1012
import org.apache.commons.lang3.StringUtils;
1113
import org.openapitools.codegen.*;
1214
import org.openapitools.codegen.config.CodegenConfigurator;
@@ -404,6 +406,43 @@ public void testOneOfModelsDoNotImportPrimitiveTypes() throws IOException {
404406
TestUtils.assertFileContains(testDiscriminatorResponse, "export type TestDiscriminatorResponse = { discriminatorField: 'optionOne' } & OptionOne | { discriminatorField: 'optionTwo' } & OptionTwo");
405407
}
406408

409+
/**
410+
* Issue #21587
411+
* When using oneOf, the Typescript Fetch generator should import modelled types except for
412+
* types built-in primitive types, even those marked with additional properties.
413+
*/
414+
@Test()
415+
public void testOneOfModelsImportNonPrimitiveTypes() throws IOException {
416+
File output = generate(
417+
Collections.emptyMap(),
418+
"src/test/resources/3_0/typescript-fetch/issue_21587.yaml"
419+
);
420+
421+
Path testResponse = Paths.get(output + "/models/OneOfResponse.ts");
422+
TestUtils.assertFileExists(testResponse);
423+
424+
// Primitive built-in types should not be included. This list is based off the type mappings
425+
// and language specific primitive keywords established in the AbstractTypeScriptClientCodegen
426+
Stream.of(
427+
"Set",
428+
"Array",
429+
"boolean",
430+
"string",
431+
"number",
432+
"object",
433+
"any",
434+
"Date",
435+
"Error"
436+
).forEach(primitiveType ->
437+
TestUtils.assertFileNotContains(
438+
testResponse,
439+
String.format(Locale.ROOT, "import type { %s } from './%s'", primitiveType, primitiveType)
440+
)
441+
);
442+
TestUtils.assertFileContains(testResponse, "import type { OptionOne } from './OptionOne'");
443+
TestUtils.assertFileContains(testResponse, "import type { OptionTwo } from './OptionTwo'");
444+
}
445+
407446
private static File generate(
408447
Map<String, Object> properties
409448
) throws IOException {

0 commit comments

Comments
 (0)