Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow JSON Response to Return Boolean false Value #383

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/middlewares/openapi.response.validator.ts
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
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
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);
}));
});