Skip to content

Commit 5b41033

Browse files
committed
More WIP
1 parent de644f9 commit 5b41033

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

src/execute/oas3/parameter-builders.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ function query({req, value, parameter}) {
3434
key: parameter.name,
3535
value,
3636
style: parameter.style || 'form',
37-
explode: parameter.explode || true
38-
})
37+
explode: typeof parameter.explode === 'undefined' ? true : parameter.explode
38+
}),
39+
skipEncoding: true
3940
}
4041
}
4142
else if (parameter.allowEmptyValue) {

src/execute/oas3/style-serializer.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ export default function (config) {
1111
return encodePrimitive(config)
1212
}
1313

14-
function encodeArray({key, value, style, explode}) {
14+
const escape = str => encodeURIComponent(str)
15+
16+
function encodeArray({key, value, style, explode, parameter}) {
1517
if (style === 'simple') {
1618
return value.join(',')
1719
}
20+
1821
if (style === 'label') {
1922
return `.${value.join('.')}`
2023
}
24+
2125
if (style === 'matrix') {
2226
return value.reduce((prev, curr) => {
2327
if (!prev || explode) {
@@ -26,8 +30,20 @@ function encodeArray({key, value, style, explode}) {
2630
return `${prev},${curr}`
2731
}, '')
2832
}
33+
2934
if (style === 'form') {
30-
return value.join(',')
35+
const after = explode ? `&${key}=` : escape(',')
36+
return value.join(after)
37+
}
38+
39+
if (style === 'spaceDelimited') {
40+
const after = explode ? `${key}=` : ''
41+
return value.join(`${escape(' ')}${after}`)
42+
}
43+
44+
if (style === 'pipeDelimited') {
45+
const after = explode ? `${key}=` : ''
46+
return value.join(`${escape('|')}${after}`)
3147
}
3248
}
3349

@@ -72,18 +88,30 @@ function encodeObject({key, value, style, explode}) {
7288
return `${prefix}${curr},${val}`
7389
}, '')
7490
}
91+
92+
if (style === 'form') {
93+
return valueKeys.reduce((prev, curr) => {
94+
const val = value[curr]
95+
const prefix = prev ? `${prev}&` : ''
96+
97+
return `${prefix}${curr}=${val}`
98+
}, '')
99+
}
75100
}
76101

77102
function encodePrimitive({key, value, style, explode}) {
78103
if (style === 'simple') {
79104
return value
80105
}
106+
81107
if (style === 'label') {
82108
return `.${value}`
83109
}
110+
84111
if (style === 'matrix') {
85112
return `;${key}=${value}`
86113
}
114+
87115
if (style === 'form') {
88116
return value
89117
}

src/http.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,10 @@ export function encodeFormOrQuery(data) {
180180
const encodedQuery = Object.keys(data).reduce((result, parameterName) => {
181181
const isObject = a => a && typeof a === 'object'
182182
const paramValue = data[parameterName]
183+
const skipEncoding = !!paramValue.skipEncoding
183184
const encodedParameterName = encodeURIComponent(parameterName)
184185
const notArray = isObject(paramValue) && !Array.isArray(paramValue)
185-
result[encodedParameterName] = formatValue(notArray ? paramValue : {value: paramValue})
186+
result[encodedParameterName] = formatValue(notArray ? paramValue : {value: paramValue}, skipEncoding)
186187
return result
187188
}, {})
188189
return qs.stringify(encodedQuery, {encode: false, indices: false}) || ''

test/oas3/execute/style-explode.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import expect, {createSpy, spyOn} from 'expect'
22
import xmock from 'xmock'
33
import path from 'path'
44
import fs from 'fs'
5+
import qs from 'querystring'
56
import jsYaml from 'js-yaml'
67

78
import {execute, buildRequest, baseUrl, applySecurities, self as stubs} from '../../../src/execute'
@@ -11,7 +12,7 @@ const petstoreSpec = jsYaml.safeLoad(fs.readFileSync(path.join('test', 'oas3', '
1112
// Supported shape... { spec, operationId, parameters, securities, fetch }
1213
// One can use operationId or pathItem + method
1314

14-
describe('buildRequest w/ `style` & `explode` - OpenAPI Specification 3.0', function () {
15+
describe.only('buildRequest w/ `style` & `explode` - OpenAPI Specification 3.0', function () {
1516
describe('path parameters', function () {
1617
describe('primitive values', function () {
1718
it('default: should build a path parameter in a simple/no-explode format', function () {
@@ -1055,7 +1056,7 @@ describe('buildRequest w/ `style` & `explode` - OpenAPI Specification 3.0', func
10551056

10561057
expect(req).toEqual({
10571058
method: 'GET',
1058-
url: '/users?id=3,4,5',
1059+
url: '/users?id=3%2C4%2C5',
10591060
credentials: 'same-origin',
10601061
headers: {},
10611062
})
@@ -1169,7 +1170,7 @@ describe('buildRequest w/ `style` & `explode` - OpenAPI Specification 3.0', func
11691170

11701171
expect(req).toEqual({
11711172
method: 'GET',
1172-
url: '/users?id=3|id=4|id=5',
1173+
url: '/users?id=3%7Cid=4%7Cid=5',
11731174
credentials: 'same-origin',
11741175
headers: {},
11751176
})
@@ -1207,13 +1208,13 @@ describe('buildRequest w/ `style` & `explode` - OpenAPI Specification 3.0', func
12071208

12081209
expect(req).toEqual({
12091210
method: 'GET',
1210-
url: '/users?id=3|4|5',
1211+
url: '/users?id=3%7C4%7C5',
12111212
credentials: 'same-origin',
12121213
headers: {},
12131214
})
12141215
})
12151216
})
1216-
describe.skip('object values', function () {
1217+
describe('object values', function () {
12171218
const VALUE = {
12181219
role: 'admin',
12191220
firstName: 'Alex'

0 commit comments

Comments
 (0)