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] a quick take on removing "nullable" and solving nullability of z.custom() #179

Merged
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
47 changes: 42 additions & 5 deletions packages/zod-openapi/src/lib/zod-openapi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
type: 'object',
properties: {
aUndefined: {},
aNull: { type: 'string', format: 'null', nullable: true },
aNull: { type: 'null' },
aVoid: {},
},
required: ['aNull'],
Expand Down Expand Up @@ -239,7 +239,7 @@
aArrayNonempty: {
type: 'array',
minItems: 1,
items: { type: 'string', format: 'null', nullable: true },
items: { type: 'null' },
},
aArrayMinAndMax: {
type: 'array',
Expand Down Expand Up @@ -624,9 +624,9 @@
literals: {
type: 'object',
properties: {
wordOne: { nullable: true, type: 'string', enum: ['One'] },
wordOne: { type: ['string', 'null'], enum: ['One'] },
numberTwo: { type: 'number', enum: [2] },
isThisTheEnd: { nullable: true, type: 'boolean', enum: [false] },
isThisTheEnd: { type: ['boolean', 'null'], enum: [false] },
},
required: ['wordOne'],
},
Expand Down Expand Up @@ -730,7 +730,7 @@
oneOf: [{ type: 'string' }, { type: 'string' }],
description: 'Odd pattern here',
},
aNullish: { nullable: true, type: 'string' },
aNullish: { type: ['string', 'null'] },
stringLengthOutput: { type: 'number' },
favourites: {
type: 'object',
Expand Down Expand Up @@ -961,6 +961,42 @@
maximum: 10,
} satisfies SchemaObject);
});


it('should work with ZodTransform and correctly set nullable and optional', () => {
type Type = string;
const schema = z.object({
item: extendApi(
z.custom<Type>((data) => true),

Check warning on line 970 in packages/zod-openapi/src/lib/zod-openapi.spec.ts

View workflow job for this annotation

GitHub Actions / build (18)

'data' is defined but never used
generateSchema(z.string().nullable())
),
});
expect(generateSchema(schema)).toEqual({
properties: {
item: {
type: ['string', 'null'],
},
},
type: 'object',
});
const schema2 = z.object({
item: extendApi(
z.custom<Type>((data) => !!data),
generateSchema(z.string())
),
});
expect(generateSchema(schema2)).toEqual({
properties: {
item: {
type: 'string',
},
},
required: ['item'],
type: 'object',
});

});

test('should work with ZodReadonly', () => {
expect(generateSchema(z.object({ field: z.string() })))
.toMatchInlineSnapshot(`
Expand Down Expand Up @@ -991,5 +1027,6 @@
"type": "object",
}
`);

});
});
31 changes: 19 additions & 12 deletions packages/zod-openapi/src/lib/zod-openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,29 +299,38 @@ function parseDate({ zodRef, schemas }: ParsingArgs<z.ZodDate>): SchemaObject {
function parseNull({ zodRef, schemas }: ParsingArgs<z.ZodNull>): SchemaObject {
return merge(
{
type: 'string' as SchemaObjectType,
format: 'null',
nullable: true,
type: 'null' as SchemaObjectType,
},
zodRef.description ? { description: zodRef.description } : {},
...schemas
);
}

function parseOptionalNullable({
function parseOptional({
schemas,
zodRef,
useOutput,
}: ParsingArgs<
z.ZodOptional<OpenApiZodAny> | z.ZodNullable<OpenApiZodAny>
>): SchemaObject {
}: ParsingArgs<z.ZodOptional<OpenApiZodAny>>): SchemaObject {
return merge(
generateSchema(zodRef.unwrap(), useOutput),
zodRef.description ? { description: zodRef.description } : {},
...schemas
);
}

function parseNullable({
schemas,
zodRef,
useOutput,
}: ParsingArgs<z.ZodNullable<OpenApiZodAny>>): SchemaObject {
const schema = generateSchema(zodRef.unwrap(), useOutput);
return merge(
{ ...schema, type: [schema.type, 'null'] as SchemaObjectType[] },
zodRef.description ? { description: zodRef.description } : {},
...schemas
);
}

function parseDefault({
schemas,
zodRef,
Expand Down Expand Up @@ -542,8 +551,8 @@ const workerMap = {
ZodBoolean: parseBoolean,
ZodDate: parseDate,
ZodNull: parseNull,
ZodOptional: parseOptionalNullable,
ZodNullable: parseOptionalNullable,
ZodOptional: parseOptional,
ZodNullable: parseNullable,
ZodDefault: parseDefault,
ZodArray: parseArray,
ZodLiteral: parseLiteral,
Expand Down Expand Up @@ -577,11 +586,9 @@ export function generateSchema(
useOutput?: boolean
): SchemaObject {
const { metaOpenApi = {} } = zodRef;
const schemas = [
zodRef.isNullable && zodRef.isNullable() ? { nullable: true } : {},
const schemas: AnatineSchemaObject[] = [
...(Array.isArray(metaOpenApi) ? metaOpenApi : [metaOpenApi]),
];

try {
const typeName = zodRef._def.typeName as WorkerKeys;
if (typeName in workerMap) {
Expand Down