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

Fix handling of nulls, nullables, and exclusive{Min,Max} for OpenAPI 3.0 #235

Merged
merged 3 commits into from
Jan 20, 2025
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
10 changes: 9 additions & 1 deletion packages/zod-openapi/src/lib/zod-openapi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
aBigInt: z.bigint(),
aBoolean: z.boolean(),
aDate: z.date(),
aNullableString: z.string().nullable(),
aUnionIncludingNull: z.union([z.string(), z.null(), z.number()]),
aNumberMin: z.number().min(3).optional(),
aNumberGt: z.number().gt(5).optional(),
}),
{
description: `Primitives also testing overwriting of "required"`,
Expand All @@ -61,8 +65,12 @@
aBigInt: { type: 'integer', format: 'int64' },
aBoolean: { type: 'boolean' },
aDate: { type: 'string', format: 'date-time' },
aNullableString: { type: 'string', nullable: true },
aUnionIncludingNull: { oneOf: [{ type: 'string' }, { type: 'number' }], nullable: true },
aNumberMin: { type: 'number', minimum: 3 },
aNumberGt: { type: 'number', minimum: 5, exclusiveMinimum: true },
},
required: ['aBigInt', 'aBoolean', 'aDate', 'aNumber'],
required: ['aBigInt', 'aBoolean', 'aDate', 'aNullableString', 'aUnionIncludingNull', 'aNumber'],
description: 'Primitives also testing overwriting of "required"',
});
});
Expand Down Expand Up @@ -1039,7 +1047,7 @@
type Type = string;
const schema = z.object({
item: extendApi(
z.custom<Type>((data) => true),

Check warning on line 1050 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())
),
});
Expand Down
39 changes: 33 additions & 6 deletions packages/zod-openapi/src/lib/zod-openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
schema: T,
schemaObject: AnatineSchemaObject = {}
): T {
const This = (schema as any).constructor;

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

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected any. Specify a different type
const newSchema = new This(schema._def);
newSchema.metaOpenApi = Object.assign(
{},
Expand Down Expand Up @@ -170,12 +170,30 @@
checks.forEach((item) => {
switch (item.kind) {
case 'max':
if (item.inclusive) baseSchema.maximum = item.value;
else baseSchema.exclusiveMaximum = item.value;
if (item.inclusive || openApiVersion === '3.0') {
baseSchema.maximum = item.value;
}
if (!item.inclusive) {
if (openApiVersion === '3.0') {
// exclusiveMaximum has conflicting types in oas31 and oas30
baseSchema.exclusiveMaximum = true as unknown as number;
} else {
baseSchema.exclusiveMaximum = item.value;
}
}
break;
case 'min':
if (item.inclusive) baseSchema.minimum = item.value;
else baseSchema.exclusiveMinimum = item.value;
if (item.inclusive || openApiVersion === '3.0') {
baseSchema.minimum = item.value;
}
if (!item.inclusive) {
if (openApiVersion === '3.0') {
// exclusiveMinimum has conflicting types in oas31 and oas30
baseSchema.exclusiveMinimum = true as unknown as number;
} else {
baseSchema.exclusiveMinimum = item.value;
}
}
break;
case 'int':
baseSchema.type = typeFormat('integer', openApiVersion);
Expand Down Expand Up @@ -359,7 +377,9 @@
const schema = generateSchema(zodRef.unwrap(), useOutput, openApiVersion);
return merge(
schema,
{ type: typeFormat('null', openApiVersion) },
openApiVersion === '3.0'
? { nullable: true }
: { type: typeFormat('null', openApiVersion) },
zodRef.description ? { description: zodRef.description } : {},
...schemas
);
Expand Down Expand Up @@ -494,10 +514,17 @@
}
}

const oneOfContents =
openApiVersion === '3.0'
? contents.filter((content) => content._def.typeName !== 'ZodNull')
: contents;
const contentsHasNull = contents.length != oneOfContents.length;

return merge(
{
oneOf: contents.map((schema) => generateSchema(schema, useOutput, openApiVersion)),
oneOf: oneOfContents.map((schema) => generateSchema(schema, useOutput, openApiVersion)),
},
contentsHasNull ? { nullable: true } : {},
zodRef.description ? { description: zodRef.description } : {},
...schemas
);
Expand Down
Loading