Skip to content

Commit

Permalink
fixup! add test
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed May 16, 2019
1 parent 8172c46 commit 3eec6a5
Showing 1 changed file with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ParameterObject,
SchemaObject,
} from '@loopback/openapi-v3-types';
import {model, property} from '@loopback/repository';
import {Entity, model, property} from '@loopback/repository';
import {expect} from '@loopback/testlab';
import {
api,
Expand Down Expand Up @@ -307,6 +307,71 @@ describe('controller spec', () => {
});
});

it('allows operations to get definitions of models when defined through a different method', async () => {
@model()
class Todo extends Entity {
@property({
type: 'string',
required: true,
})
title: string;
}

class MyController {
@get('/todos', {
responses: {
'200': {
description: 'Array of Category model instances',
content: {
'application/json': {
schema: {
$ref: '#/definitions/Todo',
definitions: {
Todo: {
title: 'Todo',
properties: {
title: {type: 'string'},
},
},
},
},
},
},
},
},
})
async find(): Promise<object[]> {
return []; // dummy implementation, it's never called
}

@get('/todos/{id}', {
responses: {
'200': {
content: {
'application/json': {
schema: {$ref: '#/components/schemas/Todo'},
},
},
},
},
})
async findById(): Promise<Todo> {
return new Todo();
}
}

const spec = getControllerSpec(MyController);
const opSpec: OperationObject = spec.paths['/todos/{id}'].get;
const responseSpec = opSpec.responses['200'].content['application/json'];
expect(responseSpec.schema).to.deepEqual({
$ref: '#/components/schemas/Todo',
});

const controller = new MyController();
const todo = await controller.findById();
expect(todo instanceof Todo).to.be.true();
});

it('returns undefined when it cannot find definition of referenced model', () => {
class MyController {
@get('/todos', {
Expand Down Expand Up @@ -358,7 +423,6 @@ describe('controller spec', () => {
'application/json': {
schema: {
$ref: '#/definitions/Todo',
definitions: {},
},
},
},
Expand Down

0 comments on commit 3eec6a5

Please sign in to comment.