Skip to content

Commit ad22256

Browse files
committed
fix: treat an empty @uploadedfiles array as missing when the parameter is required
A multipart request that carries other fields but no file reaches the handler as an empty array (multer sets req.files = []), not as undefined, so the required check let it through and the action ran with no files. An absent @uploadedfile was already rejected because req.file stays undefined.
1 parent bf6fbfd commit ad22256

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

‎src/ActionParameterHandler.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ export class ActionParameterHandler<T extends BaseDriver> {
8181
} else {
8282
if (!value) return Promise.reject(new AuthorizationRequiredError(action));
8383
}
84+
} else if (param.type === 'files' && (isValueEmpty || (Array.isArray(value) && value.length === 0))) {
85+
// a multipart request that carries other fields but no file arrives as an empty array, not as undefined
86+
return Promise.reject(new ParamRequiredError(action, param));
8487
} else if (param.name && isValueEmpty) {
8588
// regular check for all other parameters // todo: figure out something with param.name usage and multiple things params (query params, upload files etc.)
8689
return Promise.reject(new ParamRequiredError(action, param));

‎test/functional/action-params.spec.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,4 +1138,18 @@ describe(``, () => {
11381138
expect(error.response.status).toEqual(HttpStatusCodes.INTERNAL_SERVER_ERROR);
11391139
}
11401140
});
1141+
1142+
it('@UploadedFiles when required is used a multipart request with other fields but no file is a bad request', async () => {
1143+
expect.assertions(1);
1144+
const form = new FormData();
1145+
form.append('caption', 'no photos attached');
1146+
1147+
try {
1148+
await axios.post('/photos-with-required', form, {
1149+
headers: form.getHeaders(),
1150+
});
1151+
} catch (error) {
1152+
expect(error.response.status).toEqual(HttpStatusCodes.BAD_REQUEST);
1153+
}
1154+
});
11411155
});

0 commit comments

Comments
 (0)