Skip to content

Commit 8180a43

Browse files
Merge pull request #238 from kamilogorek/use-extended-schema-object
fix: Use AnatineSchemaObject in openapi extension method
2 parents 961ce2d + cbf5985 commit 8180a43

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

packages/zod-openapi/src/lib/zod-extensions.spec.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ describe('Zod Extensions', () => {
1212
const schema = z.object({
1313
one: z.string().openapi({examples: ['oneOne']}),
1414
two: z.number(),
15-
16-
}).openapi({examples: [{one: 'oneOne', two: 42}]})
15+
three: z.number().optional(),
16+
}).openapi({
17+
examples: [{one: 'oneOne', two: 42}],
18+
hideDefinitions: ['three']
19+
})
1720

1821
const apiSchema = generateSchema(schema);
1922

@@ -35,7 +38,8 @@ describe('Zod Extensions', () => {
3538
"one",
3639
"two"
3740
],
38-
"type": ["object"]
41+
"type": ["object"],
42+
"hideDefinitions": ["three"],
3943
})
4044
})
4145

packages/zod-openapi/src/lib/zod-extensions.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
This code is heavily inspired by https://github.com/asteasolutions/zod-to-openapi/blob/master/src/zod-extensions.ts
33
*/
44

5-
import { extendApi } from './zod-openapi';
5+
import { AnatineSchemaObject, extendApi } from './zod-openapi';
66
import {z} from "zod";
7-
import { SchemaObject } from "openapi3-ts/oas31";
87
import {ZodTypeDef} from "zod";
98

109

@@ -13,7 +12,7 @@ declare module 'zod' {
1312
interface ZodSchema<Output = any, Def extends ZodTypeDef = ZodTypeDef, Input = Output> {
1413
openapi<T extends ZodSchema<Output, Def, Input>>(
1514
this: T,
16-
metadata: Partial<SchemaObject>
15+
metadata: Partial<AnatineSchemaObject>
1716
): T;
1817
}
1918
}
@@ -27,7 +26,7 @@ export function extendZodWithOpenApi(zod: typeof z, forceOverride = false) {
2726
}
2827

2928
zod.ZodSchema.prototype.openapi = function (
30-
metadata?: Partial<SchemaObject>
29+
metadata?: Partial<AnatineSchemaObject>
3130
) {
3231
return extendApi(this, metadata)
3332
}

packages/zod-openapi/src/lib/zod-openapi.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { SchemaObject, SchemaObjectType } from 'openapi3-ts/oas31';
22
import merge from 'ts-deepmerge';
33
import { AnyZodObject, z, ZodTypeAny } from 'zod';
44

5-
type AnatineSchemaObject = SchemaObject & { hideDefinitions?: string[] };
5+
export type AnatineSchemaObject = SchemaObject & { hideDefinitions?: string[] };
66

77
export interface OpenApiZodAny extends ZodTypeAny {
88
metaOpenApi?: AnatineSchemaObject | AnatineSchemaObject[];
@@ -312,7 +312,7 @@ function parseBigInt({
312312
openApiVersion,
313313
}: ParsingArgs<z.ZodBigInt>): SchemaObject {
314314
return merge(
315-
{
315+
{
316316
type: typeFormat('integer', openApiVersion),
317317
format: 'int64'
318318
},

0 commit comments

Comments
 (0)