diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java index fe890c6dfaf6..e8a93a8f947a 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java @@ -6,6 +6,7 @@ import io.swagger.v3.oas.models.media.MapSchema; import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.media.StringSchema; +import java.util.Collections; import org.apache.commons.lang3.StringUtils; import org.openapitools.codegen.*; import org.openapitools.codegen.config.CodegenConfigurator; @@ -333,7 +334,6 @@ public void testGeneratedFilenamesInCamelCase() throws IOException { @Test(description = "Verify names of files generated in camelCase and imports with additional model prefix") public void testGeneratedFilenamesInCamelCaseWithAdditionalModelPrefix() throws IOException { - Map properties = new HashMap<>(); properties.put("fileNaming", TypeScriptFetchClientCodegen.CAMEL_CASE); properties.put(CodegenConstants.MODEL_NAME_PREFIX, "SomePrefix"); @@ -349,19 +349,10 @@ public void testGeneratedFilenamesInCamelCaseWithAdditionalModelPrefix() throws @Test(description = "Issue #21295") public void givenSchemaIsOneOfAndComposedSchemasArePrimitiveThenReturnStatementsAreCorrect() throws Exception { - File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); - output.deleteOnExit(); - String outputPath = output.getAbsolutePath(); - - - TypeScriptFetchClientCodegen clientCodegen = new TypeScriptFetchClientCodegen(); - clientCodegen.setOutputDir(outputPath); - - DefaultGenerator defaultGenerator = new DefaultGenerator(); - defaultGenerator.opts( - new ClientOptInput().openAPI(TestUtils.parseSpec("src/test/resources/bugs/issue_21259.yaml")) - .config(clientCodegen) - ).generate(); + File outputPath = generate( + Collections.emptyMap(), + "src/test/resources/bugs/issue_21259.yaml" + ); Path exampleModelPath = Paths.get(outputPath + "/models/MyCustomSpeed.ts"); //FromJSON @@ -370,23 +361,66 @@ public void givenSchemaIsOneOfAndComposedSchemasArePrimitiveThenReturnStatements TestUtils.assertFileContains(exampleModelPath, "json === 'fixed-value-a' || json === 'fixed-value-b' || json === 'fixed-value-c'"); TestUtils.assertFileContains(exampleModelPath, "isNaN(new Date(json).getTime())"); TestUtils.assertFileContains(exampleModelPath, "json.every(item => typeof item === 'number'"); -// TestUtils.assertFileContains(exampleModelPath, "json.every(item => typeof item === 'string' && (item === 'oneof-array-enum-a' || item oneof-array-enum-b || item === oneof-array-enum-c)"); + TestUtils.assertFileContains(exampleModelPath, "json.every(item => typeof item === 'string' && (item === 'oneof-array-enum-a' || item === 'oneof-array-enum-b' || item === 'oneof-array-enum-c')"); //ToJSON TestUtils.assertFileContains(exampleModelPath, "typeof value === 'number'"); TestUtils.assertFileContains(exampleModelPath, "typeof value === 'string'"); TestUtils.assertFileContains(exampleModelPath, "value === 'fixed-value-a' || value === 'fixed-value-b' || value === 'fixed-value-c'"); TestUtils.assertFileContains(exampleModelPath, "value instanceof Date"); TestUtils.assertFileContains(exampleModelPath, "value.every(item => typeof item === 'number'"); -// TestUtils.assertFileContains(exampleModelPath, "value.every(item => typeof item === 'string' && (item === 'oneof-array-enum-a' || item oneof-array-enum-b || item === oneof-array-enum-c)"); + TestUtils.assertFileContains(exampleModelPath, "value.every(item => typeof item === 'string' && (item === 'oneof-array-enum-a' || item === 'oneof-array-enum-b' || item === 'oneof-array-enum-c')"); + } + + /** + * Issue #19909 + * When using oneOf, the Typescript Fetch generator should not import primitive types. + * Complex types should be imported, when the response has the type itself or the type is part + * of an array. + */ + @Test(description = "Verify oneOf model files do not import primitive types") + public void testOneOfModelsDoNotImportPrimitiveTypes() throws IOException { + File output = generate(Collections.emptyMap(), "src/test/resources/3_0/typescript-fetch/oneOf.yaml"); + + Path testResponse = Paths.get(output + "/models/TestResponse.ts"); + TestUtils.assertFileExists(testResponse); + TestUtils.assertFileContains(testResponse, "import type { TestA } from './TestA'"); + TestUtils.assertFileContains(testResponse, "import type { TestB } from './TestB'"); + TestUtils.assertFileNotContains(testResponse, "import type { string } from './string'"); + TestUtils.assertFileContains(testResponse, "export type TestResponse = TestA | TestB | string"); + + Path testArrayResponse = Paths.get(output + "/models/TestArrayResponse.ts"); + TestUtils.assertFileExists(testArrayResponse); + TestUtils.assertFileContains(testArrayResponse, "import type { TestA } from './TestA'"); + TestUtils.assertFileContains(testArrayResponse, "import type { TestB } from './TestB'"); + TestUtils.assertFileNotContains(testResponse, "import type { string } from './string'"); + TestUtils.assertFileContains(testArrayResponse, "export type TestArrayResponse = Array | Array | Array"); + + Path testDiscriminatorResponse = Paths.get(output + "/models/TestDiscriminatorResponse.ts"); + TestUtils.assertFileExists(testDiscriminatorResponse); + TestUtils.assertFileContains(testDiscriminatorResponse, "import type { OptionOne } from './OptionOne'"); + TestUtils.assertFileContains(testDiscriminatorResponse, "import type { OptionTwo } from './OptionTwo'"); + TestUtils.assertFileContains(testDiscriminatorResponse, "export type TestDiscriminatorResponse = { discriminatorField: 'optionOne' } & OptionOne | { discriminatorField: 'optionTwo' } & OptionTwo"); + } + + private static File generate( + Map properties + ) throws IOException { + return generate( + properties, + "src/test/resources/3_0/typescript-fetch/example-for-file-naming-option.yaml" + ); } - private static File generate(Map properties) throws IOException { + private static File generate( + Map properties, + String inputSpec + ) throws IOException { File output = Files.createTempDirectory("test").toFile(); output.deleteOnExit(); final CodegenConfigurator configurator = new CodegenConfigurator() .setGeneratorName("typescript-fetch") - .setInputSpec("src/test/resources/3_0/typescript-fetch/example-for-file-naming-option.yaml") + .setInputSpec(inputSpec) .setAdditionalProperties(properties) .setOutputDir(output.getAbsolutePath().replace("\\", "/")); diff --git a/modules/openapi-generator/src/test/resources/bugs/issue_21259.yaml b/modules/openapi-generator/src/test/resources/bugs/issue_21259.yaml index d0d732e01411..ca6adc219c42 100644 --- a/modules/openapi-generator/src/test/resources/bugs/issue_21259.yaml +++ b/modules/openapi-generator/src/test/resources/bugs/issue_21259.yaml @@ -44,16 +44,14 @@ components: type: number - type: array items: - - type: object + type: object - type: array items: - - type: string - enum: - # It seems enums within arrays don't work. Leaving this here, though - - "oneof-array-enum-a" - - "oneof-array-enum-b" - - "oneof-array-enum-c" - - type: + type: string + enum: + - "oneof-array-enum-a" + - "oneof-array-enum-b" + - "oneof-array-enum-c" description: A value that can be a number or a specific string. TestPayload: type: object