Skip to content

Commit

Permalink
Allow JSON Response to Return Boolean false Value (#383)
Browse files Browse the repository at this point in the history
* Check for body being undefined or null explicitly

* Add test cases
  • Loading branch information
DomParfitt committed Oct 2, 2020
1 parent b0c2a0c commit dec510e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/middlewares/openapi.response.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export class ResponseValidator {
const accepts: [string] = contentType
? [contentType]
: accept
? accept.split(',').map((h) => h.trim())
: [];
? accept.split(',').map((h) => h.trim())
: [];

return this._validate({
validators,
Expand Down Expand Up @@ -136,7 +136,7 @@ export class ResponseValidator {
return;
}

if (!body) {
if (body === undefined || body === null) {
throw new InternalServerError({
path: '.response',
message: 'response body required.',
Expand Down Expand Up @@ -280,4 +280,4 @@ export class ResponseValidator {
mediaTypeParsed.subtype === 'json' || mediaTypeParsed.suffix === 'json'
);
}
}
}
16 changes: 16 additions & 0 deletions test/resources/response.validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ paths:
responses:
'200':
description: empty
/boolean:
description: get boolean responses
get:
operationId: boolean
parameters:
- name: value
in: query
schema:
type: boolean
responses:
'200':
description: boolean
content:
application/json:
schema:
type: boolean
/object:
description: endpoints for pets
summary: endpoints for pets
Expand Down
19 changes: 19 additions & 0 deletions test/response.validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ describe(packageJson.name, () => {
app.get(`${app.basePath}/empty_response`, (req, res) => {
return res.end();
});
app.get(`${app.basePath}/boolean`, (req, res) => {
return res.json(req.query.value);
})
app.get(`${app.basePath}/object`, (req, res) => {
return res.json([
{ id: 1, name: 'name', tag: 'tag', bought_at: null },
Expand Down Expand Up @@ -197,4 +200,20 @@ describe(packageJson.name, () => {
.then((r: any) => {
expect(r.body).is.an('array').with.length(3);
}));

it('should be able to return `true` as the response body', async () =>
request(app)
.get(`${app.basePath}/boolean?value=true`)
.expect(200)
.then((r: any) => {
expect(r.body).to.equal(true);
}));

it('should be able to return `false` as the response body', async () =>
request(app)
.get(`${app.basePath}/boolean?value=false`)
.expect(200)
.then((r: any) => {
expect(r.body).to.equal(false);
}));
});

0 comments on commit dec510e

Please sign in to comment.