From 0c04248deb42967aa22bec4fa1d059079f0707f4 Mon Sep 17 00:00:00 2001 From: Douglas McConnachie Date: Sat, 28 Mar 2020 11:35:25 +0000 Subject: [PATCH] feat(rest): add disableOASConsolidator option to rest server Signed-off-by: Douglas McConnachie --- .../rest.server.open-api-spec.unit.ts | 27 ++++++++++++++++++- packages/rest/src/rest.server.ts | 10 ++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/rest/src/__tests__/unit/rest.server/rest.server.open-api-spec.unit.ts b/packages/rest/src/__tests__/unit/rest.server/rest.server.open-api-spec.unit.ts index bae31899799d..b86bc5fcace9 100644 --- a/packages/rest/src/__tests__/unit/rest.server/rest.server.open-api-spec.unit.ts +++ b/packages/rest/src/__tests__/unit/rest.server/rest.server.open-api-spec.unit.ts @@ -12,11 +12,16 @@ import { requestBody, } from '@loopback/openapi-v3'; import {model, property} from '@loopback/repository'; -import {expect, validateApiSpec} from '@loopback/testlab'; +import { + expect, + givenHttpServerConfig, + validateApiSpec, +} from '@loopback/testlab'; import { createControllerFactoryForClass, RestComponent, RestServer, + RestServerConfig, } from '../../..'; import {RestTags} from '../../../keys'; import {TestInfoSpecEnhancer} from './fixtures/info.spec.extension'; @@ -387,6 +392,26 @@ describe('RestServer.getApiSpec()', () => { expect(spec.info).to.eql(EXPECTED_SPEC_INFO); }); + context('options', () => { + it('disables consolidator if disableOASConsolidator is set to true', async () => { + const options: {rest: RestServerConfig} = { + rest: {openApiSpec: {disableOASConsolidator: true}}, + }; + options.rest = givenHttpServerConfig(options.rest); + app = new Application(options); + app.component(RestComponent); + server = await app.getServer(RestServer); + await server.start(); + + // consolidation enhancer to be unset + const enhancer = await server.OASEnhancer.getEnhancerByName( + 'loopback.consolidate.schemas', + ); + expect(enhancer).to.be.undefined(); + await server.stop(); + }); + }); + async function givenApplication() { app = new Application(); app.component(RestComponent); diff --git a/packages/rest/src/rest.server.ts b/packages/rest/src/rest.server.ts index 6772a08ed0c7..8085d5e651fe 100644 --- a/packages/rest/src/rest.server.ts +++ b/packages/rest/src/rest.server.ts @@ -254,7 +254,9 @@ export class RestServer extends Context implements Server, HttpServerLike { * Register core built in OAS Enhancers */ registerCoreEnhancers() { - this.add(createBindingFromClass(ConsolidationEnhancer)); + if (!this.config.openApiSpec.disableOASConsolidator) { + this.add(createBindingFromClass(ConsolidationEnhancer)); + } } protected _setupRequestHandlerIfNeeded() { @@ -1075,6 +1077,12 @@ export interface OpenApiSpecOptions { * Set this flag to disable the endpoint for OpenAPI spec */ disabled?: true; + + /** + * Set this flag to `true` to disable OAS schema consolidation. If not set, + * the value defaults to `false`. + */ + disableOASConsolidator?: boolean; } export interface ApiExplorerOptions {