Skip to content

Commit 05da7a3

Browse files
committed
Add allowReserved tests
1 parent d55bd1b commit 05da7a3

File tree

3 files changed

+168
-6
lines changed

3 files changed

+168
-6
lines changed

src/execute/oas3/parameter-builders.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function path({req, value, parameter}) {
1414
value,
1515
style: style || 'simple',
1616
explode: explode || false,
17+
escape: !parameter.allowReserved,
1718
})
1819

1920
req.url = req.url.replace(`{${name}}`, styledValue)
@@ -41,7 +42,8 @@ function query({req, value, parameter}) {
4142
value: stylize({
4243
key: k,
4344
value: v,
44-
style: 'deepObject'
45+
style: 'deepObject',
46+
escape: !parameter.allowReserved,
4547
}),
4648
skipEncoding: true
4749
}
@@ -63,7 +65,8 @@ function query({req, value, parameter}) {
6365
value: stylize({
6466
key: k,
6567
value: v,
66-
style: parameter.style || 'form'
68+
style: parameter.style || 'form',
69+
escape: !parameter.allowReserved,
6770
})
6871
}
6972
})
@@ -73,7 +76,8 @@ function query({req, value, parameter}) {
7376
key: parameter.name,
7477
value,
7578
style: parameter.style || 'form',
76-
explode: typeof parameter.explode === 'undefined' ? true : parameter.explode
79+
explode: typeof parameter.explode === 'undefined' ? true : parameter.explode,
80+
escape: !parameter.allowReserved,
7781
}),
7882
skipEncoding: true
7983
}
@@ -93,7 +97,8 @@ function header({req, parameter, value}) {
9397
key: parameter.name,
9498
value,
9599
style: parameter.style || 'simple',
96-
explode: typeof parameter.explode === 'undefined' ? false : parameter.explode
100+
explode: typeof parameter.explode === 'undefined' ? false : parameter.explode,
101+
escape: !parameter.allowReserved,
97102
})
98103
}
99104
}

src/execute/oas3/style-serializer.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default function (config) {
1313

1414
const escapeFn = str => encodeURIComponent(str)
1515

16-
function encodeArray({key, value, style, explode, parameter, escape = true}) {
16+
function encodeArray({key, value, style, explode, escape}) {
1717
if (style === 'simple') {
1818
return value.join(',')
1919
}
@@ -44,7 +44,8 @@ function encodeArray({key, value, style, explode, parameter, escape = true}) {
4444

4545
if (style === 'pipeDelimited') {
4646
const after = explode ? `${key}=` : ''
47-
return value.join(`${escapeFn('|')}${after}`)
47+
const separator = escape ? escapeFn('|') : '|'
48+
return value.join(`${separator}${after}`)
4849
}
4950
}
5051

test/oas3/execute/style-explode.js

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,45 @@ describe('buildRequest w/ `style` & `explode` - OpenAPI Specification 3.0', func
10621062
})
10631063
})
10641064

1065+
it('should build a query parameter in form/no-explode format with allowReserved', function () {
1066+
// Given
1067+
const spec = {
1068+
openapi: '3.0.0',
1069+
paths: {
1070+
'/users': {
1071+
get: {
1072+
operationId: 'myOperation',
1073+
parameters: [
1074+
{
1075+
name: 'id',
1076+
in: 'query',
1077+
style: 'form',
1078+
explode: false,
1079+
allowReserved: true
1080+
}
1081+
]
1082+
}
1083+
}
1084+
}
1085+
}
1086+
1087+
// when
1088+
const req = buildRequest({
1089+
spec,
1090+
operationId: 'myOperation',
1091+
parameters: {
1092+
id: VALUE
1093+
}
1094+
})
1095+
1096+
expect(req).toEqual({
1097+
method: 'GET',
1098+
url: '/users?id=3,4,5',
1099+
credentials: 'same-origin',
1100+
headers: {},
1101+
})
1102+
})
1103+
10651104
it('should build a query parameter in space-delimited/explode format', function () {
10661105
// Given
10671106
const spec = {
@@ -1100,6 +1139,45 @@ describe('buildRequest w/ `style` & `explode` - OpenAPI Specification 3.0', func
11001139
})
11011140
})
11021141

