Skip to content

Commit

Permalink
Treat bytes array as many binary parts
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin committed Dec 13, 2023
1 parent 8be7c23 commit cdb55a3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
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

0 comments on commit cdb55a3

Please sign in to comment.