Skip to content

Commit 9d02a7d

Browse files
authored
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
1 parent a86fcf3 commit 9d02a7d

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/core/plugins/samples/fn.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export const sampleFromSchema = (schema, config={}) => {
2929
let { type, example, properties, additionalProperties, items } = objectify(schema)
3030
let { includeReadOnly, includeWriteOnly } = config
3131

32+
3233
if(example !== undefined) {
3334
return deeplyStripKey(example, "$$ref", (val) => {
3435
// do a couple of quick sanity tests to ensure the value

src/core/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ export const getCommonExtensions = (defObj) => defObj.filter((v, k) => /^pattern
742742
// `predicate` can be used to discriminate the stripping further,
743743
// by preserving the key's place in the object based on its value.
744744
export function deeplyStripKey(input, keyToStrip, predicate = () => true) {
745-
if(typeof input !== "object" || Array.isArray(input) || !keyToStrip) {
745+
if(typeof input !== "object" || Array.isArray(input) || input === null || !keyToStrip) {
746746
return input
747747
}
748748

test/core/plugins/samples/fn.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,46 @@ describe("sampleFromSchema", function() {
379379

380380
expect(sampleFromSchema(definition)).toEqual(expected)
381381
})
382+
383+
it("returns null for a null example", function() {
384+
var definition = {
385+
"type": "object",
386+
"properties": {
387+
"foo": {
388+
"type": "string",
389+
"nullable": true,
390+
"example": null
391+
}
392+
}
393+
}
394+
395+
var expected = {
396+
foo: null
397+
}
398+
399+
expect(sampleFromSchema(definition)).toEqual(expected)
400+
})
401+
402+
it("returns null for a null object-level example", function() {
403+
var definition = {
404+
"type": "object",
405+
"properties": {
406+
"foo": {
407+
"type": "string",
408+
"nullable": true
409+
}
410+
},
411+
"example": {
412+
"foo": null
413+
}
414+
}
415+
416+
var expected = {
417+
foo: null
418+
}
419+
420+
expect(sampleFromSchema(definition)).toEqual(expected)
421+
})
382422
})
383423
})
384424

0 commit comments

Comments
 (0)