6
6
import io .swagger .v3 .oas .models .media .MapSchema ;
7
7
import io .swagger .v3 .oas .models .media .Schema ;
8
8
import io .swagger .v3 .oas .models .media .StringSchema ;
9
+ import java .util .Collections ;
9
10
import org .apache .commons .lang3 .StringUtils ;
10
11
import org .openapitools .codegen .*;
11
12
import org .openapitools .codegen .config .CodegenConfigurator ;
@@ -333,7 +334,6 @@ public void testGeneratedFilenamesInCamelCase() throws IOException {
333
334
334
335
@ Test (description = "Verify names of files generated in camelCase and imports with additional model prefix" )
335
336
public void testGeneratedFilenamesInCamelCaseWithAdditionalModelPrefix () throws IOException {
336
-
337
337
Map <String , Object > properties = new HashMap <>();
338
338
properties .put ("fileNaming" , TypeScriptFetchClientCodegen .CAMEL_CASE );
339
339
properties .put (CodegenConstants .MODEL_NAME_PREFIX , "SomePrefix" );
@@ -349,19 +349,10 @@ public void testGeneratedFilenamesInCamelCaseWithAdditionalModelPrefix() throws
349
349
350
350
@ Test (description = "Issue #21295" )
351
351
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
+ );
365
356
366
357
Path exampleModelPath = Paths .get (outputPath + "/models/MyCustomSpeed.ts" );
367
358
//FromJSON
@@ -370,23 +361,66 @@ public void givenSchemaIsOneOfAndComposedSchemasArePrimitiveThenReturnStatements
370
361
TestUtils .assertFileContains (exampleModelPath , "json === 'fixed-value-a' || json === 'fixed-value-b' || json === 'fixed-value-c'" );
371
362
TestUtils .assertFileContains (exampleModelPath , "isNaN(new Date(json).getTime())" );
372
363
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' )" );
374
365
//ToJSON
375
366
TestUtils .assertFileContains (exampleModelPath , "typeof value === 'number'" );
376
367
TestUtils .assertFileContains (exampleModelPath , "typeof value === 'string'" );
377
368
TestUtils .assertFileContains (exampleModelPath , "value === 'fixed-value-a' || value === 'fixed-value-b' || value === 'fixed-value-c'" );
378
369
TestUtils .assertFileContains (exampleModelPath , "value instanceof Date" );
379
370
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
+ );
381
412
}
382
413
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 {
384
418
File output = Files .createTempDirectory ("test" ).toFile ();
385
419
output .deleteOnExit ();
386
420
387
421
final CodegenConfigurator configurator = new CodegenConfigurator ()
388
422
.setGeneratorName ("typescript-fetch" )
389
- .setInputSpec ("src/test/resources/3_0/typescript-fetch/example-for-file-naming-option.yaml" )
423
+ .setInputSpec (inputSpec )
390
424
.setAdditionalProperties (properties )
391
425
.setOutputDir (output .getAbsolutePath ().replace ("\\ " , "/" ));
392
426
0 commit comments