From 7c3982d31363801660a95c260b8d9ca008973de6 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Fri, 16 Nov 2018 14:36:27 +0100 Subject: [PATCH 1/2] fix: prevent object inheritance mutations in recursive sampleXmlFromSchema calls --- src/core/plugins/samples/fn.js | 3 +- test/core/plugins/samples/fn.js | 67 +++++++++++++++++++++++++-------- 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/src/core/plugins/samples/fn.js b/src/core/plugins/samples/fn.js index 83b59d193bd..401ba329141 100644 --- a/src/core/plugins/samples/fn.js +++ b/src/core/plugins/samples/fn.js @@ -1,6 +1,7 @@ import { objectify, isFunc, normalizeArray, deeplyStripKey } from "core/utils" import XML from "@kyleshockey/xml" import memoizee from "memoizee" +import deepAssign from "@kyleshockey/object-assign-deep" const primitives = { "string": () => "string", @@ -120,7 +121,7 @@ export const inferSchema = (thing) => { export const sampleXmlFromSchema = (schema, config={}) => { - let objectifySchema = objectify(schema) + let objectifySchema = deepAssign({}, objectify(schema)) let { type, properties, additionalProperties, items, example } = objectifySchema let { includeReadOnly, includeWriteOnly } = config let defaultValue = objectifySchema.default diff --git a/test/core/plugins/samples/fn.js b/test/core/plugins/samples/fn.js index 7cb51768d89..8a6d262abaa 100644 --- a/test/core/plugins/samples/fn.js +++ b/test/core/plugins/samples/fn.js @@ -949,25 +949,62 @@ describe("createXMLExample", function () { expect(sut(definition)).toEqual(expected) }) - it("returns array with example values with wrapped=true", function () { - var expected = "\n\n\t1\n\t2\n" - var definition = { - type: "array", - items: { - type: "string", + it("returns array with example values with wrapped=true", function () { + var expected = "\n\n\t1\n\t2\n" + var definition = { + type: "array", + items: { + type: "string", + xml: { + name: "animal" + } + }, + "example": [ "1", "2" ], xml: { - name: "animal" + wrapped: true, + name: "animals" } - }, - "example": [ "1", "2" ], - xml: { - wrapped: true, - name: "animals" } - } - expect(sut(definition)).toEqual(expected) - }) + expect(sut(definition)).toEqual(expected) + }) + + it("returns array of objects with example values with wrapped=true", function () { + var expected = `\n\n\t\n\t\t1\n\t\tArthur Dent\n\t\n\t\n\t\t2\n\t\tFord Prefect\n\t\n` + var definition = { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "name": { + "type": "string" + } + }, + "xml": { + "name": "user" + } + }, + "xml": { + "name": "users", + "wrapped": true + }, + "example": [ + { + "id": 1, + "name": "Arthur Dent" + }, + { + "id": 2, + "name": "Ford Prefect" + } + ] + } + + expect(sut(definition)).toEqual(expected) + }) }) From b73a3b739f7ed4558672cd98b8df8cb735383e95 Mon Sep 17 00:00:00 2001 From: Kyle Shockey Date: Fri, 16 Nov 2018 20:54:24 +0100 Subject: [PATCH 2/2] fix unrelated test --- test/core/plugins/samples/fn.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/core/plugins/samples/fn.js b/test/core/plugins/samples/fn.js index 8a6d262abaa..09a77dc7d14 100644 --- a/test/core/plugins/samples/fn.js +++ b/test/core/plugins/samples/fn.js @@ -244,9 +244,12 @@ describe("sampleFromSchema", function() { format: "date-time" } - var expected = new Date().toISOString() + // 0-20 chops off milliseconds + // necessary because test latency can cause failures + // it would be better to mock Date globally and expect a string - KS 11/18 + var expected = new Date().toISOString().substring(0, 20) - expect(sampleFromSchema(definition)).toEqual(expected) + expect(sampleFromSchema(definition)).toInclude(expected) }) it("returns example value for date property", function() {