From 821ba887a617fd17bc98ddff416abc493bd543e6 Mon Sep 17 00:00:00 2001 From: Mahtis Michel Date: Mon, 21 Sep 2020 21:16:26 +0200 Subject: [PATCH] fix: string samples should also be json stringified. (#6412) fixes #6120 --- src/core/utils.js | 4 +++- test/unit/core/utils.js | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/core/utils.js b/src/core/utils.js index 30b3f5fd..fb5c4396 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -560,7 +560,9 @@ export const getSampleSchema = (schema, contentType="", config={}) => { const res = memoizedSampleFromSchema(schema, config) - return typeof res === "object" ? JSON.stringify(res, null, 2) : res + return typeof res === "object" || typeof res === "string" + ? JSON.stringify(res, null, 2) + : res } export const parseSearch = () => { diff --git a/test/unit/core/utils.js b/test/unit/core/utils.js index 3b7b53af..92a46ec8 100644 --- a/test/unit/core/utils.js +++ b/test/unit/core/utils.js @@ -1494,7 +1494,7 @@ describe("utils", () => { Date = oriDate }) - it("should not unnecessarily stringify non-object values", () => { + it("should stringify string values", () => { // Given const res = getSampleSchema({ type: "string", @@ -1502,7 +1502,17 @@ describe("utils", () => { }) // Then - expect(res).toEqual(new Date().toISOString()) + expect(res).toEqual(JSON.stringify(new Date().toISOString())) + }) + + it("should not unnecessarily stringify non-object values", () => { + // Given + const res = getSampleSchema({ + type: "number" + }) + + // Then + expect(res).toEqual(0) }) })