Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(execute): fix encoding of object properties with null value #3726

Merged
merged 5 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/execute/oas3/style-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ export default function stylize(config) {
}

export function valueEncoder(value, escape = false) {
if (Array.isArray(value) || (value !== null && typeof value === 'object')) {
if (Array.isArray(value) || typeof value === 'object') {
value = JSON.stringify(value);
} else if (typeof value === 'number' || typeof value === 'boolean') {
value = String(value);
}

if (escape && value.length > 0) {
if (escape && typeof value === 'string' && value.length > 0) {
return encodeCharacters(value, escape);
}
return value;
Expand Down
136 changes: 136 additions & 0 deletions test/oas3/execute/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,142 @@ 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=null?b=null`,
Copy link
Member

@char0n char0n Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks weird. Why would anybody want that? Le't think about this, if it make sense

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed this should have the same semantics as undefined - absence of a value, which is represented by ''

credentials: 'same-origin',
headers: {
FooHeader: 'c=null',
Cookie: 'myCookie=d,null',
},
});
});

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',
},
explode: true,
},
{
name: 'pathPartial',
in: 'path',
schema: {
type: 'array',
},
explode: true,
},
{
name: 'myCookie',
in: 'cookie',
schema: {
type: 'array',
},
},
],
},
},
},
},
operationId: 'myOp',
parameters: {
pathPartial: [undefined],
query: [undefined],
FooHeader: [undefined],
myCookie: [undefined],
glowcloud marked this conversation as resolved.
Show resolved Hide resolved
},
});

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