diff --git a/packages/rest/src/rest.server.ts b/packages/rest/src/rest.server.ts index bb6e2387aedc..f43c443e6e39 100644 --- a/packages/rest/src/rest.server.ts +++ b/packages/rest/src/rest.server.ts @@ -9,12 +9,15 @@ import { BindingScope, Constructor, Context, + createBindingFromClass, inject, } from '@loopback/context'; import {Application, CoreBindings, Server} from '@loopback/core'; import {HttpServer, HttpServerOptions} from '@loopback/http-server'; import { getControllerSpec, + OASEnhancerService, + OAS_ENHANCER_SERVICE, OpenAPIObject, OpenApiSpec, OperationObject, @@ -130,6 +133,12 @@ export class RestServer extends Context implements Server, HttpServerLike { * @param res - The response. */ + protected _OASEnhancer: OASEnhancerService; + public get OASEnhancer(): OASEnhancerService { + this._setupOASEnhancerIfNeeded(); + return this._OASEnhancer; + } + protected _requestHandler: HttpRequestListener; public get requestHandler(): HttpRequestListener { if (this._requestHandler == null) { @@ -219,6 +228,16 @@ export class RestServer extends Context implements Server, HttpServerLike { this.bind(RestBindings.HANDLER).toDynamicValue(() => this.httpHandler); } + protected _setupOASEnhancerIfNeeded() { + if (this._OASEnhancer != null) return; + this.add( + createBindingFromClass(OASEnhancerService, { + key: OAS_ENHANCER_SERVICE, + }).inScope(BindingScope.SINGLETON), + ); + this._OASEnhancer = this.getSync(OAS_ENHANCER_SERVICE); + } + protected _setupRequestHandlerIfNeeded() { if (this._expressApp != null) return; this._expressApp = express(); @@ -754,6 +773,10 @@ export class RestServer extends Context implements Server, HttpServerLike { spec = this.updateSpecFromRequest(spec, requestContext); } + // Apply OAS enhancers to the OpenAPI specification + this.OASEnhancer.spec = spec; + spec = await this.OASEnhancer.applyAllEnhancers(); + return spec; }