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

More complete OpenAPI 3.1 support in zod-openapi. #173

Merged
merged 15 commits into from
Mar 20, 2024
2 changes: 1 addition & 1 deletion apps/example/src/app/cats/cats.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const GetCatsParamsZ = extendApi(
id: z.string(),
}),
{
example: { id: 'mouse-terminator-2000' },
examples: [{ id: 'mouse-terminator-2000' }],
}
);

Expand Down
29 changes: 15 additions & 14 deletions apps/example/src/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('Cats', () => {
.set('Accept', 'application/json')
.expect(200)
.expect({
// todo: this should report 3.1.0 (or use openapi 3.0.0 supporting zod-openapi)
openapi: '3.0.0',
paths: {
'/': {
Expand Down Expand Up @@ -162,42 +163,42 @@ describe('Cats', () => {
components: {
schemas: {
GetCatsDto: {
type: 'object',
type: ['object'],
properties: {
cats: {
type: 'array',
items: { type: 'string' },
type: ['array'],
items: { type: ['string'] },
description: 'List of cats',
},
},
required: ['cats'],
title: 'Get Cat Response',
},
CatDto: {
type: 'object',
type: ['object'],
properties: {
name: { type: 'string' },
age: { type: 'number' },
breed: { type: 'string' },
name: { type: ['string'] },
age: { type: ['number'] },
breed: { type: ['string'] },
},
required: ['name', 'age', 'breed'],
title: 'Cat',
description: 'A cat',
},
CreateCatResponseDto: {
type: 'object',
type: ['object'],
properties: {
success: { type: 'boolean' },
message: { type: 'string' },
name: { type: 'string' },
success: { type: ['boolean'] },
message: { type: ['string'] },
name: { type: ['string'] },
},
required: ['success', 'message', 'name'],
},
UpdateCatDto: {
type: 'object',
type: ['object'],
properties: {
age: { type: 'number' },
breed: { type: 'string' },
age: { type: ['number'] },
breed: { type: ['string'] },
},
required: ['age', 'breed'],
},
Expand Down
2 changes: 1 addition & 1 deletion packages/zod-nestjs/src/lib/create-zod-dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export const createZodDto = <T extends OpenApiZodAny>(
convertedSchemaObject.type.includes('null') || undefined;
convertedSchemaObject.type = convertedSchemaObject.type.find(
(item) => item !== 'null'
);
) || 'string';
} else if (convertedSchemaObject.type === 'null') {
convertedSchemaObject.type = 'string'; // There ist no explicit null value in OpenAPI 3.0
convertedSchemaObject.nullable = true;
Expand Down
16 changes: 8 additions & 8 deletions packages/zod-openapi/src/lib/zod-extensions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ describe('Zod Extensions', () => {
extendZodWithOpenApi(z, true)

const schema = z.object({
one: z.string().openapi({example: 'oneOne'}),
one: z.string().openapi({examples: ['oneOne']}),
two: z.number(),

}).openapi({example: {one: 'oneOne', two: 42}})
}).openapi({examples: [{one: 'oneOne', two: 42}]})

const apiSchema = generateSchema(schema);

expect(apiSchema).toEqual({
"example": {
"examples": [{
"one": "oneOne",
"two": 42
},
}],
"properties": {
"one": {
"example": "oneOne",
"type": "string"
"examples": ["oneOne"],
"type": ["string"]
},
"two": {
"type": "number"
"type": ["number"]
}
},
"required": [
"one",
"two"
],
"type": "object"
"type": ["object"]
})
})

Expand Down
Loading
Loading