Skip to content

Commit

Permalink
empty response passes response check #219
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Jan 16, 2020
1 parent d6f1f4c commit bccf173
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/middlewares/openapi.response.validator.ts
Expand Up @@ -104,6 +104,9 @@ export class ResponseValidator {
// assume valid
return;
}
if (!body) {
throw validationError(500, '.response', 'response body required.');
}
const valid = validator({
response: body,
});
Expand All @@ -127,6 +130,9 @@ export class ResponseValidator {
responses: OpenAPIV3.ResponsesObject,
): { [key: string]: ajv.ValidateFunction } {
const canValidate = response => {
if (!response.content) {
return 'no_content';
}
if (typeof response.content !== 'object') {
return false;
}
Expand Down Expand Up @@ -157,6 +163,9 @@ export class ResponseValidator {
// don't validate
// assume is valid
continue;
} else if (mediaTypeToValidate === 'no_content') {
schemas[name] = {};
continue;
}
const schema = response.content[mediaTypeToValidate].schema;

Expand Down
14 changes: 14 additions & 0 deletions test/resources/response.validation.yaml
Expand Up @@ -14,6 +14,20 @@ info:
servers:
- url: /v1
paths:
/empty_response:
description: get empty response
summary: get empty response
get:
description: get empty response
operationId: get empty resp
parameters:
- name: mode
in: query
schema:
type: string
responses:
'200':
description: empty
/pets:
description: endpoints for pets
summary: endpoints for pets
Expand Down
36 changes: 36 additions & 0 deletions test/response.validation.spec.ts
Expand Up @@ -19,6 +19,9 @@ describe(packageJson.name, () => {
},
3005,
app => {
app.get(`${app.basePath}/empty_response`, (req, res) => {
return res.end();
});
app.get(`${app.basePath}/users`, (req, res) => {
const json = ['user1', 'user2', 'user3'];
return res.json(json);
Expand All @@ -38,7 +41,12 @@ describe(packageJson.name, () => {
},
{ id: 3, name: 'name', tag: 'tag' },
];
} else if (req.query.mode === 'empty_object') {
json = {};
} else if (req.query.mode === 'empty_response') {
return res.json();
}

return res.json(json);
});
app.post(`${app.basePath}/no_additional_props`, (req, res) => {
Expand Down Expand Up @@ -70,6 +78,34 @@ describe(packageJson.name, () => {
.that.equals(500);
}));

it('should fail if response is response is empty object', async () =>
request(app)
.get(`${app.basePath}/pets?mode=empty_object`)
.expect(500)
.then((r: any) => {
console.log(r.body);
expect(r.body.message).to.contain('should be array');
expect(r.body)
.to.have.property('code')
.that.equals(500);
}));

it('should fail if response is response is empty', async () =>
request(app)
.get(`${app.basePath}/pets?mode=empty_response`)
.expect(500)
.then((r: any) => {
expect(r.body.message).to.contain('body required');
expect(r.body)
.to.have.property('code')
.that.equals(500);
}));

it('should return 200 for endpoints that return empty response', async () =>
request(app)
.get(`${app.basePath}/empty_response`)
.expect(200));

it('should fail if additional properties are provided when set false', async () =>
request(app)
.post(`${app.basePath}/no_additional_props`)
Expand Down

0 comments on commit bccf173

Please sign in to comment.