Skip to content

Commit

Permalink
Use allof when building nullable enum (#4727)
Browse files Browse the repository at this point in the history
fix [#4398](#4398)
  • Loading branch information
timotheeguerin authored Oct 31, 2024
1 parent 012ec81 commit 151be7f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/openapi3"
---

Nullable enum use `allOf` instead of `oneOf`
2 changes: 1 addition & 1 deletion packages/openapi3/src/schema-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter<
...additionalProps,
});
} else {
return new ObjectBuilder({ oneOf: B.array([schema]), ...additionalProps });
return new ObjectBuilder({ allOf: B.array([schema]), ...additionalProps });
}
} else {
const merged = new ObjectBuilder<OpenAPI3Schema>(schema);
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi3/test/nullable-properties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("openapi3: nullable properties", () => {
}
`,
);
deepStrictEqual(res.schemas.X.properties.prop.oneOf, [
deepStrictEqual(res.schemas.X.properties.prop.allOf, [
{
$ref: "#/components/schemas/A",
},
Expand Down
19 changes: 18 additions & 1 deletion packages/openapi3/test/union-schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expectDiagnostics } from "@typespec/compiler/testing";
import { deepStrictEqual, ok, strictEqual } from "assert";
import { describe, it } from "vitest";
import { describe, expect, it } from "vitest";
import { diagnoseOpenApiFor, oapiForModel, openApiFor } from "./test-host.js";

describe("openapi3: union type", () => {
Expand Down Expand Up @@ -577,4 +577,21 @@ describe("openapi3: union type", () => {
},
});
});

describe("null and another single variant produce allOf", () => {
it.each([
["model", "model Other {}"],
["enum", "enum Other {a, b}"],
])("%s variant", async (_, code) => {
const openApi = await openApiFor(`
union Test { Other, null }
${code}
`);

expect(openApi.components.schemas.Test).toMatchObject({
allOf: [{ $ref: "#/components/schemas/Other" }],
nullable: true,
});
});
});
});

0 comments on commit 151be7f

Please sign in to comment.