Skip to content

Commit

Permalink
feat(rest): add openapi schema consolidation
Browse files Browse the repository at this point in the history
Add openapi schema enhancer to rest server. Consolidates openapi schema,
by creating references to schema to reduce duplication.

Can be disabled by setting rest option openApiSpec.consolidate to false.

feat(openapi-v3): getEnhancerByName accept generic parameter

Signed-off-by: Douglas McConnachie <[email protected]>
  • Loading branch information
dougal83 committed Apr 19, 2020
1 parent fc8570a commit 1990c7b
Show file tree
Hide file tree
Showing 11 changed files with 672 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/openapi-v3/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/openapi-v3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@loopback/testlab": "^3.0.1",
"@types/debug": "^4.1.5",
"@types/http-status": "^1.1.2",
"@types/json-merge-patch": "0.0.4",
"@types/lodash": "^4.14.150",
"@types/node": "^10.17.20"
},
Expand Down
6 changes: 4 additions & 2 deletions packages/openapi-v3/src/enhancers/spec-enhancer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ export class OASEnhancerService {
* Find an enhancer by its name
* @param name The name of the enhancer you want to find
*/
async getEnhancerByName(name: string): Promise<OASEnhancer | undefined> {
async getEnhancerByName<T extends OASEnhancer = OASEnhancer>(
name: string,
): Promise<T | undefined> {
// Get the latest list of enhancers
const enhancers = await this.getEnhancers();
return enhancers.find(e => e.name === name);
return enhancers.find(e => e.name === name) as T | undefined;
}

/**
Expand Down
23 changes: 23 additions & 0 deletions packages/rest/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"debug": "^4.1.1",
"express": "^4.17.1",
"http-errors": "^1.7.3",
"json-schema-compare": "^0.2.2",
"js-yaml": "^3.13.1",
"lodash": "^4.17.15",
"on-finished": "^2.3.0",
Expand All @@ -57,6 +58,7 @@
"@loopback/repository": "^2.1.1",
"@loopback/testlab": "^3.0.1",
"@types/debug": "^4.1.5",
"@types/json-schema-compare": "^0.2.0",
"@types/js-yaml": "^3.12.3",
"@types/lodash": "^4.14.150",
"@types/multer": "^1.4.3",
Expand Down
160 changes: 159 additions & 1 deletion packages/rest/src/__tests__/integration/rest.server.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
// License text available at https://opensource.org/licenses/MIT

import {Application} from '@loopback/core';
import {anOpenApiSpec, anOperationSpec} from '@loopback/openapi-spec-builder';
import {
aComponentsSpec,
anOpenApiSpec,
anOperationSpec,
} from '@loopback/openapi-spec-builder';
import {
createClientForHandler,
createRestAppClient,
Expand Down Expand Up @@ -875,6 +879,160 @@ paths:
await server.stop();
});

it('disables consolidator if openApiSpec.consolidate option is set to false', async () => {
const options = {openApiSpec: {consolidate: false}};
const server = await givenAServer({rest: options});

const EXPECTED_SPEC = anOpenApiSpec()
.withOperation(
'get',
'/',
anOperationSpec().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
},
},
},
}),
)
.build();

server.route('get', '/', EXPECTED_SPEC.paths['/'].get, () => {});

await server.start();
const spec = await server.getApiSpec();
expect(spec).to.eql(EXPECTED_SPEC);
await server.stop();
});

it('runs consolidator if openApiSpec.consolidate option is set to true', async () => {
const options = {openApiSpec: {consolidate: true}};
const server = await givenAServer({rest: options});

const EXPECTED_SPEC = anOpenApiSpec()
.withOperation(
'get',
'/',
anOperationSpec().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/loopback.example',
},
},
},
}),
)
.withComponents(
aComponentsSpec().withSchema('loopback.example', {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
}),
)
.build();

server.route(
'get',
'/',
anOperationSpec()
.withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
},
},
},
})
.build(),
() => {},
);

await server.start();
const spec = await server.getApiSpec();
expect(spec).to.eql(EXPECTED_SPEC);
await server.stop();
});

it('runs consolidator if openApiSpec.consolidate option is undefined', async () => {
const options = {openApiSpec: {consolidate: undefined}};
const server = await givenAServer({rest: options});

const EXPECTED_SPEC = anOpenApiSpec()
.withOperation(
'get',
'/',
anOperationSpec().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/loopback.example',
},
},
},
}),
)
.withComponents(
aComponentsSpec().withSchema('loopback.example', {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
}),
)
.build();

server.route(
'get',
'/',
anOperationSpec()
.withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
},
},
},
})
.build(),
() => {},
);

await server.start();
const spec = await server.getApiSpec();
expect(spec).to.eql(EXPECTED_SPEC);
await server.stop();
});

it('registers controller routes under routes.*', async () => {
const server = await givenAServer();
server.controller(DummyController);
Expand Down
Loading

0 comments on commit 1990c7b

Please sign in to comment.