Skip to content

Commit

Permalink
fix(execute): accept falsy values in formData parameters (#3435)
Browse files Browse the repository at this point in the history
Refs #3434
  • Loading branch information
glowcloud authored Mar 25, 2024
1 parent 0883d2d commit 7e1cf7b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/execute/swagger2/parameter-builders.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ function bodyBuilder({ req, value }) {

// Add a form data object.
function formDataBuilder({ req, value, parameter }) {
if (value === false && parameter.type === 'boolean') {
value = 'false';
}

if (value === 0 && ['number', 'integer'].indexOf(parameter.type) > -1) {
value = '0';
}

if (value || parameter.allowEmptyValue) {
req.form = req.form || {};
req.form[parameter.name] = {
Expand Down
52 changes: 52 additions & 0 deletions test/execute/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,58 @@ describe('execute', () => {
// Then
expect(req.body).toEqual('petId=1,2,3');
});

test('should correctly process falsy parameters', () => {
// Given
const spec = {
host: 'swagger.io',
basePath: '/v1',
paths: {
'/pets/find': {
get: {
operationId: 'getMe',
parameters: [
{
in: 'formData',
name: 'status',
type: 'boolean',
required: false,
},
{
in: 'formData',
name: 'id',
type: 'integer',
required: false,
},
],
responses: {
200: {
description: 'ok',
},
},
},
},
},
};

// When
const req = buildRequest({
spec,
operationId: 'getMe',
parameters: { status: false, id: 0 },
});

// Then
expect(req).toEqual({
url: 'http://swagger.io/v1/pets/find',
method: 'GET',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'status=false&id=0',
});
});
});

test('should correctly process boolean parameters', () => {
Expand Down

0 comments on commit 7e1cf7b

Please sign in to comment.