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

Hotfix: Multipart bytes[] part #2754

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,10 @@
{
"changes": [
{
"packageName": "@typespec/openapi3",
"comment": "Fix: Multipart part of type `bytes[]` is now treated as multiple binary part",
"type": "none"
}
],
"packageName": "@typespec/openapi3"
}
26 changes: 18 additions & 8 deletions packages/openapi3/src/schema-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,27 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter<
return props;
}

#isBytesKeptRaw(type: Type) {
const program = this.emitter.getProgram();
return (
type.kind === "Scalar" && type.name === "bytes" && getEncode(program, type) === undefined
);
}

modelPropertyLiteral(prop: ModelProperty): EmitterOutput<object> {
const program = this.emitter.getProgram();
const isMultipart = this.#getContentType().startsWith("multipart/");
if (
isMultipart &&
prop.type.kind === "Scalar" &&
prop.type.name === "bytes" &&
getEncode(program, prop.type) === undefined &&
getEncode(program, prop) === undefined
) {
return { type: "string", format: "binary" };
if (isMultipart) {
if (this.#isBytesKeptRaw(prop.type) && getEncode(program, prop) === undefined) {
return { type: "string", format: "binary" };
}
if (
prop.type.kind === "Model" &&
isArrayModelType(program, prop.type) &&
this.#isBytesKeptRaw(prop.type.indexer.value)
) {
return { type: "array", items: { type: "string", format: "binary" } };
}
}

const refSchema = this.emitter.emitTypeReference(prop.type, {
Expand Down
21 changes: 21 additions & 0 deletions packages/openapi3/test/multipart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ describe("typespec-autorest: multipart", () => {
});
});

it("part of type `bytes[]` produce `type: array, items: {type: string, format: binary}`", async () => {
const res = await openApiFor(
`
op upload(@header contentType: "multipart/form-data", profileImage: bytes[]): void;
`
);
const op = res.paths["/"].post;
deepStrictEqual(op.requestBody.content["multipart/form-data"], {
schema: {
type: "object",
properties: {
profileImage: {
type: "array",
items: { type: "string", format: "binary" },
},
},
required: ["profileImage"],
},
});
});

it("part of type `string` produce `type: string`", async () => {
const res = await openApiFor(
`
Expand Down