Filter $$ref from examples (#4392)
* fix(dev-server): don't open localhost in a browser * tests: refactor model-example enzyme tests to be more isolated * tests: add failing sampleFromSchema tests for $$ref keys * tests: add additional test for user-created $$ref values * fix: create deeplyStripKey; use it to filter $$refs out of examples * tests: add cases for deeplyStripKey
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { objectify, isFunc, normalizeArray } from "core/utils"
|
||||
import { objectify, isFunc, normalizeArray, deeplyStripKey } from "core/utils"
|
||||
import XML from "xml"
|
||||
import memoizee from "memoizee"
|
||||
|
||||
@@ -29,13 +29,14 @@ export const sampleFromSchema = (schema, config={}) => {
|
||||
let { type, example, properties, additionalProperties, items } = objectify(schema)
|
||||
let { includeReadOnly, includeWriteOnly } = config
|
||||
|
||||
if(example && example.$$ref) {
|
||||
delete example.$$ref
|
||||
if(example !== undefined) {
|
||||
return deeplyStripKey(example, "$$ref", (val) => {
|
||||
// do a couple of quick sanity tests to ensure the value
|
||||
// looks like a $$ref that swagger-client generates.
|
||||
return typeof val === "string" && val.indexOf("#") > -1
|
||||
})
|
||||
}
|
||||
|
||||
if(example !== undefined)
|
||||
return example
|
||||
|
||||
if(!type) {
|
||||
if(properties) {
|
||||
type = "object"
|
||||
|
||||
@@ -712,3 +712,25 @@ export const createDeepLinkPath = (str) => typeof str == "string" || str instanc
|
||||
export const escapeDeepLinkPath = (str) => cssEscape( createDeepLinkPath(str) )
|
||||
|
||||
export const getExtensions = (defObj) => defObj.filter((v, k) => /^x-/.test(k))
|
||||
|
||||
// Deeply strips a specific key from an object.
|
||||
//
|
||||
// `predicate` can be used to discriminate the stripping further,
|
||||
// by preserving the key's place in the object based on its value.
|
||||
export function deeplyStripKey(input, keyToStrip, predicate = () => true) {
|
||||
if(typeof input !== "object" || Array.isArray(input) || !keyToStrip) {
|
||||
return input
|
||||
}
|
||||
|
||||
const obj = Object.assign({}, input)
|
||||
|
||||
Object.keys(obj).forEach(k => {
|
||||
if(k === keyToStrip && predicate(obj[k], k)) {
|
||||
delete obj[k]
|
||||
return
|
||||
}
|
||||
obj[k] = deeplyStripKey(obj[k], keyToStrip, predicate)
|
||||
})
|
||||
|
||||
return obj
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user