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

Fix OpenAPI3 union names when declared within a namespace #4123

Merged
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"
---

Fix OpenAPI3 union names when declared within a namespace
3 changes: 2 additions & 1 deletion packages/openapi3/src/schema-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter<

unionDeclaration(union: Union, name: string): EmitterOutput<object> {
const schema = this.#unionSchema(union);
return this.#createDeclaration(union, name, schema);
const baseName = getOpenAPITypeName(this.emitter.getProgram(), union, this.#typeNameOptions());
return this.#createDeclaration(union, baseName, schema);
}

#unionSchema(union: Union): ObjectBuilder<OpenAPI3Schema> {
Expand Down
57 changes: 57 additions & 0 deletions packages/openapi3/test/union-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,63 @@ describe("openapi3: union type", () => {
});
});

it("handles unions defined in a namespace", async () => {
const res = await openApiFor(`
namespace Foo {
model A {
foo: string;
}
}

namespace Bar {
model A {
bar: string;
}
}

namespace Baz {
union A {
foo: Foo.A,
bar: Bar.A
}
}

@get
op getFoo(data: Baz.A): {};
`);

deepStrictEqual(res.components.schemas["Foo.A"], {
properties: {
foo: {
type: "string",
},
},
required: ["foo"],
type: "object",
});

deepStrictEqual(res.components.schemas["Bar.A"], {
properties: {
bar: {
type: "string",
},
},
required: ["bar"],
type: "object",
});

deepStrictEqual(res.components.schemas["Baz.A"], {
anyOf: [
{
$ref: "#/components/schemas/Foo.A",
},
{
$ref: "#/components/schemas/Bar.A",
},
],
});
});

it("throws diagnostics for empty enum definitions", async () => {
const diagnostics = await diagnoseOpenApiFor(`union Pet {}`);

Expand Down
Loading