From ec52b89a8b103d4f99530ab10e3f908c24746698 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Mon, 24 Sep 2018 15:45:05 -0700 Subject: [PATCH] feat(cli): add responses for PingController.ping() --- .../src/controllers/ping.controller.ts.ejs | 35 +++++++++++++++++-- .../ping.controller.acceptance.ts.ejs | 4 ++- .../integration/generators/app.integration.js | 6 +++- packages/rest/src/index.ts | 1 + 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/packages/cli/generators/app/templates/src/controllers/ping.controller.ts.ejs b/packages/cli/generators/app/templates/src/controllers/ping.controller.ts.ejs index d61347c988f4..b83833a1efc8 100644 --- a/packages/cli/generators/app/templates/src/controllers/ping.controller.ts.ejs +++ b/packages/cli/generators/app/templates/src/controllers/ping.controller.ts.ejs @@ -1,6 +1,32 @@ -import {Request, RestBindings, get} from '@loopback/rest'; +import {Request, RestBindings, get, ResponseObject} from '@loopback/rest'; import {inject} from '@loopback/context'; +/** + * OpenAPI response for ping() + */ +const PING_RESPONSE: ResponseObject = { + description: 'Ping Response', + content: { + 'application/json': { + schema: { + type: 'object', + properties: { + greeting: {type: 'string'}, + date: {type: 'string'}, + url: {type: 'string'}, + headers: { + type: 'object', + patternProperties: { + '^.*$': {type: 'string'}, + }, + additionalProperties: false, + }, + }, + }, + }, + }, +}; + /** * A simple controller to bounce back http requests */ @@ -8,8 +34,13 @@ export class PingController { constructor(@inject(RestBindings.Http.REQUEST) private req: Request) {} // Map to `GET /ping` - @get('/ping') + @get('/ping', { + responses: { + '200': PING_RESPONSE, + }, + }) ping(): object { + // Reply with a greeting, the current time, the url, and request headers return { greeting: 'Hello from LoopBack', date: new Date(), diff --git a/packages/cli/generators/app/templates/test/acceptance/ping.controller.acceptance.ts.ejs b/packages/cli/generators/app/templates/test/acceptance/ping.controller.acceptance.ts.ejs index acaf4622bfb3..aaaf55d20d00 100644 --- a/packages/cli/generators/app/templates/test/acceptance/ping.controller.acceptance.ts.ejs +++ b/packages/cli/generators/app/templates/test/acceptance/ping.controller.acceptance.ts.ejs @@ -2,6 +2,7 @@ import { Client, createRestAppClient, givenHttpServerConfig, + expect, } from '@loopback/testlab'; import {<%= project.applicationName %>} from '../..'; @@ -25,7 +26,8 @@ describe('PingController', () => { }); it('invokes GET /ping', async () => { - await client.get('/ping?msg=world').expect(200); + const res = await client.get('/ping?msg=world').expect(200); + expect(res.body).to.containEql({greeting: 'Hello from LoopBack'}); }); function givenAnApplication() { diff --git a/packages/cli/test/integration/generators/app.integration.js b/packages/cli/test/integration/generators/app.integration.js index 82442bab4980..d8e549f01b22 100644 --- a/packages/cli/test/integration/generators/app.integration.js +++ b/packages/cli/test/integration/generators/app.integration.js @@ -52,13 +52,17 @@ describe('app-generator specific files', () => { assert.fileContent('src/controllers/ping.controller.ts', /@inject/); assert.fileContent( 'src/controllers/ping.controller.ts', - /@get\('\/ping'\)/, + /@get\('\/ping'\, \{/, ); assert.fileContent('src/controllers/ping.controller.ts', /ping\(\)/); assert.fileContent( 'src/controllers/ping.controller.ts', /\'\@loopback\/rest\'/, ); + assert.fileContent( + 'test/acceptance/ping.controller.acceptance.ts', + /describe\('PingController'/, + ); }); }); diff --git a/packages/rest/src/index.ts b/packages/rest/src/index.ts index 5fec39d1d20f..e7bdf8771117 100644 --- a/packages/rest/src/index.ts +++ b/packages/rest/src/index.ts @@ -37,3 +37,4 @@ import * as HttpErrors from 'http-errors'; export {HttpErrors}; export * from '@loopback/openapi-v3'; +export * from '@loopback/openapi-v3-types';