From c022d73820c8d1c4440c619747cd416b40ecc1e5 Mon Sep 17 00:00:00 2001 From: mathis-m Date: Tue, 12 Jan 2021 03:08:56 +0100 Subject: [PATCH 1/2] fix(sample-gen): should render additionalProperties in example --- src/core/plugins/samples/fn.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/core/plugins/samples/fn.js b/src/core/plugins/samples/fn.js index 03d0a462dac..cdfc927c21f 100644 --- a/src/core/plugins/samples/fn.js +++ b/src/core/plugins/samples/fn.js @@ -143,8 +143,7 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und let addPropertyToResult if(respectXML) { addPropertyToResult = (propName, overrideE = undefined) => { - - if(schema) { + if(schema && props[propName]) { // case it is an xml attribute props[propName].xml = props[propName].xml || {} @@ -168,6 +167,13 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und return } props[propName].xml.name = props[propName].xml.name || propName + } else if(!props[propName] && additionalProperties !== false) { + // case only additionalProperty that is not defined in schema + props[propName] = { + xml: { + name: propName + } + } } let t = sampleFromSchemaGeneric(schema && props[propName] || undefined, config, overrideE, respectXML) From 2b9f21277df585668a59f79977580555fce35acd Mon Sep 17 00:00:00 2001 From: mathis-m Date: Tue, 12 Jan 2021 04:16:23 +0100 Subject: [PATCH 2/2] test(sample-gen): should return additional property from example for object and array schemas --- test/unit/core/plugins/samples/fn.js | 41 ++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/unit/core/plugins/samples/fn.js b/test/unit/core/plugins/samples/fn.js index 0a76637917a..c2b327e0528 100644 --- a/test/unit/core/plugins/samples/fn.js +++ b/test/unit/core/plugins/samples/fn.js @@ -1093,7 +1093,28 @@ describe("createXMLExample", function () { expect(sut(definition)).toEqual(expected) }) + it("should return additionalProperty example", () => { + let expected = "\n\n\ttest\n" + let definition = { + type: "array", + items: { + type: "object", + properties: { + alien: { + type: "string" + }, + dog: { + type: "integer" + } + } + }, + xml: { + name: "aliens" + } + } + expect(sut(definition, {}, [{ notalien: "test" }])).toEqual(expected) + }) }) describe("object", function () { @@ -1521,6 +1542,26 @@ describe("createXMLExample", function () { expect(sut(definition, {}, overrideExample)).toEqual(expected) }) + + it("should return additionalProperty example", () => { + let expected = "\n\n\ttest\n\t1\n" + let definition = { + type: "object", + properties: { + alien: { + type: "string" + }, + dog: { + type: "integer" + } + }, + xml: { + name: "aliens" + } + } + + expect(sut(definition, {}, { alien: "test", dog: 1 })).toEqual(expected) + }) }) })