Skip to content

Commit

Permalink
fix: convert URL config values to proper types
Browse files Browse the repository at this point in the history
  • Loading branch information
glowcloud committed Apr 11, 2024
1 parent 2f1eb1d commit 981dfe0
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import DownloadUrlPlugin from "./plugins/download-url"
import SafeRenderPlugin from "./plugins/safe-render"

import { parseSearch } from "./utils"
import { convertConfigValues } from "./utils/convertConfigValues"
import win from "./window"

// eslint-disable-next-line no-undef
Expand Down Expand Up @@ -134,7 +135,7 @@ export default function SwaggerUI(opts) {
}
}

let queryConfig = opts.queryConfigEnabled ? parseSearch() : {}
let queryConfig = opts.queryConfigEnabled ? convertConfigValues(parseSearch()) : {}

const domNode = opts.domNode
delete opts.domNode
Expand Down
77 changes: 77 additions & 0 deletions src/core/utils/convertConfigValues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* @prettier
*/
const booleanConfigs = [
"deepLinking",
"displayOperationId",
"displayRequestDuration",
"persistAuthorization",
"requestSnippetsEnabled",
"showCommonExtensions",
"showExtensions",
"showMutatedRequest",
"syntaxHighlight.activated",
"tryItOutEnabled",
"withCredentials",
]
const numberConfigs = [
"defaultModelExpandDepth",
"defaultModelsExpandDepth",
"maxDisplayedTags",
]
const objectConfigs = ["syntaxHighlight", "requestSnippets"]
const arrayConfigs = ["request.curlOptions", "supportedSubmitMethods"]

const convertValue = (key, value) => {
const isBoolean = booleanConfigs.includes(key)
const isNumber = numberConfigs.includes(key)
const isObject = objectConfigs.includes(key)
const isArray = arrayConfigs.includes(key)

if (key === "validatorUrl") {
return value === "null" ? null : value
}

if (key === "filter") {
return value === "false" ? false : value
}

if (isBoolean) {
return value === "true" ? true : value === "false" ? false : value
}

if (isNumber) {
const parsedValue = parseInt(value)
return isNaN(parsedValue) ? value : parsedValue
}

if (isObject) {
if (key === "syntaxHighlight" && value === "false") return false
try {
const parsedValue = JSON.parse(value)
return typeof parsedValue === "object" && !Array.isArray(parsedValue)
? parsedValue
: value
} catch (e) {
return value
}
}

if (isArray) {
try {
const parsedValue = JSON.parse(value)
return Array.isArray(parsedValue) ? parsedValue : value
} catch (e) {
return value
}
}

return value
}

export const convertConfigValues = (config) => {
Object.entries(config).forEach(([key, value]) => {
config[key] = convertValue(key, value)
})
return config
}
11 changes: 1 addition & 10 deletions src/core/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,16 +619,7 @@ export const parseSearch = () => {
continue
}
i = params[i].split("=")

let value = i[1] ? decodeURIComponent(i[1]) : ""

if (value === "true") {
value = true
} else if (value === "false") {
value = false
}

map[decodeURIComponent(i[0])] = value
map[decodeURIComponent(i[0])] = (i[1] && decodeURIComponent(i[1])) || ""
}
}

Expand Down
59 changes: 54 additions & 5 deletions test/unit/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ import {
safeBuildUrl,
} from "core/utils/url"

import {
convertConfigValues
} from "core/utils/convertConfigValues"

import win from "core/window"
import { afterAll, beforeAll, expect, jest } from "@jest/globals"

Expand Down Expand Up @@ -1326,11 +1330,6 @@ describe("utils", () => {
win.location.search = "?foo=foo%20bar"
expect(parseSearch()).toEqual({foo: "foo bar"})
})

it("parses boolean values", () => {
win.location.search = "?foo=true&bar=false"
expect(parseSearch()).toEqual({foo: true, bar: false})
})
})

describe("serializing", () => {
Expand Down Expand Up @@ -1781,4 +1780,54 @@ describe("utils", () => {
expect(createCodeChallenge(codeVerifier)).toBe(expectedCodeChallenge)
})
})

describe("convertConfigValues", () => {
it("should convert stringified `true` and `false` values to boolean" , () => {
const config = { deepLinking: "true", tryItOutEnabled: "false" }

const expectedConfig = { deepLinking: true, tryItOutEnabled: false }

expect(convertConfigValues(config)).toStrictEqual(expectedConfig)
})

it("should convert stringified number values to number" , () => {
const config = { defaultModelExpandDepth: "5", defaultModelsExpandDepth: "-1" }

const expectedConfig = { defaultModelExpandDepth: 5, defaultModelsExpandDepth: -1 }

expect(convertConfigValues(config)).toStrictEqual(expectedConfig)
})

it("should convert stringified number values to number" , () => {
const config = { defaultModelExpandDepth: "5", defaultModelsExpandDepth: "-1" }

const expectedConfig = { defaultModelExpandDepth: 5, defaultModelsExpandDepth: -1 }

expect(convertConfigValues(config)).toStrictEqual(expectedConfig)
})

it("should convert stringified array values to arrays" , () => {
const config = { supportedSubmitMethods: '["get", "post"]' }

const expectedConfig = { supportedSubmitMethods: ["get", "post"] }

expect(convertConfigValues(config)).toStrictEqual(expectedConfig)
})

it("should convert stringified object values to objects" , () => {
const config = { syntaxHighlight: '{"theme":"monokai"}' }

const expectedConfig = { syntaxHighlight: { theme: "monokai" } }

expect(convertConfigValues(config)).toStrictEqual(expectedConfig)
})

it("should not convert string values" , () => {
const config = { defaultModelRendering: "model" }

const expectedConfig = { defaultModelRendering: "model" }

expect(convertConfigValues(config)).toStrictEqual(expectedConfig)
})
})
})

0 comments on commit 981dfe0

Please sign in to comment.