Skip to content

Commit

Permalink
Merge 6880891 into 9de1133
Browse files Browse the repository at this point in the history
  • Loading branch information
dougal83 committed Jan 24, 2020
2 parents 9de1133 + 6880891 commit cf22e7a
Show file tree
Hide file tree
Showing 6 changed files with 468 additions and 0 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.

3 changes: 3 additions & 0 deletions packages/openapi-v3/package.json
Expand Up @@ -10,6 +10,7 @@
"@loopback/repository-json-schema": "^1.11.4",
"debug": "^4.1.1",
"json-merge-patch": "^0.2.3",
"json-schema-compare": "^0.2.2",
"lodash": "^4.17.15",
"openapi3-ts": "^1.3.0"
},
Expand All @@ -20,6 +21,8 @@
"@loopback/repository": "^1.17.0",
"@loopback/testlab": "^1.10.1",
"@types/debug": "^4.1.5",
"@types/json-merge-patch": "0.0.4",
"@types/json-schema-compare": "^0.2.0",
"@types/lodash": "^4.14.149",
"@types/node": "^10.17.13"
},
Expand Down
@@ -0,0 +1,257 @@
// Copyright IBM Corp. 2019. 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 {
OpenApiSpecBuilder,
OperationSpecBuilder,
} from '@loopback/openapi-spec-builder';
import {expect} from '@loopback/testlab';
import jsonmergepatch from 'json-merge-patch';
import {ConsolidationEnhancer} from '../../..';

const consolidationEnhancer = new ConsolidationEnhancer();

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

const expectedSpec = new OpenApiSpecBuilder()
.withOperation(
'get',
'/',
new OperationSpecBuilder().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/loopback.example',
},
},
},
}),
)
.build();

// TODO(dougal83): improve on patched test
const expectedComponents = {
components: {
schemas: {
'loopback.example': {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
},
},
},
};
jsonmergepatch.apply(expectedSpec, expectedComponents);

expect(consolidationEnhancer.modifySpec(inputSpec)).to.eql(expectedSpec);
});

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

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

it('Avoids naming collision', () => {
const inputSpec = new OpenApiSpecBuilder()
.withOperation(
'get',
'/',
new OperationSpecBuilder().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
},
},
},
}),
)
.build();

// TODO(dougal83): improve on patched test
const inputComponents = {
components: {
schemas: {
'loopback.example': {
title: 'Different loopback.example exists',
properties: {
testDiff: {
type: 'string',
},
},
},
},
},
};
jsonmergepatch.apply(inputSpec, inputComponents);

const expectedSpec = new OpenApiSpecBuilder()
.withOperation(
'get',
'/',
new OperationSpecBuilder().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/loopback.example1',
},
},
},
}),
)
.build();

// TODO(dougal83): improve on patched test
const expectedComponents = {
components: {
schemas: {
'loopback.example': {
title: 'Different loopback.example exists',
properties: {
testDiff: {
type: 'string',
},
},
},
'loopback.example1': {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
},
},
},
};
jsonmergepatch.apply(expectedSpec, expectedComponents);

expect(consolidationEnhancer.modifySpec(inputSpec)).to.eql(expectedSpec);
});

it('If array items has no title, copy parent title if exists', () => {
const inputSpec = new OpenApiSpecBuilder()
.withOperation(
'get',
'/',
new OperationSpecBuilder().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
myarray: {
title: 'MyArray',
type: 'array',
items: {
properties: {
test: {
type: 'string',
},
},
},
},
},
},
},
}),
)
.build();

const expectedSpec = new OpenApiSpecBuilder()
.withOperation(
'get',
'/',
new OperationSpecBuilder().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
myarray: {
title: 'MyArray',
type: 'array',
items: {
$ref: '#/components/schemas/MyArray.Items',
},
},
},
},
},
}),
)
.build();

// TODO(dougal83): improve on patched test
const expectedComponents = {
components: {
schemas: {
'MyArray.Items': {
title: 'MyArray.Items',
properties: {
test: {
type: 'string',
},
},
},
},
},
};
jsonmergepatch.apply(expectedSpec, expectedComponents);

expect(consolidationEnhancer.modifySpec(inputSpec)).to.eql(expectedSpec);
});
});

0 comments on commit cf22e7a

Please sign in to comment.