Skip to content

Commit 882165d

Browse files
committed
Add | null to oneOf union type when Null variant is filtered
When a oneOf includes type: 'null', the Null model reference is removed to prevent broken imports. The union type should include | null to preserve the nullable semantics from the original spec.
1 parent c2aaa11 commit 882165d

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOfInterfaces.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
* {{#lambda.indented_star_1}}{{{unescapedDescription}}}{{/lambda.indented_star_1}}
44
* @export
55
*/
6-
export type {{classname}} = {{#discriminator}}{{#mappedModels}}{ {{discriminator.propertyName}}: '{{mappingName}}' } & {{modelName}}{{^-last}} | {{/-last}}{{/mappedModels}}{{/discriminator}}{{^discriminator}}{{#oneOf}}{{{.}}}{{^-last}} | {{/-last}}{{/oneOf}}{{/discriminator}};
6+
export type {{classname}} = {{#discriminator}}{{#mappedModels}}{ {{discriminator.propertyName}}: '{{mappingName}}' } & {{modelName}}{{^-last}} | {{/-last}}{{/mappedModels}}{{/discriminator}}{{^discriminator}}{{#oneOf}}{{{.}}}{{^-last}} | {{/-last}}{{/oneOf}}{{/discriminator}}{{#isNullable}} | null{{/isNullable}};

modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,8 @@ public void testNullableOneOfDoesNotImportNullModel() throws IOException {
462462
// Should contain the valid model types
463463
TestUtils.assertFileContains(nullableResult, "FileLocation");
464464
TestUtils.assertFileContains(nullableResult, "DetailedLocation");
465-
// Union type should not include Null
466-
TestUtils.assertFileContains(nullableResult, "export type NullableResult = DetailedLocation | FileLocation");
465+
// Union type should not include Null but should be nullable
466+
TestUtils.assertFileContains(nullableResult, "export type NullableResult = DetailedLocation | FileLocation | null");
467467

468468
// No Null.ts model file should be generated
469469
TestUtils.assertFileNotExists(Paths.get(output + "/models/Null.ts"));

0 commit comments

Comments
 (0)