Skip to content

Commit

Permalink
fix(execute): skip undefined values serialization for OpenAPI 3.x.y (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
glowcloud committed Mar 25, 2024
1 parent 0fa7806 commit 5808f2a
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/execute/oas3/parameter-builders.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,37 @@ import serialize from './content-serializer.js';
export function path({ req, value, parameter }) {
const { name, style, explode, content } = parameter;

if (value === undefined) return;

if (content) {
const effectiveMediaType = Object.keys(content)[0];

req.url = req.url
.split(`{${name}}`)
.join(encodeDisallowedCharacters(serialize(value, effectiveMediaType), { escape: true }));
return;
}

const styledValue = stylize({
key: parameter.name,
value,
style: style || 'simple',
explode: explode || false,
escape: true,
});
} else {
const styledValue = stylize({
key: parameter.name,
value,
style: style || 'simple',
explode: explode || false,
escape: true,
});

req.url = req.url.split(`{${name}}`).join(styledValue);
req.url = req.url.replace(new RegExp(`{${name}}`, 'g'), styledValue);
}
}

export function query({ req, value, parameter }) {
req.query = req.query || {};

if (parameter.content) {
if (value !== undefined && parameter.content) {
const effectiveMediaType = Object.keys(parameter.content)[0];
const serializedValue = serialize(value, effectiveMediaType);

if (serializedValue) {
req.query[parameter.name] = serializedValue;
} else if (parameter.allowEmptyValue && value !== undefined) {
} else if (parameter.allowEmptyValue) {
const paramName = parameter.name;
req.query[paramName] = req.query[paramName] || {};
req.query[paramName].allowEmptyValue = true;
Expand Down Expand Up @@ -72,7 +73,7 @@ export function header({ req, parameter, value }) {
return;
}

if (parameter.content) {
if (value !== undefined && parameter.content) {
const effectiveMediaType = Object.keys(parameter.content)[0];

req.headers[parameter.name] = serialize(value, effectiveMediaType);
Expand All @@ -94,7 +95,7 @@ export function cookie({ req, parameter, value }) {
req.headers = req.headers || {};
const type = typeof value;

if (parameter.content) {
if (value !== undefined && parameter.content) {
const effectiveMediaType = Object.keys(parameter.content)[0];

req.headers.Cookie = `${parameter.name}=${serialize(value, effectiveMediaType)}`;
Expand Down
79 changes: 79 additions & 0 deletions test/oas3/execute/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,85 @@ describe('buildRequest - OpenAPI Specification 3.0', () => {
},
});
});

it('should not serialize undefined parameters', () => {
const spec = {
openapi: '3.0.1',
info: {
title: 'OpenAPI definition',
version: 'v0',
},
servers: [
{
url: 'http://localhost:8080',
description: 'Generated server url',
},
],
paths: {
'/{pathParam}': {
post: {
operationId: 'undefinedParams',
parameters: [
{
name: 'pathParam',
in: 'path',
content: {
'text/plain': {
schema: {},
},
},
},
{
name: 'headerParam',
in: 'header',
content: {
'text/plain': {
schema: {},
},
},
},
{
name: 'queryParam',
in: 'query',
content: {
'text/plain': {
schema: {},
},
},
},
{
name: 'cookieParam',
in: 'cookie',
content: {
'text/plain': {
schema: {},
},
},
},
],
},
},
},
};

const request = buildRequest({
spec,
operationId: 'undefinedParams',
parameters: {
pathParam: undefined,
queryParam: undefined,
headerParam: undefined,
cookieParam: undefined,
},
});

expect(request).toEqual({
url: 'http://localhost:8080/{pathParam}',
credentials: 'same-origin',
headers: {},
method: 'POST',
});
});
});
describe('baseUrl', () => {
// Verify that given serverUrl and contextUrl produces exepcted baseUrl
Expand Down

0 comments on commit 5808f2a

Please sign in to comment.