|
7 | 7 | import io.swagger.v3.oas.models.media.Schema; |
8 | 8 | import io.swagger.v3.oas.models.media.StringSchema; |
9 | 9 | import java.util.Collections; |
| 10 | +import java.util.Locale; |
| 11 | +import java.util.stream.Stream; |
10 | 12 | import org.apache.commons.lang3.StringUtils; |
11 | 13 | import org.openapitools.codegen.*; |
12 | 14 | import org.openapitools.codegen.config.CodegenConfigurator; |
@@ -404,6 +406,43 @@ public void testOneOfModelsDoNotImportPrimitiveTypes() throws IOException { |
404 | 406 | TestUtils.assertFileContains(testDiscriminatorResponse, "export type TestDiscriminatorResponse = { discriminatorField: 'optionOne' } & OptionOne | { discriminatorField: 'optionTwo' } & OptionTwo"); |
405 | 407 | } |
406 | 408 |
|
| 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 | + |
407 | 446 | private static File generate( |
408 | 447 | Map<String, Object> properties |
409 | 448 | ) throws IOException { |
|
0 commit comments