66import io .swagger .v3 .oas .models .media .MapSchema ;
77import io .swagger .v3 .oas .models .media .Schema ;
88import io .swagger .v3 .oas .models .media .StringSchema ;
9+ import java .util .Collections ;
910import org .apache .commons .lang3 .StringUtils ;
1011import org .openapitools .codegen .*;
1112import org .openapitools .codegen .config .CodegenConfigurator ;
@@ -333,7 +334,6 @@ public void testGeneratedFilenamesInCamelCase() throws IOException {
333334
334335 @ Test (description = "Verify names of files generated in camelCase and imports with additional model prefix" )
335336 public void testGeneratedFilenamesInCamelCaseWithAdditionalModelPrefix () throws IOException {
336-
337337 Map <String , Object > properties = new HashMap <>();
338338 properties .put ("fileNaming" , TypeScriptFetchClientCodegen .CAMEL_CASE );
339339 properties .put (CodegenConstants .MODEL_NAME_PREFIX , "SomePrefix" );
@@ -349,19 +349,10 @@ public void testGeneratedFilenamesInCamelCaseWithAdditionalModelPrefix() throws
349349
350350 @ Test (description = "Issue #21295" )
351351 public void givenSchemaIsOneOfAndComposedSchemasArePrimitiveThenReturnStatementsAreCorrect () throws Exception {
352- File output = Files .createTempDirectory ("test" ).toFile ().getCanonicalFile ();
353- output .deleteOnExit ();
354- String outputPath = output .getAbsolutePath ();
355-
356-
357- TypeScriptFetchClientCodegen clientCodegen = new TypeScriptFetchClientCodegen ();
358- clientCodegen .setOutputDir (outputPath );
359-
360- DefaultGenerator defaultGenerator = new DefaultGenerator ();
361- defaultGenerator .opts (
362- new ClientOptInput ().openAPI (TestUtils .parseSpec ("src/test/resources/bugs/issue_21259.yaml" ))
363- .config (clientCodegen )
364- ).generate ();
352+ File outputPath = generate (
353+ Collections .emptyMap (),
354+ "src/test/resources/bugs/issue_21259.yaml"
355+ );
365356
366357 Path exampleModelPath = Paths .get (outputPath + "/models/MyCustomSpeed.ts" );
367358 //FromJSON
@@ -370,23 +361,66 @@ public void givenSchemaIsOneOfAndComposedSchemasArePrimitiveThenReturnStatements
370361 TestUtils .assertFileContains (exampleModelPath , "json === 'fixed-value-a' || json === 'fixed-value-b' || json === 'fixed-value-c'" );
371362 TestUtils .assertFileContains (exampleModelPath , "isNaN(new Date(json).getTime())" );
372363 TestUtils .assertFileContains (exampleModelPath , "json.every(item => typeof item === 'number'" );
373- // TestUtils.assertFileContains(exampleModelPath, "json.every(item => typeof item === 'string' && (item === 'oneof-array-enum-a' || item oneof-array-enum-b || item === oneof-array-enum-c)");
364+ TestUtils .assertFileContains (exampleModelPath , "json.every(item => typeof item === 'string' && (item === 'oneof-array-enum-a' || item === ' oneof-array-enum-b' || item === ' oneof-array-enum-c' )" );
374365 //ToJSON
375366 TestUtils .assertFileContains (exampleModelPath , "typeof value === 'number'" );
376367 TestUtils .assertFileContains (exampleModelPath , "typeof value === 'string'" );
377368 TestUtils .assertFileContains (exampleModelPath , "value === 'fixed-value-a' || value === 'fixed-value-b' || value === 'fixed-value-c'" );
378369 TestUtils .assertFileContains (exampleModelPath , "value instanceof Date" );
379370 TestUtils .assertFileContains (exampleModelPath , "value.every(item => typeof item === 'number'" );
380- // TestUtils.assertFileContains(exampleModelPath, "value.every(item => typeof item === 'string' && (item === 'oneof-array-enum-a' || item oneof-array-enum-b || item === oneof-array-enum-c)");
371+ TestUtils .assertFileContains (exampleModelPath , "value.every(item => typeof item === 'string' && (item === 'oneof-array-enum-a' || item === 'oneof-array-enum-b' || item === 'oneof-array-enum-c')" );
372+ }
373+
374+ /**
375+ * Issue #19909
376+ * When using oneOf, the Typescript Fetch generator should not import primitive types.
377+ * Complex types should be imported, when the response has the type itself or the type is part
378+ * of an array.
379+ */
380+ @ Test (description = "Verify oneOf model files do not import primitive types" )
381+ public void testOneOfModelsDoNotImportPrimitiveTypes () throws IOException {
382+ File output = generate (Collections .emptyMap (), "src/test/resources/3_0/typescript-fetch/oneOf.yaml" );
383+
384+ Path testResponse = Paths .get (output + "/models/TestResponse.ts" );
385+ TestUtils .assertFileExists (testResponse );
386+ TestUtils .assertFileContains (testResponse , "import type { TestA } from './TestA'" );
387+ TestUtils .assertFileContains (testResponse , "import type { TestB } from './TestB'" );
388+ TestUtils .assertFileNotContains (testResponse , "import type { string } from './string'" );
389+ TestUtils .assertFileContains (testResponse , "export type TestResponse = TestA | TestB | string" );
390+
391+ Path testArrayResponse = Paths .get (output + "/models/TestArrayResponse.ts" );
392+ TestUtils .assertFileExists (testArrayResponse );
393+ TestUtils .assertFileContains (testArrayResponse , "import type { TestA } from './TestA'" );
394+ TestUtils .assertFileContains (testArrayResponse , "import type { TestB } from './TestB'" );
395+ TestUtils .assertFileNotContains (testResponse , "import type { string } from './string'" );
396+ TestUtils .assertFileContains (testArrayResponse , "export type TestArrayResponse = Array<TestA> | Array<TestB> | Array<string>" );
397+
398+ Path testDiscriminatorResponse = Paths .get (output + "/models/TestDiscriminatorResponse.ts" );
399+ TestUtils .assertFileExists (testDiscriminatorResponse );
400+ TestUtils .assertFileContains (testDiscriminatorResponse , "import type { OptionOne } from './OptionOne'" );
401+ TestUtils .assertFileContains (testDiscriminatorResponse , "import type { OptionTwo } from './OptionTwo'" );
402+ TestUtils .assertFileContains (testDiscriminatorResponse , "export type TestDiscriminatorResponse = { discriminatorField: 'optionOne' } & OptionOne | { discriminatorField: 'optionTwo' } & OptionTwo" );
403+ }
404+
405+ private static File generate (
406+ Map <String , Object > properties
407+ ) throws IOException {
408+ return generate (
409+ properties ,
410+ "src/test/resources/3_0/typescript-fetch/example-for-file-naming-option.yaml"
411+ );
381412 }
382413
383- private static File generate (Map <String , Object > properties ) throws IOException {
414+ private static File generate (
415+ Map <String , Object > properties ,
416+ String inputSpec
417+ ) throws IOException {
384418 File output = Files .createTempDirectory ("test" ).toFile ();
385419 output .deleteOnExit ();
386420
387421 final CodegenConfigurator configurator = new CodegenConfigurator ()
388422 .setGeneratorName ("typescript-fetch" )
389- .setInputSpec ("src/test/resources/3_0/typescript-fetch/example-for-file-naming-option.yaml" )
423+ .setInputSpec (inputSpec )
390424 .setAdditionalProperties (properties )
391425 .setOutputDir (output .getAbsolutePath ().replace ("\\ " , "/" ));
392426
0 commit comments