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:
kyle
2018-03-30 18:02:32 -07:00
committed by GitHub
parent 762a32b59b
commit fd8274b353
5 changed files with 214 additions and 46 deletions

View File

@@ -100,6 +100,92 @@ describe("sampleFromSchema", function() {
expect(sampleFromSchema(definition, { includeWriteOnly: true })).toEqual(expected)
})
it("returns object without any $$ref fields at the root schema level", function () {
var definition = {
type: "object",
properties: {
message: {
type: "string"
}
},
example: {
value: {
message: "Hello, World!"
},
$$ref: "#/components/examples/WelcomeExample"
},
$$ref: "#/components/schemas/Welcome"
}
var expected = {
"value": {
"message": "Hello, World!"
}
}
expect(sampleFromSchema(definition, { includeWriteOnly: true })).toEqual(expected)
})
it("returns object without any $$ref fields at nested schema levels", function () {
var definition = {
type: "object",
properties: {
message: {
type: "string"
}
},
example: {
a: {
value: {
message: "Hello, World!"
},
$$ref: "#/components/examples/WelcomeExample"
}
},
$$ref: "#/components/schemas/Welcome"
}
var expected = {
a: {
"value": {
"message": "Hello, World!"
}
}
}
expect(sampleFromSchema(definition, { includeWriteOnly: true })).toEqual(expected)
})
it("returns object with any $$ref fields that appear to be user-created", function () {
var definition = {
type: "object",
properties: {
message: {
type: "string"
}
},
example: {
$$ref: {
value: {
message: "Hello, World!"
},
$$ref: "#/components/examples/WelcomeExample"
}
},
$$ref: "#/components/schemas/Welcome"
}
var expected = {
$$ref: {
"value": {
"message": "Hello, World!"
}
}
}
expect(sampleFromSchema(definition, { includeWriteOnly: true })).toEqual(expected)
})
describe("for array type", function() {
it("returns array with sample of array type", function() {
var definition = {
@@ -108,12 +194,12 @@ describe("sampleFromSchema", function() {
type: "integer"
}
}
var expected = [ 0 ]
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("returns array of examples for array that has example", function() {
var definition = {
type: "array",
@@ -122,9 +208,9 @@ describe("sampleFromSchema", function() {
},
example: "dog"
}
var expected = [ "dog" ]
expect(sampleFromSchema(definition)).toEqual(expected)
})
@@ -132,16 +218,16 @@ describe("sampleFromSchema", function() {
var definition = {
type: "array",
items: {
type: "string",
type: "string",
},
example: [ "dog", "cat" ]
}
var expected = [ "dog", "cat" ]
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("returns array of samples for oneOf type", function() {
var definition = {
type: "array",
@@ -154,9 +240,9 @@ describe("sampleFromSchema", function() {
]
}
}
var expected = [ 0 ]
expect(sampleFromSchema(definition)).toEqual(expected)
})
@@ -175,9 +261,9 @@ describe("sampleFromSchema", function() {
]
}
}
var expected = [ "string", 0 ]
expect(sampleFromSchema(definition)).toEqual(expected)
})
@@ -198,12 +284,12 @@ describe("sampleFromSchema", function() {
]
}
}
var expected = [ "dog", 1 ]
expect(sampleFromSchema(definition)).toEqual(expected)
})
it("returns array of samples for anyOf type", function() {
var definition = {
type: "array",
@@ -216,9 +302,9 @@ describe("sampleFromSchema", function() {
]
}
}
var expected = [ 0 ]
expect(sampleFromSchema(definition)).toEqual(expected)
})
@@ -237,9 +323,9 @@ describe("sampleFromSchema", function() {
]
}
}
var expected = [ "string", 0 ]
expect(sampleFromSchema(definition)).toEqual(expected)
})
@@ -260,9 +346,9 @@ describe("sampleFromSchema", function() {
]
}
}
var expected = [ "dog", 1 ]
expect(sampleFromSchema(definition)).toEqual(expected)
})
})

View File

@@ -21,7 +21,8 @@ import {
createDeepLinkPath,
escapeDeepLinkPath,
sanitizeUrl,
extractFileNameFromContentDispositionHeader
extractFileNameFromContentDispositionHeader,
deeplyStripKey
} from "core/utils"
import win from "core/window"
@@ -942,6 +943,58 @@ describe("utils", function() {
})
})
describe("deeplyStripKey", function() {
it("should filter out a specified key", function() {
const input = {
$$ref: "#/this/is/my/ref",
a: {
$$ref: "#/this/is/my/other/ref",
value: 12345
}
}
const result = deeplyStripKey(input, "$$ref")
expect(result).toEqual({
a: {
value: 12345
}
})
})
it("should filter out a specified key by predicate", function() {
const input = {
$$ref: "#/this/is/my/ref",
a: {
$$ref: "#/keep/this/one",
value: 12345
}
}
const result = deeplyStripKey(input, "$$ref", (v) => v !== "#/keep/this/one")
expect(result).toEqual({
a: {
value: 12345,
$$ref: "#/keep/this/one"
}
})
})
it("should only call the predicate when the key matches", function() {
const input = {
$$ref: "#/this/is/my/ref",
a: {
$$ref: "#/this/is/my/other/ref",
value: 12345
}
}
let count = 0
const result = deeplyStripKey(input, "$$ref", () => {
count++
return true
})
expect(count).toEqual(2)
})
})
describe("parse and serialize search", function() {
afterEach(function() {
win.location.search = ""