1142+
it('should build a query parameter in space-delimited/explode format with allowReserved', function () {
1143+
// Given
1144+
const spec = {
1145+
openapi: '3.0.0',
1146+
paths: {
1147+
'/users': {
1148+
get: {
1149+
operationId: 'myOperation',
1150+
parameters: [
1151+
{
1152+
name: 'id',
1153+
in: 'query',
1154+
style: 'spaceDelimited',
1155+
explode: true
1156+
}
1157+
]
1158+
}
1159+
}
1160+
}
1161+
}
1162+
1163+
// when
1164+
const req = buildRequest({
1165+
spec,
1166+
operationId: 'myOperation',
1167+
parameters: {
1168+
id: VALUE
1169+
}
1170+
})
1171+
// whitespace is _not_ an RFC3986 reserved character,
1172+
// so it should still be escaped!
1173+
expect(req).toEqual({
1174+
method: 'GET',
1175+
url: '/users?id=3%20id=4%20id=5',
1176+
credentials: 'same-origin',
1177+
headers: {},
1178+
})
1179+
})
1180+
11031181
it('should build a query parameter in space-delimited/no-explode format', function () {
11041182
// Given
11051183
const spec = {
@@ -1176,6 +1254,45 @@ describe('buildRequest w/ `style` & `explode` - OpenAPI Specification 3.0', func
11761254
})
11771255
})
11781256

1257+
it('should build a query parameter in pipe-delimited/explode format with allowReserved', function () {
1258+
// Given
1259+
const spec = {
1260+
openapi: '3.0.0',
1261+
paths: {
1262+
'/users': {
1263+
get: {
1264+
operationId: 'myOperation',
1265+
parameters: [
1266+
{
1267+
name: 'id',
1268+
in: 'query',
1269+
style: 'pipeDelimited',
1270+
explode: true,
1271+
allowReserved: true
1272+
}
1273+
]
1274+
}
1275+
}
1276+
}
1277+
}
1278+
1279+
// when
1280+
const req = buildRequest({
1281+
spec,
1282+
operationId: 'myOperation',
1283+
parameters: {
1284+
id: VALUE
1285+
}
1286+
})
1287+
1288+
expect(req).toEqual({
1289+
method: 'GET',
1290+
url: '/users?id=3|id=4|id=5',
1291+
credentials: 'same-origin',
1292+
headers: {},
1293+
})
1294+
})
1295+
11791296
it('should build a query parameter in pipe-delimited/no-explode format', function () {
11801297
// Given
11811298
const spec = {
@@ -1213,6 +1330,45 @@ describe('buildRequest w/ `style` & `explode` - OpenAPI Specification 3.0', func
12131330
headers: {},
12141331
})
12151332
})
1333+
1334+
it('should build a query parameter in pipe-delimited/no-explode format with allowReserved', function () {
1335+
// Given
1336+
const spec = {
1337+
openapi: '3.0.0',
1338+
paths: {
1339+
'/users': {
1340+
get: {
1341+
operationId: 'myOperation',
1342+
parameters: [
1343+
{
1344+
name: 'id',
1345+
in: 'query',
1346+
style: 'pipeDelimited',
1347+
explode: false,
1348+
allowReserved: true
1349+
}
1350+
]
1351+
}
1352+
}
1353+
}
1354+
}
1355+
1356+
// when
1357+
const req = buildRequest({
1358+
spec,
1359+
operationId: 'myOperation',
1360+
parameters: {
1361+
id: VALUE
1362+
}
1363+
})
1364+
1365+
expect(req).toEqual({
1366+
method: 'GET',
1367+
url: '/users?id=3|4|5',
1368+
credentials: 'same-origin',
1369+
headers: {},
1370+
})
1371+
})
12161372
})
12171373
describe('object values', function () {
12181374
const VALUE = {

0 commit comments

Comments
 (0)