Skip to content

Commit ab4ec0d

Browse files
authored
Implementing unref() helper (#2581)
colinhacks/zod#4275 Extracting that into a helper.
1 parent c825246 commit ab4ec0d

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

express-zod-api/src/documentation-helpers.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ const intersect = (
167167
children: Array<JSONSchema.BaseSchema>,
168168
): JSONSchema.ObjectSchema => {
169169
const [left, right] = children
170-
.map(({ _ref, ...rest }) => (_ref ? { ...rest, ..._ref } : rest))
170+
.map(unref)
171171
.filter(
172172
(schema): schema is JSONSchema.ObjectSchema => schema.type === "object",
173173
)
@@ -204,7 +204,8 @@ const isSupportedType = (subject: string): subject is SchemaObjectType =>
204204
export const onDateIn: Overrider = ({ jsonSchema }, ctx) => {
205205
if (ctx.isResponse)
206206
throw new DocumentationError("Please use ez.dateOut() for output.", ctx);
207-
delete jsonSchema._ref; // undo default
207+
unref(jsonSchema);
208+
delete jsonSchema.anyOf; // undo default
208209
Object.assign(jsonSchema, {
209210
description: "YYYY-MM-DDTHH:mm:ss.sssZ",
210211
type: "string",
@@ -295,13 +296,14 @@ export const onPipeline: Overrider = ({ zodSchema, jsonSchema }, ctx) => {
295296
};
296297

297298
export const onRaw: Overrider = ({ jsonSchema }) => {
298-
if (!jsonSchema._ref) return;
299-
if (jsonSchema._ref.type !== "object") return;
300-
const objSchema = jsonSchema._ref as JSONSchema.ObjectSchema;
299+
unref(jsonSchema);
300+
if (jsonSchema.type !== "object") return;
301+
const objSchema = jsonSchema as JSONSchema.ObjectSchema;
301302
if (!objSchema.properties) return;
302303
if (!("raw" in objSchema.properties)) return;
303-
delete jsonSchema._ref; // undo
304304
Object.assign(jsonSchema, objSchema.properties.raw);
305+
delete jsonSchema.properties; // undo default
306+
delete jsonSchema.required;
305307
};
306308

307309
const enumerateExamples = (examples: unknown[]): ExamplesObject | undefined =>
@@ -476,6 +478,18 @@ const fixReferences = (
476478
return subject as SchemaObject; // @todo ideally, there should be a method to ensure that
477479
};
478480

481+
/** @link https://github.com/colinhacks/zod/issues/4275 */
482+
const unref = (
483+
subject: JSONSchema.BaseSchema,
484+
): Omit<JSONSchema.BaseSchema, "_ref"> => {
485+
while (subject._ref) {
486+
const copy = { ...subject._ref };
487+
delete subject._ref;
488+
Object.assign(subject, copy);
489+
}
490+
return subject;
491+
};
492+
479493
const depict = (
480494
subject: $ZodType,
481495
{ ctx, rules = overrides }: { ctx: OpenAPIContext; rules?: BrandHandling },

express-zod-api/tests/documentation-helpers.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,8 @@ describe("Documentation helpers", () => {
135135
describe("onRaw()", () => {
136136
test("should extract the raw property", () => {
137137
const jsonSchema: JSONSchema.BaseSchema = {
138-
_ref: {
139-
type: "object",
140-
properties: { raw: { format: "binary", type: "string" } },
141-
},
138+
type: "object",
139+
properties: { raw: { format: "binary", type: "string" } },
142140
};
143141
onRaw({ zodSchema: z.never(), jsonSchema }, requestCtx);
144142
expect(jsonSchema).toMatchSnapshot();
@@ -496,7 +494,7 @@ describe("Documentation helpers", () => {
496494

497495
describe("onDateIn", () => {
498496
test("should set type:string, pattern and format", () => {
499-
const jsonSchema: JSONSchema.BaseSchema = { _ref: { anyOf: [] } };
497+
const jsonSchema: JSONSchema.BaseSchema = { anyOf: [] };
500498
onDateIn({ zodSchema: z.never(), jsonSchema }, requestCtx);
501499
expect(jsonSchema).toMatchSnapshot();
502500
});

0 commit comments

Comments
 (0)