diff --git a/src/execute/oas3/style-serializer.js b/src/execute/oas3/style-serializer.js index 0617aa8b3..13c6a4e6c 100644 --- a/src/execute/oas3/style-serializer.js +++ b/src/execute/oas3/style-serializer.js @@ -47,10 +47,10 @@ export function valueEncoder(value, escape = false) { value = String(value); } - if (escape && value.length > 0) { + if (escape && typeof value === 'string' && value.length > 0) { return encodeCharacters(value, escape); } - return value; + return value ?? ''; } function encodeArray({ key, value, style, explode, escape }) { diff --git a/src/http/serializers/request/index.js b/src/http/serializers/request/index.js index c6ddcc18f..093d832f7 100644 --- a/src/http/serializers/request/index.js +++ b/src/http/serializers/request/index.js @@ -40,9 +40,7 @@ function buildFormData(reqForm) { export const stringifyQuery = (queryObject, { encode = true } = {}) => { const buildNestedParams = (params, key, value) => { - if (value == null) { - params.append(key, ''); - } else if (Array.isArray(value)) { + if (Array.isArray(value)) { value.reduce((acc, v) => buildNestedParams(params, key, v), params); } else if (value instanceof Date) { params.append(key, value.toISOString()); diff --git a/test/oas3/execute/main.js b/test/oas3/execute/main.js index 88dc06a5c..38032c985 100644 --- a/test/oas3/execute/main.js +++ b/test/oas3/execute/main.js @@ -664,6 +664,203 @@ describe('buildRequest - OpenAPI Specification 3.0', () => { }); }); + it('should encode objects with a property with `null` value', () => { + const req = buildRequest({ + spec: { + openapi: '3.0.0', + paths: { + '/{pathPartial}': { + post: { + operationId: 'myOp', + parameters: [ + { + name: 'query', + in: 'query', + schema: { + type: 'object', + }, + }, + { + name: 'FooHeader', + in: 'header', + schema: { + type: 'object', + }, + explode: true, + }, + { + name: 'pathPartial', + in: 'path', + schema: { + type: 'object', + }, + explode: true, + }, + { + name: 'myCookie', + in: 'cookie', + schema: { + type: 'object', + }, + }, + ], + }, + }, + }, + }, + operationId: 'myOp', + parameters: { + pathPartial: { + a: null, + }, + query: { + b: null, + }, + FooHeader: { + c: null, + }, + myCookie: { + d: null, + }, + }, + }); + + expect(req).toEqual({ + method: 'POST', + url: `/a=?b=`, + credentials: 'same-origin', + headers: { + FooHeader: 'c=', + Cookie: 'myCookie=d,', + }, + }); + }); + + it('should encode arrays with `undefined` items', () => { + const req = buildRequest({ + spec: { + openapi: '3.0.0', + paths: { + '/{pathPartial}': { + post: { + operationId: 'myOp', + parameters: [ + { + name: 'query', + in: 'query', + schema: { + type: 'array', + }, + }, + { + name: 'FooHeader', + in: 'header', + schema: { + type: 'array', + }, + }, + { + name: 'pathPartial', + in: 'path', + schema: { + type: 'array', + }, + }, + { + name: 'myCookie', + in: 'cookie', + schema: { + type: 'array', + }, + }, + ], + }, + }, + }, + }, + operationId: 'myOp', + parameters: { + pathPartial: [undefined], + query: [undefined], + FooHeader: [undefined], + myCookie: [undefined], + }, + }); + + expect(req).toEqual({ + method: 'POST', + url: `/?query=`, + credentials: 'same-origin', + headers: { + FooHeader: '', + Cookie: 'myCookie=', + }, + }); + }); + + it('should encode arrays with multiple `undefined` items', () => { + const req = buildRequest({ + spec: { + openapi: '3.0.0', + paths: { + '/{pathPartial}': { + post: { + operationId: 'myOp', + parameters: [ + { + name: 'query', + in: 'query', + schema: { + type: 'array', + }, + explode: false, + }, + { + name: 'FooHeader', + in: 'header', + schema: { + type: 'array', + }, + }, + { + name: 'pathPartial', + in: 'path', + schema: { + type: 'array', + }, + }, + { + name: 'myCookie', + in: 'cookie', + schema: { + type: 'array', + }, + }, + ], + }, + }, + }, + }, + operationId: 'myOp', + parameters: { + pathPartial: [undefined, undefined, undefined], + query: [undefined, undefined, undefined], + FooHeader: [undefined, undefined, undefined], + myCookie: [undefined, undefined, undefined], + }, + }); + + expect(req).toEqual({ + method: 'POST', + url: `/,,?query=,,`, + credentials: 'same-origin', + headers: { + FooHeader: ',,', + Cookie: 'myCookie=,,', + }, + }); + }); + it('should encode arrays of arrays and objects', () => { const req = buildRequest({ spec: {