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 with security enhancer by default

impl. #4365

Signed-off-by: Douglas McConnachie <[email protected]>
  • Loading branch information
dougal83 committed Feb 3, 2020
1 parent 5465865 commit aeb2ea5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ describe('RestApplication (integration)', () => {
servers: [{url: 'example.com:8080/api'}],
paths: {},
'x-foo': 'bar',
components: {
securitySchemes: {
jwt: {
bearerFormat: 'JWT',
scheme: 'bearer',
type: 'http',
},
},
},
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ describe('RestServer.getApiSpec()', () => {
servers: [{url: 'example.com:8080/api'}],
paths: {},
'x-foo': 'bar',
components: {
securitySchemes: {
jwt: {
bearerFormat: 'JWT',
scheme: 'bearer',
type: 'http',
},
},
},
});
});

Expand Down
29 changes: 29 additions & 0 deletions packages/rest/src/rest.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ 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,
SecuritySpecEnhancer,
ServerObject,
} from '@loopback/openapi-v3';
import {AssertionError} from 'assert';
Expand Down Expand Up @@ -128,6 +132,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 @@ -217,6 +227,21 @@ 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._registerCoreSpecEnhancers();
this._OASEnhancer = this.getSync(OAS_ENHANCER_SERVICE);
}

private _registerCoreSpecEnhancers() {
this.add(createBindingFromClass(SecuritySpecEnhancer));
}

protected _setupRequestHandlerIfNeeded() {
if (this._expressApp != null) return;
this._expressApp = express();
Expand Down Expand Up @@ -717,6 +742,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.applyEnhancerByName('security');

return spec;
}

Expand Down

0 comments on commit aeb2ea5

Please sign in to comment.