Skip to content

Commit

Permalink
feat: Adding support to pass down resolved baseURL, to avoid repeated…
Browse files Browse the repository at this point in the history
… computation . Issue: swagger-api#3537
  • Loading branch information
sauravazadibm committed May 27, 2024
1 parent ee7fdd6 commit c1b791a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 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 @@ -29,6 +29,7 @@ Property | Description
`signal` | `AbortSignal=null`. AbortSignal object instance, which can be used to abort a request as desired.
`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.
`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
6 changes: 3 additions & 3 deletions src/execute/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ export function buildRequest(options) {
server,
serverVariables,
http,
signal,
signal
} = options;

let { parameters, parameterBuilders } = options;
let { parameters, parameterBuilders, baseURL } = options;

const specIsOAS3 = isOpenAPI3(spec);
if (!parameterBuilders) {
Expand Down Expand Up @@ -172,7 +172,7 @@ export function buildRequest(options) {

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

const baseURL = baseUrl({
baseURL = baseURL ?? baseUrl({
spec,
scheme,
contextUrl,
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 c1b791a

Please sign in to comment.