-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rest): add openapi schema consolidation
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
Showing
9 changed files
with
584 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
281 changes: 281 additions & 0 deletions
281
packages/rest/src/__tests__/unit/rest.server/consolidate.spec.extension.unit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
Oops, something went wrong.