From 6e3aca15e4e7edf1e0381b8d35597272ec426e18 Mon Sep 17 00:00:00 2001 From: Ilias Tsangaris Date: Wed, 19 May 2021 14:45:24 -0400 Subject: [PATCH] fix: handle Oas with a server URL that has no host or domain (#437) * fix: handle Oas spec with a server URL that has no host or domain * chore: update package-lock --- __tests__/tooling/index.test.js | 4 ++++ package-lock.json | 4 ++-- tooling/index.js | 11 ++++++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/__tests__/tooling/index.test.js b/__tests__/tooling/index.test.js index 2132cdd7..71e10a19 100644 --- a/__tests__/tooling/index.test.js +++ b/__tests__/tooling/index.test.js @@ -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: [ diff --git a/package-lock.json b/package-lock.json index e0e68f3c..2fef94ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "oas", - "version": "10.7.4", + "version": "11.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "10.7.4", + "version": "11.0.0", "license": "MIT", "dependencies": { "@apidevtools/json-schema-ref-parser": "^9.0.6", diff --git a/tooling/index.js b/tooling/index.js index e563789a..3f0b2016 100644 --- a/tooling/index.js +++ b/tooling/index.js @@ -30,6 +30,7 @@ function stripTrailingSlash(url) { } function normalizedUrl(oas, selected) { + const exampleDotCom = 'https://example.com'; let url; try { url = oas.servers[selected].url; @@ -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);