fix: expand tags and operations predictably in multiple SwaggerUI instances (#9050)
Refs #6996 Co-authored-by: Vladimír Gorej <vladimir.gorej@smartbear.com>
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
import YAML, { JSON_SCHEMA } from "js-yaml"
|
import YAML, { JSON_SCHEMA } from "js-yaml"
|
||||||
import { Map } from "immutable"
|
import { Map as ImmutableMap } from "immutable"
|
||||||
import parseUrl from "url-parse"
|
import parseUrl from "url-parse"
|
||||||
import { serializeError } from "serialize-error"
|
import { serializeError } from "serialize-error"
|
||||||
import isString from "lodash/isString"
|
import isString from "lodash/isString"
|
||||||
import debounce from "lodash/debounce"
|
import debounce from "lodash/debounce"
|
||||||
import set from "lodash/set"
|
import set from "lodash/set"
|
||||||
import assocPath from "lodash/fp/assocPath"
|
import assocPath from "lodash/fp/assocPath"
|
||||||
|
import constant from "lodash/constant"
|
||||||
|
|
||||||
import { paramToValue, isEmptyValue } from "core/utils"
|
import { paramToValue, isEmptyValue } from "core/utils"
|
||||||
|
|
||||||
@@ -116,37 +117,48 @@ export const resolveSpec = (json, url) => ({specActions, specSelectors, errActio
|
|||||||
requestInterceptor,
|
requestInterceptor,
|
||||||
responseInterceptor
|
responseInterceptor
|
||||||
}).then( ({spec, errors}) => {
|
}).then( ({spec, errors}) => {
|
||||||
errActions.clear({
|
errActions.clear({
|
||||||
type: "thrown"
|
type: "thrown"
|
||||||
})
|
|
||||||
if(Array.isArray(errors) && errors.length > 0) {
|
|
||||||
let preparedErrors = errors
|
|
||||||
.map(err => {
|
|
||||||
console.error(err)
|
|
||||||
err.line = err.fullPath ? getLineNumberForPath(specStr, err.fullPath) : null
|
|
||||||
err.path = err.fullPath ? err.fullPath.join(".") : null
|
|
||||||
err.level = "error"
|
|
||||||
err.type = "thrown"
|
|
||||||
err.source = "resolver"
|
|
||||||
Object.defineProperty(err, "message", { enumerable: true, value: err.message })
|
|
||||||
return err
|
|
||||||
})
|
|
||||||
errActions.newThrownErrBatch(preparedErrors)
|
|
||||||
}
|
|
||||||
|
|
||||||
return specActions.updateResolved(spec)
|
|
||||||
})
|
})
|
||||||
|
if(Array.isArray(errors) && errors.length > 0) {
|
||||||
|
let preparedErrors = errors
|
||||||
|
.map(err => {
|
||||||
|
console.error(err)
|
||||||
|
err.line = err.fullPath ? getLineNumberForPath(specStr, err.fullPath) : null
|
||||||
|
err.path = err.fullPath ? err.fullPath.join(".") : null
|
||||||
|
err.level = "error"
|
||||||
|
err.type = "thrown"
|
||||||
|
err.source = "resolver"
|
||||||
|
Object.defineProperty(err, "message", { enumerable: true, value: err.message })
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
errActions.newThrownErrBatch(preparedErrors)
|
||||||
|
}
|
||||||
|
|
||||||
|
return specActions.updateResolved(spec)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
let requestBatch = []
|
let requestBatch = []
|
||||||
|
|
||||||
const debResolveSubtrees = debounce(async () => {
|
const debResolveSubtrees = debounce(() => {
|
||||||
const system = requestBatch.system // Just a reference to the "latest" system
|
const systemPartitionedBatches = requestBatch.reduce((acc, { path, system }) => {
|
||||||
|
if (!acc.has(system)) acc.set(system, [])
|
||||||
|
acc.get(system).push(path)
|
||||||
|
return acc
|
||||||
|
}, new Map())
|
||||||
|
|
||||||
if(!system) {
|
requestBatch = [] // clear stack
|
||||||
console.error("debResolveSubtrees: don't have a system to operate on, aborting.")
|
|
||||||
return
|
systemPartitionedBatches.forEach(async (systemRequestBatch, system) => {
|
||||||
}
|
if(!system) {
|
||||||
|
console.error("debResolveSubtrees: don't have a system to operate on, aborting.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if(!system.fn.resolveSubtree) {
|
||||||
|
console.error("Error: Swagger-Client did not provide a `resolveSubtree` method, doing nothing.")
|
||||||
|
return
|
||||||
|
}
|
||||||
const {
|
const {
|
||||||
errActions,
|
errActions,
|
||||||
errSelectors,
|
errSelectors,
|
||||||
@@ -158,113 +170,101 @@ const debResolveSubtrees = debounce(async () => {
|
|||||||
specSelectors,
|
specSelectors,
|
||||||
specActions,
|
specActions,
|
||||||
} = system
|
} = system
|
||||||
|
const getLineNumberForPath = AST.getLineNumberForPath ?? constant(undefined)
|
||||||
|
const specStr = specSelectors.specStr()
|
||||||
|
const {
|
||||||
|
modelPropertyMacro,
|
||||||
|
parameterMacro,
|
||||||
|
requestInterceptor,
|
||||||
|
responseInterceptor
|
||||||
|
} = system.getConfigs()
|
||||||
|
|
||||||
if(!resolveSubtree) {
|
try {
|
||||||
console.error("Error: Swagger-Client did not provide a `resolveSubtree` method, doing nothing.")
|
const batchResult = await systemRequestBatch.reduce(async (prev, path) => {
|
||||||
return
|
let { resultMap, specWithCurrentSubtrees } = await prev
|
||||||
}
|
const { errors, spec } = await resolveSubtree(specWithCurrentSubtrees, path, {
|
||||||
|
baseDoc: specSelectors.url(),
|
||||||
let getLineNumberForPath = AST.getLineNumberForPath ? AST.getLineNumberForPath : () => undefined
|
modelPropertyMacro,
|
||||||
|
parameterMacro,
|
||||||
const specStr = specSelectors.specStr()
|
requestInterceptor,
|
||||||
|
responseInterceptor
|
||||||
const {
|
|
||||||
modelPropertyMacro,
|
|
||||||
parameterMacro,
|
|
||||||
requestInterceptor,
|
|
||||||
responseInterceptor
|
|
||||||
} = system.getConfigs()
|
|
||||||
|
|
||||||
try {
|
|
||||||
var batchResult = await requestBatch.reduce(async (prev, path) => {
|
|
||||||
let { resultMap, specWithCurrentSubtrees } = await prev
|
|
||||||
const { errors, spec } = await resolveSubtree(specWithCurrentSubtrees, path, {
|
|
||||||
baseDoc: specSelectors.url(),
|
|
||||||
modelPropertyMacro,
|
|
||||||
parameterMacro,
|
|
||||||
requestInterceptor,
|
|
||||||
responseInterceptor
|
|
||||||
})
|
|
||||||
|
|
||||||
if(errSelectors.allErrors().size) {
|
|
||||||
errActions.clearBy(err => {
|
|
||||||
// keep if...
|
|
||||||
return err.get("type") !== "thrown" // it's not a thrown error
|
|
||||||
|| err.get("source") !== "resolver" // it's not a resolver error
|
|
||||||
|| !err.get("fullPath").every((key, i) => key === path[i] || path[i] === undefined) // it's not within the path we're resolving
|
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
if(Array.isArray(errors) && errors.length > 0) {
|
if(errSelectors.allErrors().size) {
|
||||||
let preparedErrors = errors
|
errActions.clearBy(err => {
|
||||||
.map(err => {
|
// keep if...
|
||||||
err.line = err.fullPath ? getLineNumberForPath(specStr, err.fullPath) : null
|
return err.get("type") !== "thrown" // it's not a thrown error
|
||||||
err.path = err.fullPath ? err.fullPath.join(".") : null
|
|| err.get("source") !== "resolver" // it's not a resolver error
|
||||||
err.level = "error"
|
|| !err.get("fullPath").every((key, i) => key === path[i] || path[i] === undefined) // it's not within the path we're resolving
|
||||||
err.type = "thrown"
|
|
||||||
err.source = "resolver"
|
|
||||||
Object.defineProperty(err, "message", { enumerable: true, value: err.message })
|
|
||||||
return err
|
|
||||||
})
|
})
|
||||||
errActions.newThrownErrBatch(preparedErrors)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (spec && specSelectors.isOAS3() && path[0] === "components" && path[1] === "securitySchemes") {
|
if(Array.isArray(errors) && errors.length > 0) {
|
||||||
// Resolve OIDC URLs if present
|
let preparedErrors = errors
|
||||||
await Promise.all(Object.values(spec)
|
.map(err => {
|
||||||
.filter((scheme) => scheme.type === "openIdConnect")
|
err.line = err.fullPath ? getLineNumberForPath(specStr, err.fullPath) : null
|
||||||
.map(async (oidcScheme) => {
|
err.path = err.fullPath ? err.fullPath.join(".") : null
|
||||||
const req = {
|
err.level = "error"
|
||||||
url: oidcScheme.openIdConnectUrl,
|
err.type = "thrown"
|
||||||
requestInterceptor: requestInterceptor,
|
err.source = "resolver"
|
||||||
responseInterceptor: responseInterceptor
|
Object.defineProperty(err, "message", { enumerable: true, value: err.message })
|
||||||
}
|
return err
|
||||||
try {
|
})
|
||||||
const res = await fetch(req)
|
errActions.newThrownErrBatch(preparedErrors)
|
||||||
if (res instanceof Error || res.status >= 400) {
|
}
|
||||||
console.error(res.statusText + " " + req.url)
|
|
||||||
} else {
|
if (spec && specSelectors.isOAS3() && path[0] === "components" && path[1] === "securitySchemes") {
|
||||||
oidcScheme.openIdConnectData = JSON.parse(res.text)
|
// Resolve OIDC URLs if present
|
||||||
|
await Promise.all(Object.values(spec)
|
||||||
|
.filter((scheme) => scheme.type === "openIdConnect")
|
||||||
|
.map(async (oidcScheme) => {
|
||||||
|
const req = {
|
||||||
|
url: oidcScheme.openIdConnectUrl,
|
||||||
|
requestInterceptor: requestInterceptor,
|
||||||
|
responseInterceptor: responseInterceptor
|
||||||
}
|
}
|
||||||
} catch (e) {
|
try {
|
||||||
console.error(e)
|
const res = await fetch(req)
|
||||||
}
|
if (res instanceof Error || res.status >= 400) {
|
||||||
}))
|
console.error(res.statusText + " " + req.url)
|
||||||
}
|
} else {
|
||||||
set(resultMap, path, spec)
|
oidcScheme.openIdConnectData = JSON.parse(res.text)
|
||||||
specWithCurrentSubtrees = assocPath(path, spec, specWithCurrentSubtrees)
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
set(resultMap, path, spec)
|
||||||
|
specWithCurrentSubtrees = assocPath(path, spec, specWithCurrentSubtrees)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
resultMap,
|
resultMap,
|
||||||
specWithCurrentSubtrees
|
specWithCurrentSubtrees
|
||||||
}
|
}
|
||||||
}, Promise.resolve({
|
}, Promise.resolve({
|
||||||
resultMap: (specSelectors.specResolvedSubtree([]) || Map()).toJS(),
|
resultMap: (specSelectors.specResolvedSubtree([]) || ImmutableMap()).toJS(),
|
||||||
specWithCurrentSubtrees: specSelectors.specJS()
|
specWithCurrentSubtrees: specSelectors.specJS()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
delete requestBatch.system
|
specActions.updateResolvedSubtree([], batchResult.resultMap)
|
||||||
requestBatch = [] // Clear stack
|
} catch(e) {
|
||||||
} catch(e) {
|
console.error(e)
|
||||||
console.error(e)
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
specActions.updateResolvedSubtree([], batchResult.resultMap)
|
|
||||||
}, 35)
|
}, 35)
|
||||||
|
|
||||||
export const requestResolvedSubtree = path => system => {
|
export const requestResolvedSubtree = path => system => {
|
||||||
// poor-man's array comparison
|
const isPathAlreadyBatched = requestBatch.find(({ path: batchedPath, system: batchedSystem }) => {
|
||||||
// if this ever inadequate, this should be rewritten to use Im.List
|
return batchedSystem === system && batchedPath.toString() === path.toString()
|
||||||
const isPathAlreadyBatched = requestBatch
|
})
|
||||||
.map(arr => arr.join("@@"))
|
|
||||||
.indexOf(path.join("@@")) > -1
|
|
||||||
|
|
||||||
if(isPathAlreadyBatched) {
|
if(isPathAlreadyBatched) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
requestBatch.push(path)
|
requestBatch.push({ path, system })
|
||||||
requestBatch.system = system
|
|
||||||
debResolveSubtrees()
|
debResolveSubtrees()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -294,7 +294,7 @@ export const invalidateResolvedSubtreeCache = () => {
|
|||||||
type: UPDATE_RESOLVED_SUBTREE,
|
type: UPDATE_RESOLVED_SUBTREE,
|
||||||
payload: {
|
payload: {
|
||||||
path: [],
|
path: [],
|
||||||
value: Map()
|
value: ImmutableMap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -432,7 +432,7 @@ export const executeRequest = (req) =>
|
|||||||
req.requestBody = requestBody
|
req.requestBody = requestBody
|
||||||
.map(
|
.map(
|
||||||
(val) => {
|
(val) => {
|
||||||
if (Map.isMap(val)) {
|
if (ImmutableMap.isMap(val)) {
|
||||||
return val.get("value")
|
return val.get("value")
|
||||||
}
|
}
|
||||||
return val
|
return val
|
||||||
@@ -440,8 +440,8 @@ export const executeRequest = (req) =>
|
|||||||
)
|
)
|
||||||
.filter(
|
.filter(
|
||||||
(value, key) => (Array.isArray(value)
|
(value, key) => (Array.isArray(value)
|
||||||
? value.length !== 0
|
? value.length !== 0
|
||||||
: !isEmptyValue(value)
|
: !isEmptyValue(value)
|
||||||
) || requestBodyInclusionSetting.get(key)
|
) || requestBodyInclusionSetting.get(key)
|
||||||
)
|
)
|
||||||
.toJS()
|
.toJS()
|
||||||
@@ -470,22 +470,22 @@ export const executeRequest = (req) =>
|
|||||||
|
|
||||||
|
|
||||||
return fn.execute(req)
|
return fn.execute(req)
|
||||||
.then( res => {
|
.then( res => {
|
||||||
res.duration = Date.now() - startTime
|
res.duration = Date.now() - startTime
|
||||||
specActions.setResponse(req.pathName, req.method, res)
|
specActions.setResponse(req.pathName, req.method, res)
|
||||||
} )
|
} )
|
||||||
.catch(
|
.catch(
|
||||||
err => {
|
err => {
|
||||||
// console.error(err)
|
// console.error(err)
|
||||||
if(err.message === "Failed to fetch") {
|
if(err.message === "Failed to fetch") {
|
||||||
err.name = ""
|
err.name = ""
|
||||||
err.message = "**Failed to fetch.** \n**Possible Reasons:** \n - CORS \n - Network Failure \n - URL scheme must be \"http\" or \"https\" for CORS request."
|
err.message = "**Failed to fetch.** \n**Possible Reasons:** \n - CORS \n - Network Failure \n - URL scheme must be \"http\" or \"https\" for CORS request."
|
||||||
|
}
|
||||||
|
specActions.setResponse(req.pathName, req.method, {
|
||||||
|
error: true, err: serializeError(err)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
specActions.setResponse(req.pathName, req.method, {
|
)
|
||||||
error: true, err: serializeError(err)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user