Skip to content

Commit

Permalink
fix: handle Oas with a server URL that has no host or domain (#437)
Browse files Browse the repository at this point in the history
* fix: handle Oas spec with a server URL that has no host or domain

* chore: update package-lock
  • Loading branch information
Ilias Tsangaris authored May 19, 2021
1 parent 623d1be commit 6e3aca1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
4 changes: 4 additions & 0 deletions __tests__/tooling/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ describe('#url([selected])', () => {
expect(new Oas({ servers: [{ url: 'https://example.com' }] }).url(10)).toBe('https://example.com');
});

it('should make example.com the origin if none is present', () => {
expect(new Oas({ servers: [{ url: '/api/v3' }] }).url()).toBe('https://example.com/api/v3');
});

describe('server variables', () => {
const oas = new Oas({
servers: [
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion tooling/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function stripTrailingSlash(url) {
}

function normalizedUrl(oas, selected) {
const exampleDotCom = 'https://example.com';
let url;
try {
url = oas.servers[selected].url;
Expand All @@ -38,8 +39,16 @@ function normalizedUrl(oas, selected) {

// Stripping the '/' off the end
url = stripTrailingSlash(url);

// RM-1044 Check if the URL is just a path a missing an origin, for example `/api/v3`
// If so, then make example.com the origin to avoid it becoming something invalid like https:///api/v3
if (url.startsWith('/') && !url.startsWith('//')) {
const urlWithOrigin = new URL(exampleDotCom);
urlWithOrigin.pathname = url;
url = urlWithOrigin.href;
}
} catch (e) {
url = 'https://example.com';
url = exampleDotCom;
}

return ensureProtocol(url);
Expand Down

0 comments on commit 6e3aca1

Please sign in to comment.