diff --git a/plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts b/plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts index 652cdc9..ef79cef 100644 --- a/plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts +++ b/plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts @@ -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, @@ -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", diff --git a/plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts b/plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts index c1a533b..835b2bb 100644 --- a/plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts +++ b/plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts @@ -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