Skip to content

Commit

Permalink
chore: move open api spec modification to post app.boot()
Browse files Browse the repository at this point in the history
The servers open api spec info is preserved when api spec is modified post app.boot()

Signed-off-by: Dominique Emond <dremond@ca.ibm.com>
  • Loading branch information
emonddr committed Oct 7, 2019
1 parent 7febb33 commit e8dea8d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
22 changes: 9 additions & 13 deletions packages/shopping/src/application.ts
Expand Up @@ -29,7 +29,8 @@ import {
import {PasswordHasherBindings} from './keys';
import {BcryptHasher} from './services/hash.password.bcryptjs';
import {JWTAuthenticationStrategy} from './authentication-strategies/jwt-strategy';
import {SECURITY_SCHEME_SPEC} from './utils/security-spec';
import {RestBindings} from '@loopback/rest';
import {addSecuritySchemesToOpenApiSpec} from './utils/security-spec';

/**
* Information from package.json
Expand All @@ -49,18 +50,6 @@ export class ShoppingApplication extends BootMixin(
constructor(options?: ApplicationConfig) {
super(options);

//
// Defines security scheme in OpenAPI spec.
// Controller method must enforce security scheme.
// See '/users/me' endpoint in user.controller.ts
//
this.api({
openapi: '3.0.0',
info: {title: pkg.name, version: pkg.version},
paths: {},
components: {securitySchemes: SECURITY_SCHEME_SPEC},
});

this.setUpBindings();

// Bind authentication component related elements
Expand Down Expand Up @@ -113,7 +102,14 @@ export class ShoppingApplication extends BootMixin(
this.bind(UserServiceBindings.USER_SERVICE).toClass(MyUserService);
}

modifyApiSpecAndRebind() {
let openAPISchema = this.getSync(RestBindings.API_SPEC);
addSecuritySchemesToOpenApiSpec(openAPISchema);
this.bind(RestBindings.API_SPEC).to(openAPISchema);
}

async start(): Promise<void> {
this.modifyApiSpecAndRebind();
await super.start();
}
}
8 changes: 8 additions & 0 deletions packages/shopping/src/utils/security-spec.ts
Expand Up @@ -4,6 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

import {SecuritySchemeObject, ReferenceObject} from '@loopback/openapi-v3';
import {OpenApiSpec} from '@loopback/rest';

export const SECURITY_SPEC_OPERATION = [{bearerAuth: []}];
export type SecuritySchemeObjects = {
Expand All @@ -16,3 +17,10 @@ export const SECURITY_SCHEME_SPEC: SecuritySchemeObjects = {
bearerFormat: 'JWT',
},
};


export function addSecuritySchemesToOpenApiSpec(schema: OpenApiSpec) {
schema.components = schema.components || {};
schema.components.securitySchemes = schema.components.securitySchemes || {};
Object.assign(schema.components.securitySchemes, SECURITY_SCHEME_SPEC);
}

0 comments on commit e8dea8d

Please sign in to comment.