From 9d02a7d8adb7ccbeaa132a71ae8d5c184d09b8a9 Mon Sep 17 00:00:00 2001 From: kyle Date: Wed, 27 Jun 2018 19:40:00 -0700 Subject: [PATCH] fix(sample-gen): respect null values in examples (via #4679) * improvement: re-enable and improve Models jump-to-path * fix(sample-gen): respect null values in examples --- src/core/plugins/samples/fn.js | 1 + src/core/utils.js | 2 +- test/core/plugins/samples/fn.js | 40 +++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/core/plugins/samples/fn.js b/src/core/plugins/samples/fn.js index d7fa8cad..feabcec4 100644 --- a/src/core/plugins/samples/fn.js +++ b/src/core/plugins/samples/fn.js @@ -29,6 +29,7 @@ export const sampleFromSchema = (schema, config={}) => { let { type, example, properties, additionalProperties, items } = objectify(schema) let { includeReadOnly, includeWriteOnly } = config + if(example !== undefined) { return deeplyStripKey(example, "$$ref", (val) => { // do a couple of quick sanity tests to ensure the value diff --git a/src/core/utils.js b/src/core/utils.js index 3f77a1bf..793a29ec 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -742,7 +742,7 @@ export const getCommonExtensions = (defObj) => defObj.filter((v, k) => /^pattern // `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) { + if(typeof input !== "object" || Array.isArray(input) || input === null || !keyToStrip) { return input } diff --git a/test/core/plugins/samples/fn.js b/test/core/plugins/samples/fn.js index a3266177..609ba24b 100644 --- a/test/core/plugins/samples/fn.js +++ b/test/core/plugins/samples/fn.js @@ -379,6 +379,46 @@ describe("sampleFromSchema", function() { expect(sampleFromSchema(definition)).toEqual(expected) }) + + it("returns null for a null example", function() { + var definition = { + "type": "object", + "properties": { + "foo": { + "type": "string", + "nullable": true, + "example": null + } + } + } + + var expected = { + foo: null + } + + expect(sampleFromSchema(definition)).toEqual(expected) + }) + + it("returns null for a null object-level example", function() { + var definition = { + "type": "object", + "properties": { + "foo": { + "type": "string", + "nullable": true + } + }, + "example": { + "foo": null + } + } + + var expected = { + foo: null + } + + expect(sampleFromSchema(definition)).toEqual(expected) + }) }) })