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
10 changes: 7 additions & 3 deletions packages/zod-nestjs/src/lib/create-zod-dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,16 @@
* array to a boolean.
*/
const schemaObject = properties[key] as SchemaObjectForMetadataFactory;
const schemaObjectWithRequiredField = {
const schemaObjectWithFixedFields = {
...schemaObject,
type: schemaObject.type ? schemaObject.type[0] : schemaObject.type,
Copy link
Contributor

@erkstruwe erkstruwe Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove 'null' from type array then. Besides, this doesn't work with string values (rather than arrays) which TypeScript allows here. See #182.

Copy link
Contributor Author

@A5rocks A5rocks Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my changes make this work, but there's a case (or two) which I'm not sure about: type: [] or type: ["null"]. What do you think should happen then?

As of latest commit those should lead to undefined type which is... Idk, should this error instead?

nullable: schemaObject.type ? schemaObject.type.includes('null') || undefined : undefined,
exclusiveMinimum: schemaObject.exclusiveMinimum ? true : schemaObject.exclusiveMinimum,
A5rocks marked this conversation as resolved.
Show resolved Hide resolved
exclusiveMaximum: schemaObject.exclusiveMaximum ? true : schemaObject.exclusiveMaximum,
};
schemaObjectWithRequiredField.required = !!(generatedSchema.required !== undefined,
schemaObjectWithFixedFields.required = !!(generatedSchema.required !== undefined,
generatedSchema.required?.includes(key));
properties[key] = schemaObjectWithRequiredField as any; // TODO: Fix this
properties[key] = schemaObjectWithFixedFields as any; // TODO: Fix this

Check warning on line 88 in packages/zod-nestjs/src/lib/create-zod-dto.ts

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected any. Specify a different type
}
return properties as Record<string, SchemaObject>;
}
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