Skip to content

Commit

Permalink
Merge pull request #235 from statsig-io/fix-nulls
Browse files Browse the repository at this point in the history
Fix handling of `null`s, nullables, and exclusive{Min,Max} for OpenAPI 3.0
  • Loading branch information
Brian-McBride authored Jan 20, 2025
2 parents 5ffa0a0 + dee5469 commit 8d4fa35
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
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 @@ describe('zodOpenapi', () => {
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 @@ describe('zodOpenapi', () => {
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
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 @@ -170,12 +170,30 @@ function parseNumber({
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 @@ function parseNullable({
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 @@ function parseUnion({
}
}

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

0 comments on commit 8d4fa35

Please sign in to comment.