From 1734293edecdb4ca8ce9abdc4cfde13730bacb30 Mon Sep 17 00:00:00 2001 From: Nora Date: Wed, 15 May 2019 16:12:24 -0400 Subject: [PATCH] test: test for @api decorator --- .../controller-spec.integration.ts | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) 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 5e4a9ba020a4..22ec185fe52c 100644 --- a/packages/openapi-v3/src/__tests__/integration/controller-spec.integration.ts +++ b/packages/openapi-v3/src/__tests__/integration/controller-spec.integration.ts @@ -11,6 +11,7 @@ import { import {model, property} from '@loopback/repository'; import {expect} from '@loopback/testlab'; import { + api, ControllerSpec, get, getControllerSpec, @@ -332,6 +333,66 @@ describe('controller spec', () => { expect(globalSchemas).to.be.empty(); }); + it('allows a class to provide definitions of referenced models through #/components/schemas', () => { + @api({ + paths: { + '/todos': { + get: { + 'x-operation-name': 'find', + 'x-controller-name': 'MyController', + responses: { + '200': { + content: { + 'application/json': { + schema: { + $ref: '#/components/schemas/Todo', + }, + }, + }, + }, + }, + }, + }, + }, + components: { + schemas: { + Todo: { + title: 'Todo', + properties: { + title: { + type: 'string', + }, + }, + }, + }, + }, + }) + class MyController { + async find(): Promise { + return []; // dummy implementation, it's never called + } + } + + const spec = getControllerSpec(MyController); + const opSpec: OperationObject = spec.paths['/todos'].get; + const responseSpec = opSpec.responses['200'].content['application/json']; + expect(responseSpec.schema).to.deepEqual({ + $ref: '#/components/schemas/Todo', + }); + + const globalSchemas = (spec.components || {}).schemas; + expect(globalSchemas).to.deepEqual({ + Todo: { + title: 'Todo', + properties: { + title: { + type: 'string', + }, + }, + }, + }); + }); + describe('x-ts-type', () => { @model() class MyModel {