Skip to content

Commit

Permalink
fix(rest): assign all component properties to target spec
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed May 9, 2019
1 parent dce0b9a commit af06b69
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
55 changes: 40 additions & 15 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 @@ -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: {},
Expand All @@ -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',
},
},
},
},
},
Expand All @@ -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: {
Expand Down Expand Up @@ -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'}],
Expand Down
8 changes: 5 additions & 3 deletions packages/rest/src/router/router-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {OpenApiSpec} from '@loopback/openapi-v3-types';
export type RouterSpec = Pick<OpenApiSpec, 'paths' | 'components' | 'tags'>;

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) {
Expand Down

0 comments on commit af06b69

Please sign in to comment.