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
This commit is contained in:
@@ -3,21 +3,25 @@ export function isAbsoluteUrl(url) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function addProtocol(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}`
|
return `${window.location.protocol}${url}`
|
||||||
}
|
}
|
||||||
|
|
||||||
export function buildBaseUrl(selectedServer, specUrl) {
|
export function buildBaseUrl(selectedServer, specUrl) {
|
||||||
if(!selectedServer) return specUrl
|
if (!selectedServer) return specUrl
|
||||||
if(isAbsoluteUrl(selectedServer)) return addProtocol(selectedServer)
|
if (isAbsoluteUrl(selectedServer)) return addProtocol(selectedServer)
|
||||||
|
|
||||||
return new URL(selectedServer, specUrl).href
|
return new URL(selectedServer, specUrl).href
|
||||||
}
|
}
|
||||||
|
|
||||||
export function buildUrl(url, specUrl, { selectedServer="" } = {}) {
|
export function buildUrl(url, specUrl, { selectedServer="" } = {}) {
|
||||||
if(!url) return
|
if (!url) return
|
||||||
if(isAbsoluteUrl(url)) return url
|
if (isAbsoluteUrl(url)) return url
|
||||||
|
|
||||||
const baseUrl = buildBaseUrl(selectedServer, specUrl)
|
const baseUrl = buildBaseUrl(selectedServer, specUrl)
|
||||||
|
if (!isAbsoluteUrl(baseUrl)) {
|
||||||
|
return new URL(url, window.location.href).href
|
||||||
|
}
|
||||||
return new URL(url, baseUrl).href
|
return new URL(url, baseUrl).href
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ import {
|
|||||||
} from "core/utils/url"
|
} from "core/utils/url"
|
||||||
|
|
||||||
import win from "core/window"
|
import win from "core/window"
|
||||||
|
import { afterAll, beforeAll, expect, jest } from "@jest/globals"
|
||||||
|
|
||||||
describe("utils", () => {
|
describe("utils", () => {
|
||||||
|
|
||||||
@@ -1389,6 +1390,17 @@ describe("utils", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe("buildUrl", () => {
|
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 specUrl = "https://petstore.swagger.io/v2/swagger.json"
|
||||||
|
|
||||||
const noUrl = ""
|
const noUrl = ""
|
||||||
@@ -1401,6 +1413,9 @@ describe("utils", () => {
|
|||||||
const serverUrlRelativeToBase = "server-example/base-path/path"
|
const serverUrlRelativeToBase = "server-example/base-path/path"
|
||||||
const serverUrlRelativeToHost = "/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", () => {
|
it("build no url", () => {
|
||||||
expect(buildUrl(noUrl, specUrl, { selectedServer: absoluteServerUrl })).toBe(undefined)
|
expect(buildUrl(noUrl, specUrl, { selectedServer: absoluteServerUrl })).toBe(undefined)
|
||||||
expect(buildUrl(noUrl, specUrl, { selectedServer: serverUrlRelativeToBase })).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(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")
|
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", () => {
|
describe("requiresValidationURL", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user