Skip to content

Commit 074eaa5

Browse files
authored
fix: prevent object inheritance mutations in recursive sampleXmlFromSchema calls (via #5034)
* fix: prevent object inheritance mutations in recursive sampleXmlFromSchema calls * fix unrelated test
1 parent 89868a8 commit 074eaa5

File tree

2 files changed

+59
-18
lines changed
  • src/core/plugins/samples
  • test/core/plugins/samples

2 files changed

+59
-18
lines changed

src/core/plugins/samples/fn.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { objectify, isFunc, normalizeArray, deeplyStripKey } from "core/utils"
22
import XML from "@kyleshockey/xml"
33
import memoizee from "memoizee"
4+
import deepAssign from "@kyleshockey/object-assign-deep"
45

56
const primitives = {
67
"string": () => "string",
@@ -120,7 +121,7 @@ export const inferSchema = (thing) => {
120121

121122

122123
export const sampleXmlFromSchema = (schema, config={}) => {
123-
let objectifySchema = objectify(schema)
124+
let objectifySchema = deepAssign({}, objectify(schema))
124125
let { type, properties, additionalProperties, items, example } = objectifySchema
125126
let { includeReadOnly, includeWriteOnly } = config
126127
let defaultValue = objectifySchema.default

test/core/plugins/samples/fn.js

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,12 @@ describe("sampleFromSchema", function() {
244244
format: "date-time"
245245
}
246246

247-
var expected = new Date().toISOString()
247+
// 0-20 chops off milliseconds
248+
// necessary because test latency can cause failures
249+
// it would be better to mock Date globally and expect a string - KS 11/18
250+
var expected = new Date().toISOString().substring(0, 20)
248251

249-
expect(sampleFromSchema(definition)).toEqual(expected)
252+
expect(sampleFromSchema(definition)).toInclude(expected)
250253
})
251254

252255
it("returns example value for date property", function() {
@@ -949,25 +952,62 @@ describe("createXMLExample", function () {
949952
expect(sut(definition)).toEqual(expected)
950953
})
951954

952-
it("returns array with example values with wrapped=true", function () {
953-
var expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<animal>1</animal>\n\t<animal>2</animal>\n</animals>"
954-
var definition = {
955-
type: "array",
956-
items: {
957-
type: "string",
955+
it("returns array with example values with wrapped=true", function () {
956+
var expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<animals>\n\t<animal>1</animal>\n\t<animal>2</animal>\n</animals>"
957+
var definition = {
958+
type: "array",
959+
items: {
960+
type: "string",
961+
xml: {
962+
name: "animal"
963+
}
964+
},
965+
"example": [ "1", "2" ],
958966
xml: {
959-
name: "animal"
967+
wrapped: true,
968+
name: "animals"
960969
}
961-
},
962-
"example": [ "1", "2" ],
963-
xml: {
964-
wrapped: true,
965-
name: "animals"
966970
}
967-
}
968971

969-
expect(sut(definition)).toEqual(expected)
970-
})
972+
expect(sut(definition)).toEqual(expected)
973+
})
974+
975+
it("returns array of objects with example values with wrapped=true", function () {
976+
var expected = `<?xml version="1.0" encoding="UTF-8"?>\n<users>\n\t<user>\n\t\t<id>1</id>\n\t\t<name>Arthur Dent</name>\n\t</user>\n\t<user>\n\t\t<id>2</id>\n\t\t<name>Ford Prefect</name>\n\t</user>\n</users>`
977+
var definition = {
978+
"type": "array",
979+
"items": {
980+
"type": "object",
981+
"properties": {
982+
"id": {
983+
"type": "integer"
984+
},
985+
"name": {
986+
"type": "string"
987+
}
988+
},
989+
"xml": {
990+
"name": "user"
991+
}
992+
},
993+
"xml": {
994+
"name": "users",
995+
"wrapped": true
996+
},
997+
"example": [
998+
{
999+
"id": 1,
1000+
"name": "Arthur Dent"
1001+
},
1002+
{
1003+
"id": 2,
1004+
"name": "Ford Prefect"
1005+
}
1006+
]
1007+
}
1008+
1009+
expect(sut(definition)).toEqual(expected)
1010+
})
9711011

9721012
})
9731013

0 commit comments

Comments
 (0)