Skip to content

Commit

Permalink
if requestBody required is false, allow empty requests (cdimascio#665)
Browse files Browse the repository at this point in the history
* if requestBody required is false, allow empty requests

* add test
  • Loading branch information
cykoder authored Oct 9, 2021
1 parent 62437d5 commit f94ca7a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/middlewares/parsers/body.parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ export class BodySchemaParser {
}

if (!content) {
// check if required is false, if so allow request when no content type is supplied
const contentNotProvided = contentType.contentType === 'not_provided';
if ((contentType.contentType === undefined || contentNotProvided) && requestBody.required === false) {
return {};
}
const msg =
contentType.contentType === 'not_provided'
contentNotProvided
? 'media type not specified'
: `unsupported media type ${contentType.contentType}`;
throw new UnsupportedMediaType({ path: path, message: msg });
Expand Down
5 changes: 5 additions & 0 deletions test/optional-request-body.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ describe(packageJson.name, () => {
.set('Content-Type', 'application/json')
.expect(201));

it('create document should return 201 with empty body', async () =>
request(app)
.post(`/documents`)
.expect(201));

it('return 415', async () =>
request(app)
.post(`/documents`)
Expand Down
1 change: 1 addition & 0 deletions test/optional-request-body.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ paths:
post:
summary: Create a document
requestBody:
required: false
content:
application/json:
schema:
Expand Down

0 comments on commit f94ca7a

Please sign in to comment.