Skip to content

Commit

Permalink
Merge pull request #144 from asteasolutions/fix/deep-partial-loosing-…
Browse files Browse the repository at this point in the history
…metadata

#132 preserve schema metadata for deepPartial
  • Loading branch information
AGalabov committed May 31, 2023
2 parents 5a17fec + fa52b00 commit 769bdcf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
31 changes: 31 additions & 0 deletions spec/routes/parameters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,37 @@ describe('parameters', () => {
]);
});

it('generates a deepPartial object query parameter for route', () => {
const { parameters } = generateDataForRoute({
request: {
query: z
.object({
filter: z
.object({ test: z.string() })
.openapi({ param: { style: 'deepObject' } }),
})
.deepPartial(),
},
});

expect(parameters).toEqual([
{
in: 'query',
name: 'filter',
required: false,
style: 'deepObject',
schema: {
type: 'object',
properties: {
test: {
type: 'string',
},
},
},
},
]);
});

it('generates a reference query parameter for route', () => {
const TestQuery = registerParameter(
'TestQuery',
Expand Down
15 changes: 15 additions & 0 deletions src/zod-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ export function extendZodWithOpenApi(zod: typeof z) {
return result;
};

const zodDeepPartial = zod.ZodObject.prototype.deepPartial;
zod.ZodObject.prototype.deepPartial = function (this: any) {
const initialShape = this._def.shape();

const result = zodDeepPartial.apply(this);

const resultShape = result._def.shape();

Object.entries(resultShape).forEach(([key, value]) => {
value._def.openapi = initialShape[key]?._def?.openapi;
});

return result;
};

const zodOptional = zod.ZodType.prototype.optional as any;
(zod.ZodType.prototype.optional as any) = function (
this: any,
Expand Down

0 comments on commit 769bdcf

Please sign in to comment.