From 7e1cf7b4af0545eb296f4bde3bddabfd6280352e Mon Sep 17 00:00:00 2001 From: Oliwia Rogala Date: Mon, 25 Mar 2024 16:22:21 +0100 Subject: [PATCH] fix(execute): accept falsy values in formData parameters (#3435) Refs #3434 --- src/execute/swagger2/parameter-builders.js | 8 ++++ test/execute/main.js | 52 ++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/execute/swagger2/parameter-builders.js b/src/execute/swagger2/parameter-builders.js index 1c829da3a..d5da60909 100644 --- a/src/execute/swagger2/parameter-builders.js +++ b/src/execute/swagger2/parameter-builders.js @@ -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] = { diff --git a/test/execute/main.js b/test/execute/main.js index 814ae79a3..b45247710 100644 --- a/test/execute/main.js +++ b/test/execute/main.js @@ -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', () => {