diff --git a/docs/usage/http-client-for-oas-operations.md b/docs/usage/http-client-for-oas-operations.md
index 10e1957bd..2baac4699 100644
--- a/docs/usage/http-client-for-oas-operations.md
+++ b/docs/usage/http-client-for-oas-operations.md
@@ -30,6 +30,7 @@ Property | Description
`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.
`serverVariableEncoder` | `Function=identity`. An encoder function that is run on a server variable before substituted to the URL template.
`contextUrl` | `String`. URL, e.g. `https://example.com`. Used in following situations:
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.
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.
+`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.
For all later references, we will always use following OpenAPI 3.0.0 definition when referring
to a `spec`.
diff --git a/src/execute/index.js b/src/execute/index.js
index 3ceb48300..2bbadac25 100755
--- a/src/execute/index.js
+++ b/src/execute/index.js
@@ -133,7 +133,7 @@ export function buildRequest(options) {
serverVariableEncoder,
} = options;
- let { parameters, parameterBuilders } = options;
+ let { parameters, parameterBuilders, baseURL } = options;
const specIsOAS3 = isOpenAPI3(spec);
if (!parameterBuilders) {
@@ -177,16 +177,18 @@ export function buildRequest(options) {
const { operation = {}, method, pathName } = operationRaw;
- const baseURL = baseUrl({
- spec,
- scheme,
- contextUrl,
- server,
- serverVariables,
- pathName,
- method,
- serverVariableEncoder,
- });
+ baseURL =
+ baseURL ??
+ baseUrl({
+ spec,
+ scheme,
+ contextUrl,
+ server,
+ serverVariables,
+ pathName,
+ method,
+ serverVariableEncoder,
+ });
req.url += baseURL;
diff --git a/test/execute/main.js b/test/execute/main.js
index d3e6eb0d3..c5b9ab0a0 100644
--- a/test/execute/main.js
+++ b/test/execute/main.js
@@ -63,6 +63,33 @@ describe('execute', () => {
});
});
+ test('should override the host and basePath if baseURL is provided in the options', () => {
+ // Given
+ const spec = {
+ host: 'foo.com:8081',
+ basePath: '/v1',
+ paths: {
+ '/': {
+ get: {
+ operationId: 'foo',
+ },
+ },
+ },
+ };
+
+ // When
+ const baseURL = 'https://exmpl.com/v1';
+ const req = buildRequest({ spec, operationId: 'foo', baseURL });
+
+ // Then
+ expect(req).toEqual({
+ url: `${baseURL}/`,
+ method: 'GET',
+ credentials: 'same-origin',
+ headers: {},
+ });
+ });
+
test('should include operation specifics', () => {
// Given
const spec = {
diff --git a/test/execute/openapi-3-1.js b/test/execute/openapi-3-1.js
index 47771cc5f..866cdcd94 100644
--- a/test/execute/openapi-3-1.js
+++ b/test/execute/openapi-3-1.js
@@ -189,6 +189,28 @@ describe('given OpenAPI 3.1.0 definition', () => {
method: 'GET',
});
});
+
+ test('should build a request using provided baseURL in options', () => {
+ const baseURL = 'https://exmpl.com/v1';
+ const request = SwaggerClient.buildRequest({
+ spec,
+ operationId: 'getUserList',
+ parameters: { q: 'search string' },
+ securities: { authorized: { BearerAuth: '3492342948239482398' } },
+ responseContentType: 'application/json',
+ baseURL,
+ });
+
+ expect(request).toEqual({
+ url: `${baseURL}/users?q=search%20string`,
+ credentials: 'same-origin',
+ headers: {
+ accept: 'application/json',
+ Authorization: 'Bearer 3492342948239482398',
+ },
+ method: 'GET',
+ });
+ });
});
describe('given OpenAPI 3.1.0 definition with multi-value parameters', () => {