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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/openapi3"
---

Import OpenAPI 3.1/3.2 schemas with contentEncoding: base64 as bytes type with `@encode("base64", string)` decorator
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,12 @@ function getNumberType(schema: SupportedOpenAPISchema): string {

function getStringType(schema: SupportedOpenAPISchema): string {
const format = schema.format ?? "";

// Handle contentEncoding: base64 for OpenAPI 3.1+ (indicates binary data encoded as base64 string)
if ("contentEncoding" in schema && schema.contentEncoding === "base64") {
return "bytes";
}

let type = "string";
switch (format) {
case "binary":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,14 @@ function getStringSchemaDecorators(schema: OpenAPI3Schema | OpenAPISchema3_1) {
decorators.push({ name: "format", args: [schema.format] });
}

// Handle contentEncoding: base64 for OpenAPI 3.1+ (indicates binary data encoded as base64 string)
if ("contentEncoding" in schema && schema.contentEncoding === "base64") {
decorators.push({
name: "encode",
args: [createTSValue(`"base64"`), createTSValue("string")],
});
}

return decorators;
}

Expand Down
18 changes: 18 additions & 0 deletions packages/openapi3/test/tsp-openapi3/data-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ describe("converts top-level schemas", () => {
]);
});

it("handles contentEncoding base64 as bytes with @encode decorator", async () => {
const serviceNamespace = await tspForOpenAPI3({
schemas: {
Base64Encoded: {
type: "string",
contentEncoding: "base64",
} as any,
},
});

const scalars = serviceNamespace.scalars;
/* @encode("base64", string) scalar Base64Encoded extends bytes; */
expect(scalars.get("Base64Encoded")?.baseScalar?.name).toBe("bytes");
expectDecorators(scalars.get("Base64Encoded")!.decorators, [
{ name: "encode", args: ["base64", { kind: "Scalar", name: "string" }] },
]);
});

it("handles arrays", async () => {
const serviceNamespace = await tspForOpenAPI3({
schemas: {
Expand Down
6 changes: 4 additions & 2 deletions packages/openapi3/test/tsp-openapi3/generate-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { formatTypeSpec } from "@typespec/compiler";
import { strictEqual } from "node:assert";
import { beforeAll, describe, it } from "vitest";
import { Context, createContext } from "../../src/cli/actions/convert/utils/context.js";
import { OpenAPI3Document, OpenAPI3Schema, Refable } from "../../src/types.js";
import { OpenAPI3Document, OpenAPI3Schema, OpenAPISchema3_1, Refable } from "../../src/types.js";

interface TestScenario {
schema: Refable<OpenAPI3Schema>;
schema: Refable<OpenAPI3Schema | OpenAPISchema3_1>;
expected: string;
}

Expand Down Expand Up @@ -62,6 +62,8 @@ const testScenarios: TestScenario[] = [
},
{ schema: { type: "string", format: "binary" }, expected: "bytes" },
{ schema: { type: "string", format: "byte" }, expected: "bytes" },
// OpenAPI 3.1+ contentEncoding: base64 should be bytes
{ schema: { type: "string", contentEncoding: "base64" }, expected: "bytes" },
{ schema: { type: "string", format: "date" }, expected: "plainDate" },
{ schema: { type: "string", format: "date-time" }, expected: "utcDateTime" },
{ schema: { type: "string", format: "duration" }, expected: "duration" },
Expand Down
Loading