From dd0184dda3a8fb5b779fe2f536c8af05a4bbd11b Mon Sep 17 00:00:00 2001 From: mathis-m Date: Wed, 27 Jan 2021 21:29:03 +0100 Subject: [PATCH 1/2] fix(sample-gen): case yaml parsed example to number but string schema Signed-off-by: mathis-m --- src/core/plugins/samples/fn.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/core/plugins/samples/fn.js b/src/core/plugins/samples/fn.js index 684cf347276..d71f1a6a89b 100644 --- a/src/core/plugins/samples/fn.js +++ b/src/core/plugins/samples/fn.js @@ -203,8 +203,12 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und // if json just return if(!respectXML) { + // spacial case yaml parser can not know about + if(typeof sample === "number" && type === "string") { + return `${sample}` + } // return if sample does not need any parsing - if(typeof sample !== "string") { + if(typeof sample !== "string" || type === "string") { return sample } // check if sample is parsable or just a plain string From 5ed23eab2211de48a472db4efcc9fe924201b75a Mon Sep 17 00:00:00 2001 From: mathis-m Date: Wed, 27 Jan 2021 22:26:47 +0100 Subject: [PATCH 2/2] test: ensure number examples are rendered correctly Signed-off-by: mathis-m --- test/unit/core/utils.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/unit/core/utils.js b/test/unit/core/utils.js index 34999ba4714..e567f93a33c 100644 --- a/test/unit/core/utils.js +++ b/test/unit/core/utils.js @@ -1582,6 +1582,42 @@ describe("utils", () => { expect(actual.test).toEqual(123) }) + it("should handle number example with string schema as string", () => { + // Given + const expected = 123 + const res = getSampleSchema({ + type: "string", + }, "text/json", {}, expected) + + // Then + const actual = JSON.parse(res) + expect(actual).toEqual("123") + }) + + it("should handle number literal example with string schema as string", () => { + // Given + const expected = "123" + const res = getSampleSchema({ + type: "string", + }, "text/json", {}, expected) + + // Then + const actual = JSON.parse(res) + expect(actual).toEqual("123") + }) + + it("should handle number literal example with number schema as number", () => { + // Given + const expected = "123" + const res = getSampleSchema({ + type: "number", + }, "text/json", {}, expected) + + // Then + const actual = JSON.parse(res) + expect(actual).toEqual(123) + }) + it("should return yaml example if yaml is contained in the content-type", () => { const res = getSampleSchema({ type: "object",