Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed deepPartial modifier preserving refId #202

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading