From a5eb3dc0c302f8dbe095ee61ad2569d918deb524 Mon Sep 17 00:00:00 2001 From: Tim Lai Date: Fri, 22 Jan 2021 16:55:37 -0800 Subject: [PATCH] fix(buildUrl): relative url is invalid URL (OAS3) or non-url (OAS2) (#6864) * OAS3: relative url when no servers defined AND specUrl is invalid Url * OAS2: specUrl is non-url string --- src/core/utils/url.js | 18 +++++++++++------- test/unit/core/utils.js | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/core/utils/url.js b/src/core/utils/url.js index 2f063445..e7237d32 100644 --- a/src/core/utils/url.js +++ b/src/core/utils/url.js @@ -3,21 +3,25 @@ export function isAbsoluteUrl(url) { } export function addProtocol(url) { - if(!url.match(/^\/\//i)) return url // Checks if protocol is missing e.g. //example.com + if (!url.match(/^\/\//i)) return url // Checks if protocol is missing e.g. //example.com + return `${window.location.protocol}${url}` } export function buildBaseUrl(selectedServer, specUrl) { - if(!selectedServer) return specUrl - if(isAbsoluteUrl(selectedServer)) return addProtocol(selectedServer) - + if (!selectedServer) return specUrl + if (isAbsoluteUrl(selectedServer)) return addProtocol(selectedServer) + return new URL(selectedServer, specUrl).href } -export function buildUrl(url, specUrl, { selectedServer="" } = {}) { - if(!url) return - if(isAbsoluteUrl(url)) return url +export function buildUrl(url, specUrl, { selectedServer="" } = {}) { + if (!url) return + if (isAbsoluteUrl(url)) return url const baseUrl = buildBaseUrl(selectedServer, specUrl) + if (!isAbsoluteUrl(baseUrl)) { + return new URL(url, window.location.href).href + } return new URL(url, baseUrl).href } diff --git a/test/unit/core/utils.js b/test/unit/core/utils.js index fa3704fe..91e1b9ca 100644 --- a/test/unit/core/utils.js +++ b/test/unit/core/utils.js @@ -39,6 +39,7 @@ import { } from "core/utils/url" import win from "core/window" +import { afterAll, beforeAll, expect, jest } from "@jest/globals" describe("utils", () => { @@ -1389,6 +1390,17 @@ describe("utils", () => { }) describe("buildUrl", () => { + const { location } = window + beforeAll(() => { + delete window.location + window.location = { + href: "http://localhost/", + } + }) + afterAll(() => { + window.location = location + }) + const specUrl = "https://petstore.swagger.io/v2/swagger.json" const noUrl = "" @@ -1401,6 +1413,9 @@ describe("utils", () => { const serverUrlRelativeToBase = "server-example/base-path/path" const serverUrlRelativeToHost = "/server-example/base-path/path" + const specUrlAsInvalidUrl = "./examples/test.yaml" + const specUrlOas2NonUrlString = "an allowed OAS2 TermsOfService description string" + it("build no url", () => { expect(buildUrl(noUrl, specUrl, { selectedServer: absoluteServerUrl })).toBe(undefined) expect(buildUrl(noUrl, specUrl, { selectedServer: serverUrlRelativeToBase })).toBe(undefined) @@ -1432,6 +1447,14 @@ describe("utils", () => { expect(buildUrl(urlRelativeToBase, specUrl, { selectedServer: serverUrlRelativeToHost })).toBe("https://petstore.swagger.io/server-example/base-path/relative-url/base-path/path") expect(buildUrl(urlRelativeToHost, specUrl, { selectedServer: serverUrlRelativeToHost })).toBe("https://petstore.swagger.io/relative-url/base-path/path") }) + + it("build relative url when no servers defined AND specUrl is invalid Url", () => { + expect(buildUrl(urlRelativeToHost, specUrlAsInvalidUrl, { selectedServer: noServerSelected })).toBe("http://localhost/relative-url/base-path/path") + }) + + it("build relative url when no servers defined AND specUrl is OAS2 non-url string", () => { + expect(buildUrl(urlRelativeToHost, specUrlOas2NonUrlString, { selectedServer: noServerSelected })).toBe("http://localhost/relative-url/base-path/path") + }) }) describe("requiresValidationURL", () => {