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 4 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 @@ -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
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],
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 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
Loading