Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rest): assign all component properties to target spec #2757

Merged
merged 1 commit into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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