Skip to content

Commit

Permalink
fix(execute): detect empty value for multi-value parameters (#3367)
Browse files Browse the repository at this point in the history
  • Loading branch information
glowcloud authored Feb 12, 2024
1 parent bd7417c commit 500dc17
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/execute/oas3/parameter-builders.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function header({ req, parameter, value }) {
return;
}

if (typeof value !== 'undefined') {
if (value !== undefined && !(Array.isArray(value) && value.length === 0)) {
req.headers[parameter.name] = stylize({
key: parameter.name,
value,
Expand All @@ -101,7 +101,7 @@ export function cookie({ req, parameter, value }) {
return;
}

if (type !== 'undefined') {
if (value !== undefined && !(Array.isArray(value) && value.length === 0)) {
const prefix =
type === 'object' && !Array.isArray(value) && parameter.explode ? '' : `${parameter.name}=`;

Expand Down
165 changes: 165 additions & 0 deletions test/execute/openapi-3-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,168 @@ describe('given OpenAPI 3.1.0 definition', () => {
});
});
});

describe('given OpenAPI 3.1.0 definition with multi-value parameters', () => {
const spec = {
openapi: '3.1.0',
info: {
title: 'Testing API',
version: '1.0.0',
},
servers: [
{
url: 'http://localhost:8080',
},
],
paths: {
'/findWithQuery': {
get: {
operationId: 'findWithQuery',
parameters: [
{
name: 'status',
in: 'query',
schema: {
type: 'array',
items: {
type: 'string',
enum: ['admin', 'customer'],
},
},
},
],
},
},
'/findWithHeader': {
get: {
operationId: 'findWithHeader',
parameters: [
{
name: 'status',
in: 'header',
schema: {
type: 'array',
items: {
type: 'string',
enum: ['admin', 'customer'],
},
},
},
],
},
},
'/findWithCookie': {
get: {
operationId: 'findWithCookie',
parameters: [
{
name: 'status',
in: 'cookie',
schema: {
type: 'array',
items: {
type: 'string',
enum: ['admin', 'customer'],
},
},
},
],
},
},
},
};

test('should build a request without parameters in query', () => {
const request = SwaggerClient.buildRequest({
spec,
operationId: 'findWithQuery',
parameters: { status: [] },
});

expect(request).toEqual({
url: 'http://localhost:8080/findWithQuery',
credentials: 'same-origin',
headers: {},
method: 'GET',
});
});

test('should build a request with an empty string parameter in query', () => {
const request = SwaggerClient.buildRequest({
spec,
operationId: 'findWithQuery',
parameters: { status: [''] },
});

expect(request).toEqual({
url: 'http://localhost:8080/findWithQuery?status=',
credentials: 'same-origin',
headers: {},
method: 'GET',
});
});

test('should build a request without parameters in header', () => {
const request = SwaggerClient.buildRequest({
spec,
operationId: 'findWithHeader',
parameters: { status: [] },
});

expect(request).toEqual({
url: 'http://localhost:8080/findWithHeader',
credentials: 'same-origin',
headers: {},
method: 'GET',
});
});

test('should build a request with an empty string parameter in header', () => {
const request = SwaggerClient.buildRequest({
spec,
operationId: 'findWithHeader',
parameters: { status: [''] },
});

expect(request).toEqual({
url: 'http://localhost:8080/findWithHeader',
credentials: 'same-origin',
headers: {
status: '',
},
method: 'GET',
});
});

test('should build a request without parameters in cookie', () => {
const request = SwaggerClient.buildRequest({
spec,
operationId: 'findWithCookie',
parameters: { status: [] },
});

expect(request).toEqual({
url: 'http://localhost:8080/findWithCookie',
credentials: 'same-origin',
headers: {},
method: 'GET',
});
});

test('should build a request with an empty string parameter in cookie', () => {
const request = SwaggerClient.buildRequest({
spec,
operationId: 'findWithCookie',
parameters: { status: [''] },
});

expect(request).toEqual({
url: 'http://localhost:8080/findWithCookie',
credentials: 'same-origin',
headers: {
Cookie: 'status=',
},
method: 'GET',
});
});
});

0 comments on commit 500dc17

Please sign in to comment.