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.

Signed-off-by: Douglas McConnachie <[email protected]>
  • Loading branch information
dougal83 committed Apr 13, 2020
1 parent 20b2602 commit dfe44ab
Show file tree
Hide file tree
Showing 9 changed files with 584 additions and 2 deletions.
29 changes: 29 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.149",
"@types/node": "^10.17.19"
},
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.149",
"@types/multer": "^1.4.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
// Copyright IBM Corp. 2020. All Rights Reserved.
// Node module: @loopback/openapi-v3
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {
ComponentsSpecBuilder,
OpenApiSpecBuilder,
OperationSpecBuilder,
} from '@loopback/openapi-spec-builder';
import {expect} from '@loopback/testlab';
import {ConsolidationEnhancer} from '../../../spec-enhancers/consolidate.spec-enhancer';

const consolidationEnhancer = new ConsolidationEnhancer();

describe('consolidateSchemaObjects', () => {
it('moves schema with title to component.schemas, replaces with reference', () => {
const INPUT_SPEC = new OpenApiSpecBuilder()
.withOperation(
'get',
'/',
new OperationSpecBuilder().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
},
},
},
}),
)
.build();

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

expect(consolidationEnhancer.modifySpec(INPUT_SPEC)).to.eql(EXPECTED_SPEC);
});

it('ignores schema without title property', () => {
const INPUT_SPEC = new OpenApiSpecBuilder()
.withOperation(
'get',
'/',
new OperationSpecBuilder().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
properties: {
test: {
type: 'string',
},
},
},
},
},
}),
)
.build();

expect(consolidationEnhancer.modifySpec(INPUT_SPEC)).to.eql(INPUT_SPEC);
});

it('avoids naming collision', () => {
const INPUT_SPEC = new OpenApiSpecBuilder()
.withOperation(
'get',
'/',
new OperationSpecBuilder().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
},
},
},
}),
)
.withComponents(
new ComponentsSpecBuilder().withSchema('loopback.example', {
title: 'Different loopback.example exists',
properties: {
testDiff: {
type: 'string',
},
},
}),
)
.build();

const EXPECTED_SPEC = new OpenApiSpecBuilder()
.withOperation(
'get',
'/',
new OperationSpecBuilder().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/loopback.example1',
},
},
},
}),
)
.withComponents(
new ComponentsSpecBuilder()
.withSchema('loopback.example', {
title: 'Different loopback.example exists',
properties: {
testDiff: {
type: 'string',
},
},
})
.withSchema('loopback.example1', {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
}),
)
.build();

expect(consolidationEnhancer.modifySpec(INPUT_SPEC)).to.eql(EXPECTED_SPEC);
});

it('consolidates same schema in multiple locations', () => {
const INPUT_SPEC = new OpenApiSpecBuilder()
.withOperation(
'get',
// first time has 'loopback.example'
'/path1',
new OperationSpecBuilder().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
},
},
},
}),
)
.withOperation(
'get',
// second time has 'loopback.example'
'/path2',
new OperationSpecBuilder().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
},
},
},
}),
)
.build();

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

expect(consolidationEnhancer.modifySpec(INPUT_SPEC)).to.eql(EXPECTED_SPEC);
});

it('obeys disabled option when set to true', () => {
consolidationEnhancer.disabled = true;
const INPUT_SPEC = new OpenApiSpecBuilder()
.withOperation(
'get',
'/',
new OperationSpecBuilder().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
},
},
},
}),
)
.build();

expect(consolidationEnhancer.modifySpec(INPUT_SPEC)).to.eql(INPUT_SPEC);
});
});
Loading

0 comments on commit dfe44ab

Please sign in to comment.