From 3eec6a51b19cc8b775425b2206dc20df3a341049 Mon Sep 17 00:00:00 2001 From: Nora Date: Thu, 16 May 2019 14:41:12 -0400 Subject: [PATCH] fixup! add test --- .../controller-spec.integration.ts | 68 ++++++++++++++++++- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/packages/openapi-v3/src/__tests__/integration/controller-spec.integration.ts b/packages/openapi-v3/src/__tests__/integration/controller-spec.integration.ts index aafbde53fad9..c0afb04bb217 100644 --- a/packages/openapi-v3/src/__tests__/integration/controller-spec.integration.ts +++ b/packages/openapi-v3/src/__tests__/integration/controller-spec.integration.ts @@ -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, @@ -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 { + return []; // dummy implementation, it's never called + } + + @get('/todos/{id}', { + responses: { + '200': { + content: { + 'application/json': { + schema: {$ref: '#/components/schemas/Todo'}, + }, + }, + }, + }, + }) + async findById(): Promise { + 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', { @@ -358,7 +423,6 @@ describe('controller spec', () => { 'application/json': { schema: { $ref: '#/definitions/Todo', - definitions: {}, }, }, },