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 not marking part of bytes or something else as format: binary #3013

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,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/openapi3"
---

Fix: OpenAPI3 not marking part of bytes or something else as `format: binary`
13 changes: 11 additions & 2 deletions packages/openapi3/src/schema-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter<
}

const refSchema = this.emitter.emitTypeReference(prop.type, {
referenceContext: isMultipart ? { contentType: "application/json" } : {},
referenceContext:
prop.type.kind !== "Union" && isMultipart ? { contentType: "application/json" } : {},
});

if (refSchema.kind !== "code") {
Expand Down Expand Up @@ -471,13 +472,19 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter<
const schemaMembers: { schema: any; type: Type | null }[] = [];
let nullable = false;
const discriminator = getDiscriminator(program, union);
const isMultipart = this.#getContentType().startsWith("multipart/");

for (const variant of variants) {
if (isNullType(variant.type)) {
nullable = true;
continue;
}

if (isMultipart && this.#isBytesKeptRaw(variant.type)) {
schemaMembers.push({ schema: { type: "string", format: "binary" }, type: variant.type });
continue;
}

if (isLiteralType(variant.type)) {
if (!literalVariantEnumByType[variant.type.kind]) {
const enumSchema = this.emitter.emitTypeReference(variant.type);
Expand All @@ -491,7 +498,9 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter<
literalVariantEnumByType[variant.type.kind].enum.push(variant.type.value);
}
} else {
const enumSchema = this.emitter.emitTypeReference(variant.type);
const enumSchema = this.emitter.emitTypeReference(variant.type, {
referenceContext: isMultipart ? { contentType: "application/json" } : {},
});
compilerAssert(enumSchema.kind === "code", "Unexpected enum schema. Should be kind: code");
schemaMembers.push({ schema: enumSchema.value, type: variant.type });
}
Expand Down
32 changes: 32 additions & 0 deletions packages/openapi3/test/multipart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,38 @@ describe("typespec-autorest: multipart", () => {
});
});

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

it("part of type `bytes[]` produce `type: array, items: {type: string, format: binary}`", async () => {
const res = await openApiFor(
`
Expand Down
Loading