Skip to content

Commit

Permalink
fix(execute): fix encoding of objects and arrays with null and undefi…
Browse files Browse the repository at this point in the history
…ned values (#3726)

Refs swagger-api/swagger-ui#10226
  • Loading branch information
glowcloud authored Dec 3, 2024
1 parent 575d5d5 commit 42bd8a9
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/execute/oas3/style-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand Down
4 changes: 1 addition & 3 deletions src/http/serializers/request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
197 changes: 197 additions & 0 deletions test/oas3/execute/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 42bd8a9

Please sign in to comment.