Skip to content

Commit 6864b96

Browse files
committed
refactor: convertToOpenAPI with native converter
Fixes #20
1 parent 1aef586 commit 6864b96

File tree

5 files changed

+15
-25
lines changed

5 files changed

+15
-25
lines changed

src/core/body.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe("resolveRequestBody", () => {
3434

3535
it("should handle formData content type", () => {
3636
const schema = z.object({
37-
file: z.instanceof(File),
37+
file: z.file(),
3838
});
3939

4040
const result = resolveRequestBody(schema, true);

src/core/definer.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ describe("defineRoute", () => {
121121
description: "Uploads a file to the storage service",
122122
tags: ["Upload"],
123123
requestBody: z.object({
124-
file: z.instanceof(File).describe("File object to be uploaded"),
124+
file: z.file().describe("File object to be uploaded"),
125125
}),
126126
hasFormData: true,
127127
action: mockAction,

src/core/zod-to-openapi.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ describe("convertToOpenAPI", () => {
2020
},
2121
required: ["name", "age"],
2222
additionalProperties: false,
23+
// @ts-expect-error: @omer-x/openapi-types doesn't have this
24+
$schema: "https://json-schema.org/draft/2020-12/schema",
2325
};
2426

2527
expect(openAPISchema).toEqual(expectedSchema);
@@ -33,6 +35,8 @@ describe("convertToOpenAPI", () => {
3335
const expectedSchema: SchemaObject = {
3436
type: "array",
3537
items: { type: "string" },
38+
// @ts-expect-error: @omer-x/openapi-types doesn't have this
39+
$schema: "https://json-schema.org/draft/2020-12/schema",
3640
};
3741

3842
expect(openAPISchema).toEqual(expectedSchema);
@@ -63,14 +67,16 @@ describe("convertToOpenAPI", () => {
6367
},
6468
required: ["user"],
6569
additionalProperties: false,
70+
// @ts-expect-error: @omer-x/openapi-types doesn't have this
71+
$schema: "https://json-schema.org/draft/2020-12/schema",
6672
};
6773

6874
expect(openAPISchema).toEqual(expectedSchema);
6975
});
7076

7177
it("should handle file type correctly in an object", () => {
7278
const zodSchema = z.object({
73-
file: z.instanceof(File),
79+
file: z.file(),
7480
});
7581

7682
const openAPISchema = convertToOpenAPI(zodSchema, false);
@@ -81,10 +87,13 @@ describe("convertToOpenAPI", () => {
8187
file: {
8288
type: "string",
8389
format: "binary",
90+
contentEncoding: "binary" as unknown as undefined,
8491
},
8592
},
8693
required: ["file"],
8794
additionalProperties: false,
95+
// @ts-expect-error: @omer-x/openapi-types doesn't have this
96+
$schema: "https://json-schema.org/draft/2020-12/schema",
8897
};
8998

9099
expect(openAPISchema).toEqual(expectedSchema);

src/core/zod-to-openapi.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
1-
import { zodToJsonSchema } from "zod-to-json-schema";
2-
import { isFile } from "~/utils/zod-schema";
1+
import { type ZodType, z } from "zod";
32
import type { SchemaObject } from "@omer-x/openapi-types/schema";
4-
import type { ZodObject, ZodType } from "zod";
53

64
export default function convertToOpenAPI(schema: ZodType<unknown>, isArray: boolean) {
7-
const result = zodToJsonSchema(isArray ? schema.array() : schema, {
8-
target: "openApi3",
9-
$refStrategy: "none",
10-
}) as SchemaObject;
11-
if (result.type === "object" && result.properties) {
12-
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
13-
for (const [propName, prop] of Object.entries<ZodType>((schema as ZodObject<{}>).shape)) {
14-
if (isFile(prop)) {
15-
result.properties[propName] = {
16-
type: "string",
17-
format: "binary",
18-
description: prop.description,
19-
// contentEncoding: "base64", // swagger-ui-react doesn't support this
20-
};
21-
}
22-
}
23-
}
24-
return result;
5+
return z.toJSONSchema(isArray ? schema.array() : schema) as SchemaObject;
256
}

src/utils/zod-schema.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { isFile } from "./zod-schema";
44

55
describe("isFile", () => {
66
it("should return true for a valid file schema", () => {
7-
const fileSchema = z.instanceof(File);
7+
const fileSchema = z.file();
88
const result = isFile(fileSchema);
99
expect(result).toBe(true);
1010
});

0 commit comments

Comments
 (0)