Skip to content

Commit

Permalink
feat(execute): add support for baseURL override (#3538)
Browse files Browse the repository at this point in the history
Refs #3537

Co-authored-by: Saurav Azad <[email protected]>
Co-authored-by: Vladimír Gorej <[email protected]>
  • Loading branch information
3 people authored Nov 7, 2024
1 parent f4068b6 commit 1e3d882
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
1 change: 1 addition & 0 deletions docs/usage/http-client-for-oas-operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <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.
`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`.
Expand Down
24 changes: 13 additions & 11 deletions src/execute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down
27 changes: 27 additions & 0 deletions test/execute/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
22 changes: 22 additions & 0 deletions test/execute/openapi-3-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 1e3d882

Please sign in to comment.