Skip to content

Commit bcb77a8

Browse files
committed
refactor: convertToOpenAPI with native converter
1 parent cf98551 commit bcb77a8

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

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
}

0 commit comments

Comments
 (0)