🐛 Fixed server error for repeated order query parameter

closes #12263

- Express parses repeated query parameters as an array (req.query properties). Because there is no clear reason on why not to support this behavior extended order parameter parsing logic to handle arrays. This follows the rule of "liberal inputs, conservative outputs"
- Example supported query string for ordering can now look like: `?order=featured&order=published_at asc`, the priority of the order stays the same with the most significant appearing first and least significant last
This commit is contained in:
Naz 2020-11-03 00:15:24 +13:00
parent 14ac6168ac
commit 7326241718

View File

@ -7,14 +7,22 @@ const order = function order(Bookshelf) {
parseOrderOption: function (orderQueryString, withRelated) {
let orderAttributes;
let result;
let rules;
let rules = [];
orderAttributes = this.orderAttributes();
if (withRelated && withRelated.indexOf('count.posts') > -1) {
orderAttributes.push('count.posts');
}
result = {};
rules = orderQueryString.split(',');
// CASE: repeat order query parameter keys are present
if (_.isArray(orderQueryString)) {
orderQueryString.forEach((qs) => {
rules.push(...qs.split(','));
});
} else {
rules = orderQueryString.split(',');
}
_.each(rules, function (rule) {
let match;