Skip to content

Commit

Permalink
feat(rest): add openapi enhancer service
Browse files Browse the repository at this point in the history
add openapi spec enhancer to rest server

impl. loopbackio#4380

Signed-off-by: Douglas McConnachie <[email protected]>
  • Loading branch information
dougal83 committed Feb 17, 2020
1 parent 4a019c0 commit 1c3b292
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/rest/src/rest.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 1c3b292

Please sign in to comment.