Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/openapi3"
---

Fix invalid OpenAPI 3.0 output for a union of several types with `null`, such as `string | int32 | null`. `nullable: true` is now set on each variant of the `anyOf` or `oneOf` instead of on the wrapper schema, where OpenAPI 3.0 ignores it because the wrapper has no `type`.
8 changes: 3 additions & 5 deletions packages/openapi3/src/schema-emitter-3-0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ export class OpenAPI3SchemaEmitter extends OpenAPI3SchemaEmitterBase<OpenAPI3Sch
? this.applyConstraints(union, {})
: {};

if (mergeUnionWideConstraints && nullable) {
// OpenAPI 3.0 ignores `nullable` unless `type` is set on the same schema, so it
// goes on each variant instead of on the `anyOf`/`oneOf` wrapper.
if (nullable) {
additionalProps.nullable = true;
}

Expand Down Expand Up @@ -274,10 +276,6 @@ export class OpenAPI3SchemaEmitter extends OpenAPI3SchemaEmitterBase<OpenAPI3Sch
),
};

if (!isMerge && nullable) {
schema.nullable = true;
}

return this.applyConstraints(union, schema);
}

Expand Down
69 changes: 59 additions & 10 deletions packages/openapi3/test/union-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -815,16 +815,65 @@ worksFor(["3.0.0"], ({ diagnoseOpenApiFor, oapiForModel, openApiFor }) => {
`,
);
ok(res.isRef);
ok(res.schemas.Pet.properties.name.nullable);
deepStrictEqual(res.schemas.Pet.properties.name.anyOf, [
{
type: "integer",
format: "int32",
},
{
type: "string",
},
]);
deepStrictEqual(res.schemas.Pet.properties.name, {
anyOf: [
{
type: "integer",
format: "int32",
nullable: true,
},
{
type: "string",
nullable: true,
},
],
});
});

it("sets nullable on each variant when literals and scalars are mixed", async () => {
const res = await oapiForModel(
"Pet",
`
model Pet {
name: "a" | int32 | null;
};
`,
);
deepStrictEqual(res.schemas.Pet.properties.name, {
anyOf: [
{ type: "string", enum: ["a"], nullable: true },
{ type: "integer", format: "int32", nullable: true },
],
});
});

it("sets nullable on each variant of a named union and keeps its doc on the union", async () => {
const openApi = await openApiFor(`
/** A name or a number. */
union Test { string, int32, null }
`);
deepStrictEqual(openApi.components.schemas.Test, {
anyOf: [
{ type: "string", nullable: true },
{ type: "integer", format: "int32", nullable: true },
],
description: "A name or a number.",
});
});

it("sets nullable on each variant of a @oneOf union", async () => {
const openApi = await openApiFor(`
model Foo {
@oneOf
bar: string | int32 | null;
}
`);
deepStrictEqual(openApi.components.schemas.Foo.properties.bar, {
oneOf: [
{ type: "string", nullable: true },
{ type: "integer", format: "int32", nullable: true },
],
});
});

it("defines enums with a nullable variant", async () => {
Expand Down