Skip to content

Commit

Permalink
fix(backport): fix paramsSerializer function validation
Browse files Browse the repository at this point in the history
  • Loading branch information
solonzhu committed Apr 18, 2024
1 parent 3021e0d commit 8427c90
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/core/Axios.js
Expand Up @@ -61,15 +61,19 @@ Axios.prototype.request = function request(configOrUrl, config) {

var paramsSerializer = config.paramsSerializer;

if (paramsSerializer !== undefined) {
validator.assertOptions(paramsSerializer, {
encode: validators.function,
serialize: validators.function
}, true);
if (paramsSerializer != null) {
if (utils.isFunction(paramsSerializer)) {
config.paramsSerializer = {
serialize: paramsSerializer
};
} else {
validator.assertOptions(paramsSerializer, {
encode: validators.function,
serialize: validators.function
}, true);
}
}

utils.isFunction(paramsSerializer) && (config.paramsSerializer = {serialize: paramsSerializer});

// filter out skipped interceptors
var requestInterceptorChain = [];
var synchronousRequestInterceptors = true;
Expand Down
20 changes: 20 additions & 0 deletions test/unit/adapters/http.js
Expand Up @@ -1532,4 +1532,24 @@ describe('supports http with nodejs', function () {
}).catch(done);
});
});

it('should support function as paramsSerializer value', function (done) {
server = http.createServer(function (req, res) {
res.end('ok');
}).listen(4444, function () {
var data;

axios.post('http://localhost:4444', 'test', {
params: {
x: 1
},
paramsSerializer: () => 'foo',
maxRedirects: 0,
}).then(function (res) {
assert.strictEqual(res.request.path, '/?foo');
data = res;
done()
}).catch(done);
});
});
});

0 comments on commit 8427c90

Please sign in to comment.