Skip to content

Commit

Permalink
Merge pull request #206 from adambrgmn/support-open-api-3.0
Browse files Browse the repository at this point in the history
Bring back support for Open API v3.0
  • Loading branch information
Brian-McBride authored Jun 21, 2024
2 parents 132e472 + 65fc3d3 commit b7955c6
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 57 deletions.
30 changes: 15 additions & 15 deletions apps/example/src/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('Cats', () => {
// console.log(inspect(body, false, 10, true));
// });

it(`Swagger Test`, () => {
it(`Swagger Test`, async () => {
return request(app.getHttpServer())
.get('/api-json')
.set('Accept', 'application/json')
Expand Down Expand Up @@ -163,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
13 changes: 9 additions & 4 deletions packages/zod-nestjs/src/lib/patch-nest-swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ interface Type<T = any> extends Function {
new (...args: any[]): T;
}

type SchemaObjectFactoryModule =
typeof import('@nestjs/swagger/dist/services/schema-object-factory');

export const patchNestjsSwagger = (
schemaObjectFactoryModule = require('@nestjs/swagger/dist/services/schema-object-factory')
schemaObjectFactoryModule: SchemaObjectFactoryModule | undefined = undefined,
openApiVersion: '3.0' | '3.1' = '3.0'
): void => {
// eslint-disable-next-line @typescript-eslint/no-var-requires,@typescript-eslint/naming-convention
const { SchemaObjectFactory } = schemaObjectFactoryModule;
const { SchemaObjectFactory } = (schemaObjectFactoryModule ??
require('@nestjs/swagger/dist/services/schema-object-factory')) as SchemaObjectFactoryModule;

const orgExploreModelSchema =
SchemaObjectFactory.prototype.exploreModelSchema;
Expand All @@ -29,6 +33,7 @@ export const patchNestjsSwagger = (
// schemas: Record<string, SchemaObject>,
// schemaRefsStack: string[] = []
) {
// @ts-expect-error Reported as private, but since we are patching, we will be able to reach it
if (this.isLazyTypeFunc(type)) {
// eslint-disable-next-line @typescript-eslint/ban-types
type = (type as Function)();
Expand All @@ -38,7 +43,7 @@ export const patchNestjsSwagger = (
return orgExploreModelSchema.call(this, type, schemas, schemaRefsStack);
}

schemas[type.name] = generateSchema(type.zodSchema);
schemas[type.name] = generateSchema(type.zodSchema, false, openApiVersion);

return type.name;
};
Expand Down
30 changes: 30 additions & 0 deletions packages/zod-openapi/src/lib/zod-openapi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,36 @@ describe('zodOpenapi', () => {
});
});

it('should support basic primitives for OpenAPI v3.0', () => {
const zodSchema = extendApi(
z.object({
aString: z.string().describe('A test string').optional(),
aNumber: z.number().optional(),
aBigInt: z.bigint(),
aBoolean: z.boolean(),
aDate: z.date(),
}),
{
description: `Primitives also testing overwriting of "required"`,
required: ['aNumber'], // All schema settings "merge"
}
);
const apiSchema = generateSchema(zodSchema, false, '3.0');

expect(apiSchema).toEqual({
type: 'object',
properties: {
aString: { description: 'A test string', type: 'string' },
aNumber: { type: 'number' },
aBigInt: { type: 'integer', format: 'int64' },
aBoolean: { type: 'boolean' },
aDate: { type: 'string', format: 'date-time' },
},
required: ['aBigInt', 'aBoolean', 'aDate', 'aNumber'],
description: 'Primitives also testing overwriting of "required"',
});
});

it('should support empty types', () => {
const zodSchema = extendApi(
z.object({
Expand Down
Loading

0 comments on commit b7955c6

Please sign in to comment.