Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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