Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openapi3 - fix bug where union-level docs were applied to members #3912

Merged
merged 1 commit into from
Jul 19, 2024
Merged
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"
---

Fixes bug where union documentation was being applied to each union member in emitted output.
14 changes: 9 additions & 5 deletions packages/openapi3/src/schema-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,14 +523,16 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter<

const wrapWithObjectBuilder = (
schemaMember: { schema: any; type: Type | null },
{ applyNullable }: { applyNullable: boolean }
{ mergeUnionWideConstraints }: { mergeUnionWideConstraints: boolean }
): ObjectBuilder<OpenAPI3Schema> => {
// we can just return the single schema member after applying nullable
const schema = schemaMember.schema;
const type = schemaMember.type;
const additionalProps: Partial<OpenAPI3Schema> = this.#applyConstraints(union, {});
const additionalProps: Partial<OpenAPI3Schema> = mergeUnionWideConstraints
? this.#applyConstraints(union, {})
: {};

if (applyNullable && nullable) {
if (mergeUnionWideConstraints && nullable) {
additionalProps.nullable = true;
}

Expand Down Expand Up @@ -573,11 +575,13 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter<
}

if (schemaMembers.length === 1) {
return wrapWithObjectBuilder(schemaMembers[0], { applyNullable: true });
return wrapWithObjectBuilder(schemaMembers[0], { mergeUnionWideConstraints: true });
}

const schema: OpenAPI3Schema = {
[ofType]: schemaMembers.map((m) => wrapWithObjectBuilder(m, { applyNullable: false })),
[ofType]: schemaMembers.map((m) =>
wrapWithObjectBuilder(m, { mergeUnionWideConstraints: false })
),
};

if (nullable) {
Expand Down
20 changes: 20 additions & 0 deletions packages/openapi3/test/union-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,4 +441,24 @@ describe("openapi3: union type", () => {
strictEqual(res.schemas.Foo.title, "FooUnion");
strictEqual(res.schemas.Bar.title, "BarUnion");
});

it("does not duplicate top-level description on union members", async () => {
const res = await oapiForModel(
"Foo",
`
@doc("The possible types of things")
union Foo {
string,

bar: "bar",
buzz: "buzz",
}`
);

strictEqual(res.schemas.Foo.description, "The possible types of things");
strictEqual(res.schemas.Foo.anyOf.length, 2);
for (const variant of res.schemas.Foo.anyOf) {
strictEqual(variant.description, undefined);
}
});
});
Loading