Skip to content

Commit 42bd8a9

Browse files
authored
fix(execute): fix encoding of objects and arrays with null and undefined values (#3726)
Refs swagger-api/swagger-ui#10226
1 parent 575d5d5 commit 42bd8a9

File tree

3 files changed

+200
-5
lines changed

3 files changed

+200
-5
lines changed

src/execute/oas3/style-serializer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ export function valueEncoder(value, escape = false) {
4747
value = String(value);
4848
}
4949

50-
if (escape && value.length > 0) {
50+
if (escape && typeof value === 'string' && value.length > 0) {
5151
return encodeCharacters(value, escape);
5252
}
53-
return value;
53+
return value ?? '';
5454
}
5555

5656
function encodeArray({ key, value, style, explode, escape }) {

src/http/serializers/request/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ function buildFormData(reqForm) {
4040

4141
export const stringifyQuery = (queryObject, { encode = true } = {}) => {
4242
const buildNestedParams = (params, key, value) => {
43-
if (value == null) {
44-
params.append(key, '');
45-
} else if (Array.isArray(value)) {
43+
if (Array.isArray(value)) {
4644
value.reduce((acc, v) => buildNestedParams(params, key, v), params);
4745
} else if (value instanceof Date) {
4846
params.append(key, value.toISOString());

test/oas3/execute/main.js

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,203 @@ describe('buildRequest - OpenAPI Specification 3.0', () => {
664664
});
665665
});
666666

667+
it('should encode objects with a property with `null` value', () => {
668+
const req = buildRequest({
669+
spec: {
670+
openapi: '3.0.0',
671+
paths: {
672+
'/{pathPartial}': {
673+
post: {
674+
operationId: 'myOp',
675+
parameters: [
676+
{
677+
name: 'query',
678+
in: 'query',
679+
schema: {
680+
type: 'object',
681+
},
682+
},
683+
{
684+
name: 'FooHeader',
685+
in: 'header',
686+
schema: {
687+
type: 'object',
688+
},
689+
explode: true,
690+
},
691+
{
692+
name: 'pathPartial',
693+
in: 'path',
694+
schema: {
695+
type: 'object',
696+
},
697+
explode: true,
698+
},
699+
{
700+
name: 'myCookie',
701+
in: 'cookie',
702+
schema: {
703+
type: 'object',
704+
},
705+
},
706+
],
707+
},
708+
},
709+
},
710+
},
711+
operationId: 'myOp',
712+
parameters: {
713+
pathPartial: {
714+
a: null,
715+
},
716+
query: {
717+
b: null,
718+
},
719+
FooHeader: {
720+
c: null,
721+
},
722+
myCookie: {
723+
d: null,
724+
},
725+
},
726+
});
727+
728+
expect(req).toEqual({
729+
method: 'POST',
730+
url: `/a=?b=`,
731+
credentials: 'same-origin',
732+
headers: {
733+
FooHeader: 'c=',
734+
Cookie: 'myCookie=d,',
735+
},
736+
});
737+
});
738+
739+
it('should encode arrays with `undefined` items', () => {
740+
const req = buildRequest({
741+
spec: {
742+
openapi: '3.0.0',
743+
paths: {
744+
'/{pathPartial}': {
745+
post: {
746+
operationId: 'myOp',
747+
parameters: [
748+
{
749+
name: 'query',
750+
in: 'query',
751+
schema: {
752+
type: 'array',
753+
},
754+
},
755+
{
756+
name: 'FooHeader',
757+
in: 'header',
758+
schema: {
759+
type: 'array',
760+
},
761+
},
762+
{
763+
name: 'pathPartial',
764+
in: 'path',
765+
schema: {
766+
type: 'array',
767+
},
768+
},
769+
{
770+
name: 'myCookie',
771+
in: 'cookie',
772+
schema: {
773+
type: 'array',
774+
},
775+
},
776+
],
777+
},
778+
},
779+
},
780+
},
781+
operationId: 'myOp',
782+
parameters: {
783+
pathPartial: [undefined],
784+
query: [undefined],
785+
FooHeader: [undefined],
786+
myCookie: [undefined],
787+
},
788+
});
789+
790+
expect(req).toEqual({
791+
method: 'POST',
792+
url: `/?query=`,
793+
credentials: 'same-origin',
794+
headers: {
795+
FooHeader: '',
796+
Cookie: 'myCookie=',
797+
},
798+
});
799+
});
800+
801+
it('should encode arrays with multiple `undefined` items', () => {
802+
const req = buildRequest({
803+
spec: {
804+
openapi: '3.0.0',
805+
paths: {
806+
'/{pathPartial}': {
807+
post: {
808+
operationId: 'myOp',
809+
parameters: [
810+
{
811+
name: 'query',
812+
in: 'query',
813+
schema: {
814+
type: 'array',
815+
},
816+
explode: false,
817+
},
818+
{
819+
name: 'FooHeader',
820+
in: 'header',
821+
schema: {
822+
type: 'array',
823+
},
824+
},
825+
{
826+
name: 'pathPartial',
827+
in: 'path',
828+
schema: {
829+
type: 'array',
830+
},
831+
},
832+
{
833+
name: 'myCookie',
834+
in: 'cookie',
835+
schema: {
836+
type: 'array',
837+
},
838+
},
839+
],
840+
},
841+
},
842+
},
843+
},
844+
operationId: 'myOp',
845+
parameters: {
846+
pathPartial: [undefined, undefined, undefined],
847+
query: [undefined, undefined, undefined],
848+
FooHeader: [undefined, undefined, undefined],
849+
myCookie: [undefined, undefined, undefined],
850+
},
851+
});
852+
853+
expect(req).toEqual({
854+
method: 'POST',
855+
url: `/,,?query=,,`,
856+
credentials: 'same-origin',
857+
headers: {
858+
FooHeader: ',,',
859+
Cookie: 'myCookie=,,',
860+
},
861+
});
862+
});
863+
667864
it('should encode arrays of arrays and objects', () => {
668865
const req = buildRequest({
669866
spec: {

0 commit comments

Comments
 (0)