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
56 changes: 56 additions & 0 deletions plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ReferenceObject,
SchemaObject,
} from "openapi3-ts/oas30";
import { SchemaObject as SchemaObject31 } from "openapi3-ts/oas31";
import ts from "typescript";
import {
OpenAPIComponentType,
Expand Down Expand Up @@ -494,6 +495,61 @@ describe("schemaToTypeAliasDeclaration", () => {
`);
});

it("should generate a Blob", () => {
const schema: SchemaObject = {
type: "object",
required: ["document", "opportunity_id"],
properties: {
document: {
type: "string",
format: "binary",
description: "Proposal document file",
},
opportunity_id: {
type: "string",
},
},
};

expect(printSchema(schema)).toMatchInlineSnapshot(`
"export type Test = {
/**
* Proposal document file
*
* @format binary
*/
document: Blob;
opportunity_id: string;
};"
`);
});

it("should generate a Blob (oas3.1)", () => {
const schema: SchemaObject31 = {
type: "object",
required: ["document", "opportunity_id"],
properties: {
document: {
type: "string",
contentMediaType: "application/octet-stream",
description: "Proposal document file",
},
opportunity_id: {
type: "string",
},
},
};
expect(printSchema(schema as SchemaObject)).toMatchInlineSnapshot(`
"export type Test = {
/**
* Proposal document file
*/
document: Blob;
opportunity_id: string;
};"
`);
});

it("should generate an object with escaped keys", () => {
const schema: SchemaObject = {
type: "object",
Expand Down
9 changes: 8 additions & 1 deletion plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,16 @@ export const getType = (
schema.nullable
);
case "string": {
if (schema.format === "binary") {
if (
// OAS30
schema.format === "binary" ||
// OAS31
("contentMediaType" in schema &&
schema.contentMediaType === "application/octet-stream")
) {
return f.createTypeReferenceNode("Blob");
}

return withNullable(
f.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
schema.nullable
Expand Down
Loading