Skip to content

Commit 1e3d882

Browse files
sauravazadsauravazadibmchar0n
authored
feat(execute): add support for baseURL override (#3538)
Refs #3537 Co-authored-by: Saurav Azad <sauravazad@in.ibm.com> Co-authored-by: Vladimír Gorej <vladimir.gorej@gmail.com>
1 parent f4068b6 commit 1e3d882

File tree

4 files changed

+63
-11
lines changed

4 files changed

+63
-11
lines changed

docs/usage/http-client-for-oas-operations.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Property | Description
3030
`server` | `String`. URL (`https://example.com`) or relative URI Reference (`/path/subpath`). Must match with of the defined `Server Objects`. If matched, it will be prepended to every requested path.
3131
`serverVariableEncoder` | `Function=identity`. An encoder function that is run on a server variable before substituted to the URL template.
3232
`contextUrl` | `String`. URL, e.g. `https://example.com`. Used in following situations: <br /><br />If `server` option is not matched and there is no `Server Object` defined in the definition, this URL will be prepended to every requested path.<br /><br />If matched `Server Object` is defined as relative URI Reference its `url` fixed field is resolved against `contenxtUrl`. Resolved URL will be prepended to every requested path.
33+
`baseURL` | `String`. URL (`https://example.com`) . Takes precedence over server and any defined servers in the Spec. It will be prepended to every requested path.
3334

3435
For all later references, we will always use following OpenAPI 3.0.0 definition when referring
3536
to a `spec`.

src/execute/index.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export function buildRequest(options) {
133133
serverVariableEncoder,
134134
} = options;
135135

136-
let { parameters, parameterBuilders } = options;
136+
let { parameters, parameterBuilders, baseURL } = options;
137137

138138
const specIsOAS3 = isOpenAPI3(spec);
139139
if (!parameterBuilders) {
@@ -177,16 +177,18 @@ export function buildRequest(options) {
177177

178178
const { operation = {}, method, pathName } = operationRaw;
179179

180-
const baseURL = baseUrl({
181-
spec,
182-
scheme,
183-
contextUrl,
184-
server,
185-
serverVariables,
186-
pathName,
187-
method,
188-
serverVariableEncoder,
189-
});
180+
baseURL =
181+
baseURL ??
182+
baseUrl({
183+
spec,
184+
scheme,
185+
contextUrl,
186+
server,
187+
serverVariables,
188+
pathName,
189+
method,
190+
serverVariableEncoder,
191+
});
190192

191193
req.url += baseURL;
192194

test/execute/main.js

+27
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,33 @@ describe('execute', () => {
6363
});
6464
});
6565

66+
test('should override the host and basePath if baseURL is provided in the options', () => {
67+
// Given
68+
const spec = {
69+
host: 'foo.com:8081',
70+
basePath: '/v1',
71+
paths: {
72+
'/': {
73+
get: {
74+
operationId: 'foo',
75+
},
76+
},
77+
},
78+
};
79+
80+
// When
81+
const baseURL = 'https://exmpl.com/v1';
82+
const req = buildRequest({ spec, operationId: 'foo', baseURL });
83+
84+
// Then
85+
expect(req).toEqual({
86+
url: `${baseURL}/`,
87+
method: 'GET',
88+
credentials: 'same-origin',
89+
headers: {},
90+
});
91+
});
92+
6693
test('should include operation specifics', () => {
6794
// Given
6895
const spec = {

test/execute/openapi-3-1.js

+22
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,28 @@ describe('given OpenAPI 3.1.0 definition', () => {
189189
method: 'GET',
190190
});
191191
});
192+
193+
test('should build a request using provided baseURL in options', () => {
194+
const baseURL = 'https://exmpl.com/v1';
195+
const request = SwaggerClient.buildRequest({
196+
spec,
197+
operationId: 'getUserList',
198+
parameters: { q: 'search string' },
199+
securities: { authorized: { BearerAuth: '3492342948239482398' } },
200+
responseContentType: 'application/json',
201+
baseURL,
202+
});
203+
204+
expect(request).toEqual({
205+
url: `${baseURL}/users?q=search%20string`,
206+
credentials: 'same-origin',
207+
headers: {
208+
accept: 'application/json',
209+
Authorization: 'Bearer 3492342948239482398',
210+
},
211+
method: 'GET',
212+
});
213+
});
192214
});
193215

194216
describe('given OpenAPI 3.1.0 definition with multi-value parameters', () => {

0 commit comments

Comments
 (0)