Skip to content

Commit 5cf1cef

Browse files
committed
fix: missing schema descriptions
1 parent f23045b commit 5cf1cef

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/core/zod-to-openapi.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
import { type ZodObject, type ZodType, z } from "zod";
22
import type { SchemaObject } from "@omer-x/json-schema-types";
33

4+
function alterSchema(schema: ZodType<unknown>, newShape: ZodType<unknown>): ZodType<unknown> {
5+
if (schema.description) {
6+
newShape = newShape.describe(schema.description);
7+
}
8+
return newShape;
9+
}
10+
411
function fixSchema(schema: ZodType<unknown>): ZodType<unknown> {
512
if ("unwrap" in schema && typeof schema.unwrap === "function") {
613
switch (schema._zod.def.type) {
7-
case "nullable": return fixSchema(schema.unwrap()).nullable();
8-
case "optional": return fixSchema(schema.unwrap()).optional();
9-
case "readonly": return fixSchema(schema.unwrap()).readonly();
10-
case "array": return fixSchema(schema.unwrap()).array();
14+
case "nullable": return alterSchema(schema, fixSchema(schema.unwrap()).nullable());
15+
case "optional": return alterSchema(schema, fixSchema(schema.unwrap()).optional());
16+
case "readonly": return alterSchema(schema, fixSchema(schema.unwrap()).readonly());
17+
case "array": return alterSchema(schema, fixSchema(schema.unwrap()).array());
1118
default: throw new Error(`${schema._zod.def.type} type is not covered in fixSchema (@omer-x/next-openapi-json-generator")`);
1219
}
1320
}
1421
if (schema._zod.def.type === "date") {
15-
return z.iso.datetime();
22+
return alterSchema(schema, z.iso.datetime());
1623
}
1724
if (schema._zod.def.type === "object") {
1825
const { shape } = schema as ZodObject;
1926
const entries = Object.entries(shape);
2027
const alteredEntries = entries.map(([propName, prop]) => [propName, fixSchema(prop)]);
2128
const newShape = Object.fromEntries(alteredEntries);
22-
return z.object(newShape);
29+
return alterSchema(schema, z.object(newShape));
2330
}
2431
return schema;
2532
}

0 commit comments

Comments
 (0)