Skip to content

Commit

Permalink
fixed deepPartial modifier preserving refId
Browse files Browse the repository at this point in the history
  • Loading branch information
AGalabov committed Jan 2, 2024
1 parent 01912d9 commit 64a31cc
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
109 changes: 109 additions & 0 deletions spec/modifiers/deepPartial.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { z } from 'zod';
import { expectSchema, generateDataForRoute } from '../lib/helpers';

describe('describe', () => {
it('generates a deepPartial object', () => {
const schema = z.object({
a: z.string(),
b: z.number(),
});

const { requestBody } = generateDataForRoute({
request: {
body: {
description: 'Test description',
required: true,
content: {
'application/json': {
schema: schema.deepPartial(),
},
},
},
},
});

expect(requestBody).toEqual({
description: 'Test description',
required: true,
content: {
'application/json': {
schema: {
type: 'object',
properties: {
a: { type: 'string' },
b: { type: 'number' },
},
},
},
},
});
});

it('generates a deepPartial object from a registered schema', () => {
const schema = z
.object({
a: z.string(),
b: z.number(),
})
.openapi('TestSchema');

const { documentSchemas, requestBody, responses } = generateDataForRoute({
request: {
body: {
description: 'Test description',
required: true,
content: {
'application/json': {
schema: schema.deepPartial(),
},
},
},
},
responses: {
200: {
description: 'Response description',
content: {
'application/json': { schema },
},
},
},
});

// The schema is registered
expect(documentSchemas).toEqual({
TestSchema: {
type: 'object',
properties: {
a: { type: 'string' },
b: { type: 'number' },
},
required: ['a', 'b'],
},
});

expect(responses[200]).toEqual({
description: 'Response description',
content: {
'application/json': {
schema: { $ref: '#/components/schemas/TestSchema' },
},
},
});

expect(requestBody).toEqual({
description: 'Test description',
required: true,
content: {
'application/json': {
schema: {
type: 'object',
properties: {
a: { type: 'string' },
b: { type: 'number' },
},
},
},
},
});
});
});
2 changes: 2 additions & 0 deletions src/zod-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ export function extendZodWithOpenApi(zod: typeof z) {
value._def.openapi = initialShape[key]?._def?.openapi;
});

result._def.openapi = undefined;

return result;
};

Expand Down

0 comments on commit 64a31cc

Please sign in to comment.