diff --git a/packages/rest/src/__tests__/unit/router/assign-router-spec.unit.ts b/packages/rest/src/__tests__/unit/router/assign-router-spec.unit.ts index 95dff1f931f3..c1f96516dc2d 100644 --- a/packages/rest/src/__tests__/unit/router/assign-router-spec.unit.ts +++ b/packages/rest/src/__tests__/unit/router/assign-router-spec.unit.ts @@ -7,7 +7,7 @@ import {expect} from '@loopback/testlab'; import {assignRouterSpec, RouterSpec} from '../../../'; describe('assignRouterSpec', () => { - it('duplicates the additions spec if the target spec is empty', async () => { + it('duplicates the additions spec if the target spec is empty', () => { const target: RouterSpec = {paths: {}}; const additions: RouterSpec = { paths: { @@ -37,15 +37,26 @@ describe('assignRouterSpec', () => { }, }, }, + requestBodies: { + Greeting: { + content: { + 'application/json': { + schema: { + $ref: '#/components/schemas/Greeting', + }, + }, + }, + }, + }, }, tags: [{name: 'greeting', description: 'greetings'}], }; assignRouterSpec(target, additions); - expect(target).to.eql(additions); + expect(target).to.deepEqual(additions); }); - it('does not assign components without schema', async () => { + it('assigns all components', () => { const target: RouterSpec = { paths: {}, components: {}, @@ -54,15 +65,24 @@ describe('assignRouterSpec', () => { const additions: RouterSpec = { paths: {}, components: { - parameters: { - addParam: { - name: 'add', - in: 'query', - description: 'number of items to add', - required: true, - schema: { - type: 'integer', - format: 'int32', + schemas: { + Greeting: { + type: 'object', + properties: { + message: { + type: 'string', + }, + }, + }, + }, + requestBodies: { + Greeting: { + content: { + 'application/json': { + schema: { + $ref: '#/components/schemas/Greeting', + }, + }, }, }, }, @@ -75,10 +95,15 @@ describe('assignRouterSpec', () => { }; assignRouterSpec(target, additions); - expect(target.components).to.be.empty(); + expect(target.components).to.deepEqual(additions.components); + expect(target.components).to.have.properties([ + 'schemas', + 'requestBodies', + 'responses', + ]); }); - it('uses the route registered first', async () => { + it('uses the route registered first', () => { const originalPath = { '/': { get: { @@ -129,7 +154,7 @@ describe('assignRouterSpec', () => { expect(target.paths).to.eql(originalPath); }); - it('does not duplicate tags from the additional spec', async () => { + it('does not duplicate tags from the additional spec', () => { const target: RouterSpec = { paths: {}, tags: [{name: 'greeting', description: 'greetings'}], diff --git a/packages/rest/src/router/router-spec.ts b/packages/rest/src/router/router-spec.ts index 31b8aa2e0a40..6ac78cdefbfb 100644 --- a/packages/rest/src/router/router-spec.ts +++ b/packages/rest/src/router/router-spec.ts @@ -8,10 +8,12 @@ import {OpenApiSpec} from '@loopback/openapi-v3-types'; export type RouterSpec = Pick; export function assignRouterSpec(target: RouterSpec, additions: RouterSpec) { - if (additions.components && additions.components.schemas) { + if (additions.components) { if (!target.components) target.components = {}; - if (!target.components.schemas) target.components.schemas = {}; - Object.assign(target.components.schemas, additions.components.schemas); + for (const key in additions.components) { + if (!target.components[key]) target.components[key] = {}; + Object.assign(target.components[key], additions.components[key]); + } } for (const url in additions.paths) {