Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(booter-lb3app): process generated OpenApiSpec of lb3 app #3623

Merged
merged 1 commit into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {OperationObject} from '@loopback/rest';
import {OperationObject, OpenApiSpec} from '@loopback/rest';
import {Client, expect} from '@loopback/testlab';
import * as _ from 'lodash';
import {
Expand Down Expand Up @@ -189,4 +189,30 @@ describe('booter-lb3app', () => {
await client.get('/coffee').expect(404);
});
});

context('using specTransformer to modify OpenAPI spec', () => {
before(async () => {
({app, client} = await setupApplication({
lb3app: {
path: '../fixtures/lb3app/server/server',
specTransformer: (spec: OpenApiSpec): OpenApiSpec =>
_.merge(spec, {
paths: {
'/CoffeeShops': {
post: {
summary: 'just a very simple modification',
},
},
},
}),
},
}));
});

it('does apply the spec modification', () => {
const spec = app.restServer.getApiSpec();
const createOp: OperationObject = spec.paths['/api/CoffeeShops'].post;
expect(createOp.summary).to.eql('just a very simple modification');
});
});
});
7 changes: 6 additions & 1 deletion packages/booter-lb3app/src/lb3app.booter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ export class Lb3AppBooter implements Booter {
// swagger2openapi options
});

return result.openapi as OpenApiSpec;
let spec = result.openapi as OpenApiSpec;
if (typeof this.options.specTransformer === 'function') {
spec = this.options.specTransformer(spec);
}
return spec;
}

private mountFullApp(lb3App: Lb3Application, spec: OpenApiSpec) {
Expand All @@ -128,6 +132,7 @@ export interface Lb3AppBooterOptions {
path: string;
mode: 'fullApp' | 'restRouter';
restApiRoot: string;
specTransformer?: (spec: OpenApiSpec) => OpenApiSpec;
Copy link
Contributor

@raymondfeng raymondfeng Sep 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm good to have specTransformer to be synchronous function as a starting point. We should consider generalizing the idea:

  1. Allow asynchronous transformation
  2. Create an extension point for spec transformers/processors to transform OpenAPI specs generated from controller metadata. For example, our authentication/authorization packages may want to add more information to the spec.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note this transformer is applied only for LB3 applications imported to LB4.

}

interface Lb3Application extends ExpressApplication {
Expand Down