Skip to content

Commit

Permalink
openapi3 - fix bug where union-level docs were applied to members (#3912
Browse files Browse the repository at this point in the history
)

Fixes regression introduced in #3908 where docs set at the union level
were being applied to each union variant.

Co-authored-by: Christopher Radek <Christopher.Radek@microsoft.com>
  • Loading branch information
chrisradek and Christopher Radek authored Jul 19, 2024
1 parent c0f58d3 commit d8a4760
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/inv-openapi3-circ-ref-2024-6-19-13-33-42.md
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);
}
});
});

0 comments on commit d8a4760

Please sign in to comment.