Skip to content

Commit 267ba8f

Browse files
committed
fix(execute): fix multiple path parameters templating
Refs #3508
1 parent 7be6636 commit 267ba8f

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

src/execute/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export function buildRequest(options) {
172172

173173
const { operation = {}, method, pathName } = operationRaw;
174174

175-
req.url += baseUrl({
175+
const baseURL = baseUrl({
176176
spec,
177177
scheme,
178178
contextUrl,
@@ -182,6 +182,8 @@ export function buildRequest(options) {
182182
method,
183183
});
184184

185+
req.url += baseURL;
186+
185187
// Mostly for testing
186188
if (!operationId) {
187189
// Not removing req.cookies causes testing issues and would
@@ -266,7 +268,7 @@ export function buildRequest(options) {
266268
value,
267269
operation,
268270
spec,
269-
pathName,
271+
baseURL,
270272
});
271273
}
272274
});

src/execute/oas3/parameter-builders.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,25 @@ import { resolve as resolvePathTemplate } from 'openapi-path-templating';
33
import stylize, { encodeCharacters } from './style-serializer.js';
44
import serialize from './content-serializer.js';
55

6-
export function path({ req, value, parameter, pathName }) {
6+
export function path({ req, value, parameter, baseURL }) {
77
const { name, style, explode, content } = parameter;
88

99
if (value === undefined) return;
1010

11+
const pathname = req.url.replace(baseURL, '');
1112
let resolvedPathname;
1213

1314
if (content) {
1415
const effectiveMediaType = Object.keys(content)[0];
1516

1617
resolvedPathname = resolvePathTemplate(
17-
pathName,
18+
pathname,
1819
{ [name]: value },
1920
{ encoder: (val) => encodeCharacters(serialize(val, effectiveMediaType)) }
2021
);
2122
} else {
2223
resolvedPathname = resolvePathTemplate(
23-
pathName,
24+
pathname,
2425
{ [name]: value },
2526
{
2627
encoder: (val) =>
@@ -35,7 +36,7 @@ export function path({ req, value, parameter, pathName }) {
3536
);
3637
}
3738

38-
req.url = req.url.replace(pathName, resolvedPathname);
39+
req.url = baseURL + resolvedPathname;
3940
}
4041

4142
export function query({ req, value, parameter }) {

src/execute/swagger2/parameter-builders.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ function headerBuilder({ req, parameter, value }) {
5151
}
5252

5353
// Replace path paramters, with values ( ie: the URL )
54-
function pathBuilder({ req, value, parameter, pathName }) {
54+
function pathBuilder({ req, value, parameter, baseURL }) {
5555
if (value !== undefined) {
56-
const resolvedPathname = resolvePathTemplate(pathName, { [parameter.name]: value });
56+
const pathname = req.url.replace(baseURL, '');
57+
const resolvedPathname = resolvePathTemplate(pathname, { [parameter.name]: value });
5758

58-
req.url = req.url.replace(pathName, resolvedPathname);
59+
req.url = baseURL + resolvedPathname;
5960
}
6061
}
6162

test/execute/main.js

+47
Original file line numberDiff line numberDiff line change
@@ -1982,6 +1982,53 @@ describe('execute', () => {
19821982
});
19831983
});
19841984

1985+
test('should replace all path parameters with their values', () => {
1986+
const spec = {
1987+
host: 'swagger.io',
1988+
basePath: '/v1',
1989+
paths: {
1990+
'/{id}/orders/{order}/{itemId}': {
1991+
get: {
1992+
operationId: 'getMe',
1993+
parameters: [
1994+
{
1995+
in: 'path',
1996+
name: 'id',
1997+
type: 'number',
1998+
required: true,
1999+
},
2000+
{
2001+
in: 'path',
2002+
name: 'order',
2003+
type: 'number',
2004+
required: true,
2005+
},
2006+
{
2007+
in: 'path',
2008+
name: 'itemId',
2009+
type: 'number',
2010+
required: true,
2011+
},
2012+
],
2013+
},
2014+
},
2015+
},
2016+
};
2017+
2018+
const req = buildRequest({
2019+
spec,
2020+
operationId: 'getMe',
2021+
parameters: { id: '123', order: '456', itemId: '789' },
2022+
});
2023+
2024+
expect(req).toEqual({
2025+
url: 'http://swagger.io/v1/123/orders/456/789',
2026+
method: 'GET',
2027+
credentials: 'same-origin',
2028+
headers: {},
2029+
});
2030+
});
2031+
19852032
test('should not replace path parameters with undefined values', () => {
19862033
const spec = {
19872034
host: 'swagger.io',

0 commit comments

Comments
 (0)