|
17 | 17 | import org.openapitools.codegen.typescript.TypeScriptGroups; |
18 | 18 | import org.openapitools.codegen.utils.ModelUtils; |
19 | 19 | import org.testng.Assert; |
| 20 | +import org.testng.annotations.DataProvider; |
20 | 21 | import org.testng.annotations.Test; |
21 | 22 |
|
22 | 23 | import java.io.File; |
@@ -752,6 +753,38 @@ public void testOneOfModelsDoNotImportPrimitiveTypes() throws IOException { |
752 | 753 | TestUtils.assertFileContains(testDiscriminatorResponse, "export type TestDiscriminatorResponse = { discriminatorField: 'optionOne' } & OptionOne | { discriminatorField: 'optionTwo' } & OptionTwo"); |
753 | 754 | } |
754 | 755 |
|
| 756 | + @DataProvider |
| 757 | + public Object[][] nullableOneOfRuntimeCheckModes() { |
| 758 | + return new Object[][] {{false}, {true}}; |
| 759 | + } |
| 760 | + |
| 761 | + @Test(dataProvider = "nullableOneOfRuntimeCheckModes") |
| 762 | + public void testNullablePrimitiveOneOfTypes(boolean withoutRuntimeChecks) throws IOException { |
| 763 | + Map<String, Object> properties = new HashMap<>(); |
| 764 | + properties.put(TypeScriptFetchClientCodegen.WITHOUT_RUNTIME_CHECKS, withoutRuntimeChecks); |
| 765 | + File output = generate(properties, |
| 766 | + "src/test/resources/3_0/typescript-fetch/oneof-nullable-primitives.yaml"); |
| 767 | + |
| 768 | + for (String model : List.of("InlineNullableValue", "ReferencedNullableValue")) { |
| 769 | + Path file = Paths.get(output + (withoutRuntimeChecks ? "/models/index.ts" : "/models/" + model + ".ts")); |
| 770 | + TestUtils.assertFileContains(file, "export type " + model + " = null | number | string;"); |
| 771 | + } |
| 772 | + |
| 773 | + // Null must match exactly one branch, rather than none or both branches. |
| 774 | + for (String model : List.of("NonNullableValue", "MultipleNullableValue")) { |
| 775 | + Path file = Paths.get(output + (withoutRuntimeChecks ? "/models/index.ts" : "/models/" + model + ".ts")); |
| 776 | + TestUtils.assertFileContains(file, "export type " + model + " = number | string;"); |
| 777 | + } |
| 778 | + |
| 779 | + Path arrayItems = Paths.get(output + (withoutRuntimeChecks |
| 780 | + ? "/models/index.ts" : "/models/NullableArrayItems.ts")); |
| 781 | + TestUtils.assertFileContains(arrayItems, "export type NullableArrayItems = Array<string | null> | number;"); |
| 782 | + |
| 783 | + Path values = Paths.get(output + (withoutRuntimeChecks ? "/models/index.ts" : "/models/Values.ts")); |
| 784 | + TestUtils.assertFileContains(values, "array: Array<InlineNullableValue>;", |
| 785 | + "map: { [key: string]: InlineNullableValue; };"); |
| 786 | + } |
| 787 | + |
755 | 788 | /** |
756 | 789 | * Issue #21587 |
757 | 790 | * When using oneOf, the Typescript Fetch generator should import modelled types except for |
|
0 commit comments