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..fcb0028949ab 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,29 @@ import {Request, RestBindings, get} from '@loopback/rest'; import {inject} from '@loopback/context'; +const PING_RESPONSE = { + 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,7 +31,11 @@ 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 { return { greeting: 'Hello from LoopBack', 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..960a2f67bc20 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,47 @@ 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'}); + }); + + it('exposes OpenAPI spec at /openapi.json', async () => { + const res = await client.get('/openapi.json').expect(200); + expect(res.body).to.containDeep({ + paths: { + '/ping': { + get: { + 'x-controller-name': 'PingController', + 'x-operation-name': 'ping', + tags: ['PingController'], + responses: { + '200': { + 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, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }); }); 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'/, + ); }); });