Skip to content

Commit

Permalink
fix(rest): add components.requestBodies to spec
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed Apr 15, 2019
1 parent 301ccb2 commit f21f1be
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
43 changes: 39 additions & 4 deletions packages/rest/src/__tests__/unit/router/assign-router-spec.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {},
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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'}],
Expand All @@ -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);
});
});
9 changes: 9 additions & 0 deletions packages/rest/src/router/router-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit f21f1be

Please sign in to comment.