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

[zod-openapi] Merge additional OpenAPI definitions on ZodPipeline #189

Merged
merged 1 commit into from
Mar 19, 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
42 changes: 42 additions & 0 deletions packages/zod-openapi/src/lib/zod-openapi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,48 @@ describe('zodOpenapi', () => {
} satisfies SchemaObject);
});

it('should work with ZodPipeline and additional extendApi', () => {
expect(
generateSchema(
extendApi(
z
.string()
.regex(/^\d+$/)
.transform(Number)
.pipe(z.number().min(0).max(10)),
{
description: 'Foo description',
}
)
)
).toEqual({
type: 'string',
pattern: '^\\d+$',
description: 'Foo description',
} satisfies SchemaObject);

expect(
generateSchema(
extendApi(
z
.string()
.regex(/^\d+$/)
.transform(Number)
.pipe(z.number().min(0).max(10)),
{
description: 'Foo description',
}
),
true
)
).toEqual({
type: 'number',
minimum: 0,
maximum: 10,
description: 'Foo description',
} satisfies SchemaObject);
});


it('should work with ZodTransform and correctly set nullable and optional', () => {
type Type = string;
Expand Down
9 changes: 5 additions & 4 deletions packages/zod-openapi/src/lib/zod-openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,14 @@ function catchAllParser({
}

function parsePipeline({
schemas,
zodRef,
useOutput,
}: ParsingArgs<z.ZodPipeline<never, never>>): SchemaObject {
if (useOutput) {
return generateSchema(zodRef._def.out, useOutput);
}
return generateSchema(zodRef._def.in, useOutput);
return merge(
generateSchema(useOutput ? zodRef._def.out : zodRef._def.in, useOutput),
...schemas,
);
}

function parseReadonly({
Expand Down
Loading