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..0b0a311b60c6 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: { @@ -45,7 +45,7 @@ describe('assignRouterSpec', () => { expect(target).to.eql(additions); }); - it('does not assign components without schema', async () => { + it('does not assign components without schema', () => { const target: RouterSpec = { paths: {}, components: {}, @@ -78,7 +78,7 @@ describe('assignRouterSpec', () => { expect(target.components).to.be.empty(); }); - it('uses the route registered first', async () => { + it('uses the route registered first', () => { const originalPath = { '/': { get: { @@ -129,7 +129,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'}], @@ -148,4 +148,39 @@ describe('assignRouterSpec', () => { {name: 'salutation', description: 'salutations!'}, ]); }); + + it("assigns request bodies to the target's components when they exist", () => { + const target: RouterSpec = {paths: {}}; + const additions: RouterSpec = { + paths: {}, + components: { + schemas: { + Greeting: { + type: 'object', + properties: { + message: { + type: 'string', + }, + }, + }, + }, + requestBodies: { + Greeting: { + content: { + 'application/json': { + schema: { + $ref: '#/components/schemas/Greeting', + }, + }, + }, + description: 'Model instance data', + }, + }, + }, + tags: [{name: 'greeting', description: 'greetings'}], + }; + + assignRouterSpec(target, additions); + expect(target).to.eql(additions); + }); }); diff --git a/packages/rest/src/router/router-spec.ts b/packages/rest/src/router/router-spec.ts index 31b8aa2e0a40..b5b7a4c59d7e 100644 --- a/packages/rest/src/router/router-spec.ts +++ b/packages/rest/src/router/router-spec.ts @@ -12,6 +12,15 @@ export function assignRouterSpec(target: RouterSpec, additions: RouterSpec) { if (!target.components) target.components = {}; if (!target.components.schemas) target.components.schemas = {}; Object.assign(target.components.schemas, additions.components.schemas); + + if (additions.components.requestBodies) { + if (!target.components.requestBodies) + target.components.requestBodies = {}; + Object.assign( + target.components.requestBodies, + additions.components.requestBodies, + ); + } } for (const url in additions.paths) {