Skip to content
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 OpenAPI emitter failing with "Duplicate type name" error when using a named union with a `bytes` variant in a multipart body (e.g. `HttpPart<MyUnion>` where `MyUnion` includes `bytes`).
10 changes: 10 additions & 0 deletions packages/openapi3/src/visibility-usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ function addUsagesInOperation(
navigateReferencedTypes(httpOperation.parameters.body.type, visibility, (type, vis) =>
trackUsage(metadataInfo, usages, type, vis),
);
// For multipart bodies, also navigate part types directly. HttpPart<T> wrappers are
// empty models with no properties, so navigateReferencedTypes won't reach T through
// normal property traversal, causing T to be incorrectly treated as unreachable.
if (httpOperation.parameters.body.bodyKind === "multipart") {
for (const part of httpOperation.parameters.body.parts) {
navigateReferencedTypes(part.body.type, visibility, (type, vis) =>
trackUsage(metadataInfo, usages, type, vis),
);
}
}
}
for (const param of httpOperation.parameters.parameters) {
navigateReferencedTypes(param.param, visibility, (type, vis) =>
Expand Down
27 changes: 27 additions & 0 deletions packages/openapi3/test/multipart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,33 @@ worksFor(supportedVersions, ({ openApiFor }) => {
description: "My doc",
});
});

it("named union with bytes variant does not cause 'Duplicate type name' error", async () => {
const res = await openApiFor(
`
union BinaryOrJson {
bytes,
{ file_id: string },
}
op upload(@header contentType: "multipart/form-data", @multipartBody body: { attachment: HttpPart<BinaryOrJson> }): void;
`,
);
const op = res.paths["/"].post;
// The union should be referenced as a $ref (not inlined), and should be available in components
expect(op.requestBody.content["multipart/form-data"].schema.properties.attachment.$ref).toEqual(
"#/components/schemas/BinaryOrJson",
);
const schema = res.components.schemas.BinaryOrJson;
expect(schema).toBeDefined();
// The schema should use anyOf with 2 variants
expect(schema.anyOf).toHaveLength(2);
// The object variant ({file_id: string}) should appear in the schema regardless of version
expect(schema.anyOf).toContainEqual({
type: "object",
properties: { file_id: { type: "string" } },
required: ["file_id"],
});
});
});

worksFor(["3.0.0"], ({ openApiFor }) => {
Expand Down
Loading