Skip to content

Commit ec52b89

Browse files
committed
feat(cli): add responses for PingController.ping()
1 parent 2bfd50e commit ec52b89

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

packages/cli/generators/app/templates/src/controllers/ping.controller.ts.ejs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,46 @@
1-
import {Request, RestBindings, get} from '@loopback/rest';
1+
import {Request, RestBindings, get, ResponseObject} from '@loopback/rest';
22
import {inject} from '@loopback/context';
33

4+
/**
5+
* OpenAPI response for ping()
6+
*/
7+
const PING_RESPONSE: ResponseObject = {
8+
description: 'Ping Response',
9+
content: {
10+
'application/json': {
11+
schema: {
12+
type: 'object',
13+
properties: {
14+
greeting: {type: 'string'},
15+
date: {type: 'string'},
16+
url: {type: 'string'},
17+
headers: {
18+
type: 'object',
19+
patternProperties: {
20+
'^.*$': {type: 'string'},
21+
},
22+
additionalProperties: false,
23+
},
24+
},
25+
},
26+
},
27+
},
28+
};
29+
430
/**
531
* A simple controller to bounce back http requests
632
*/
733
export class PingController {
834
constructor(@inject(RestBindings.Http.REQUEST) private req: Request) {}
935

1036
// Map to `GET /ping`
11-
@get('/ping')
37+
@get('/ping', {
38+
responses: {
39+
'200': PING_RESPONSE,
40+
},
41+
})
1242
ping(): object {
43+
// Reply with a greeting, the current time, the url, and request headers
1344
return {
1445
greeting: 'Hello from LoopBack',
1546
date: new Date(),

packages/cli/generators/app/templates/test/acceptance/ping.controller.acceptance.ts.ejs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
Client,
33
createRestAppClient,
44
givenHttpServerConfig,
5+
expect,
56
} from '@loopback/testlab';
67
import {<%= project.applicationName %>} from '../..';
78

@@ -25,7 +26,8 @@ describe('PingController', () => {
2526
});
2627

2728
it('invokes GET /ping', async () => {
28-
await client.get('/ping?msg=world').expect(200);
29+
const res = await client.get('/ping?msg=world').expect(200);
30+
expect(res.body).to.containEql({greeting: 'Hello from LoopBack'});
2931
});
3032

3133
function givenAnApplication() {

packages/cli/test/integration/generators/app.integration.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,17 @@ describe('app-generator specific files', () => {
5252
assert.fileContent('src/controllers/ping.controller.ts', /@inject/);
5353
assert.fileContent(
5454
'src/controllers/ping.controller.ts',
55-
/@get\('\/ping'\)/,
55+
/@get\('\/ping'\, \{/,
5656
);
5757
assert.fileContent('src/controllers/ping.controller.ts', /ping\(\)/);
5858
assert.fileContent(
5959
'src/controllers/ping.controller.ts',
6060
/\'\@loopback\/rest\'/,
6161
);
62+
assert.fileContent(
63+
'test/acceptance/ping.controller.acceptance.ts',
64+
/describe\('PingController'/,
65+
);
6266
});
6367
});
6468

packages/rest/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ import * as HttpErrors from 'http-errors';
3737
export {HttpErrors};
3838

3939
export * from '@loopback/openapi-v3';
40+
export * from '@loopback/openapi-v3-types';

0 commit comments

Comments
 (0)