Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -813,12 +813,21 @@ private ExtendedCodegenModel processCodeGenModel(ExtendedCodegenModel cm) {
.map(CodegenComposedSchemas::getOneOf)
.orElse(Collections.emptyList());

// Remove "Null" from oneOf variants. In OpenAPI 3.1, oneOf can include
// `type: 'null'` to represent nullable types. The codegen maps this to a
// "Null" model name, but no Null model file is generated, causing import
// errors. Instead, mark the model as nullable and filter out the Null entry.
if (cm.oneOf != null && !cm.oneOf.isEmpty() && cm.oneOf.remove("Null")) {
cm.isNullable = true;
}

// create a set of any non-primitive, non-array types used in the oneOf schemas which will
// need to be imported.
cm.oneOfModels = oneOfsList.stream()
.filter(cp -> !cp.getIsPrimitiveType() && !cp.getIsArray())
.map(CodegenProperty::getBaseType)
.filter(Objects::nonNull)
.filter(baseType -> !"Null".equals(baseType))
.collect(Collectors.toCollection(TreeSet::new));

// create a set of any complex, inner types used by arrays in the oneOf schema (e.g. if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,31 @@ public void testOneOfModelsImportNonPrimitiveTypes() throws IOException {
TestUtils.assertFileContains(testResponse, "import type { OptionThree } from './OptionThree'");
}

@Test(description = "Verify nullable oneOf does not generate Null model references")
public void testNullableOneOfDoesNotImportNullModel() throws IOException {
File output = generate(
Collections.emptyMap(),
"src/test/resources/3_0/typescript-fetch/nullable-oneOf.yaml"
);

Path nullableResult = Paths.get(output + "/models/NullableResult.ts");
TestUtils.assertFileExists(nullableResult);

// Should not import or reference a non-existent "Null" model
TestUtils.assertFileNotContains(nullableResult, "import type { Null }");
TestUtils.assertFileNotContains(nullableResult, "NullFromJSON");
TestUtils.assertFileNotContains(nullableResult, "NullToJSON");
TestUtils.assertFileNotContains(nullableResult, "instanceOfNull");
// Should contain the valid model types
TestUtils.assertFileContains(nullableResult, "FileLocation");
TestUtils.assertFileContains(nullableResult, "DetailedLocation");
// Union type should not include Null
TestUtils.assertFileContains(nullableResult, "export type NullableResult = DetailedLocation | FileLocation");

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

@Test(description = "Verify validationAttributes works with withoutRuntimeChecks=true")
public void testValidationAttributesWithWithoutRuntimeChecks() throws IOException {
Map<String, Object> properties = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
openapi: 3.1.0
info:
version: 1.0.0
title: testing nullable oneOf (Null type filtering)
paths:
/locations/{locationId}:
get:
operationId: getLocation
parameters:
- name: locationId
in: path
required: true
schema:
type: string
responses:
200:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/NullableResult'
components:
schemas:
NullableResult:
oneOf:
- $ref: '#/components/schemas/FileLocation'
- $ref: '#/components/schemas/DetailedLocation'
- type: 'null'
FileLocation:
type: object
properties:
path:
type: string
required:
- path
DetailedLocation:
type: object
properties:
path:
type: string
size:
type: integer
required:
- path
- size
Loading