From 320008c57985a7b158fe489c6ab08b6edbdb4bc3 Mon Sep 17 00:00:00 2001 From: Konrad Mattheis Date: Tue, 23 Mar 2021 16:47:34 +0100 Subject: [PATCH] fix(core): allow the apibody decorator to add examples and add $ref for schema --- lib/decorators/api-body.decorator.ts | 5 ++++- lib/swagger-explorer.ts | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/decorators/api-body.decorator.ts b/lib/decorators/api-body.decorator.ts index daec4e40a..705eab5d4 100644 --- a/lib/decorators/api-body.decorator.ts +++ b/lib/decorators/api-body.decorator.ts @@ -1,6 +1,8 @@ import { Type } from '@nestjs/common'; import { omit } from 'lodash'; import { + ExamplesObject, + ReferenceObject, RequestBodyObject, SchemaObject } from '../interfaces/open-api-spec.interface'; @@ -22,7 +24,8 @@ interface ApiBodyMetadata extends RequestBodyOptions { } interface ApiBodySchemaHost extends RequestBodyOptions { - schema: SchemaObject; + schema: SchemaObject | ReferenceObject; + examples?: ExamplesObject; } export type ApiBodyOptions = ApiBodyMetadata | ApiBodySchemaHost; diff --git a/lib/swagger-explorer.ts b/lib/swagger-explorer.ts index 9c9010370..3d0f5d37c 100644 --- a/lib/swagger-explorer.ts +++ b/lib/swagger-explorer.ts @@ -348,10 +348,13 @@ export class SwaggerExplorer { let consumes = mergeAndUniq(classConsumes, methodConsumes); consumes = isEmpty(consumes) ? ['application/json'] : consumes; - const keysToRemove = ['schema', 'in', 'name']; + const keysToRemove = ['schema', 'in', 'name', 'examples']; document.root.requestBody = { ...omit(requestBody, keysToRemove), - ...this.mimetypeContentWrapper.wrap(consumes, pick(requestBody, 'schema')) + ...this.mimetypeContentWrapper.wrap( + consumes, + pick(requestBody, ['schema', 'examples']) + ) }; return document; }