From ad630cc3e08245670c7479ba76fc84bcf385718e Mon Sep 17 00:00:00 2001 From: Mahtis Michel Date: Tue, 29 Sep 2020 22:40:47 +0200 Subject: [PATCH] fix: sample schema should stringify string values for content-type: text/json (#6431) * reverts #6412 which stringified string value if content-type: text/plain * enable stringify string values matching `/json/` and allowList of `shouldStringifyTypes` ("string") --- src/core/utils.js | 60 ++++++++++++++++++++++++++++------------- test/unit/core/utils.js | 25 +++++++++++++++-- 2 files changed, 65 insertions(+), 20 deletions(-) diff --git a/src/core/utils.js b/src/core/utils.js index fb5c4396..ad85013f 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -541,28 +541,52 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec return errors } +const getXmlSampleSchema = (schema, config) => { + if (!schema.xml || !schema.xml.name) { + schema.xml = schema.xml || {} + + if (schema.$$ref) { + let match = schema.$$ref.match(/\S*\/(\S+)$/) + schema.xml.name = match[1] + } else if (schema.type || schema.items || schema.properties || schema.additionalProperties) { + return "\n" + } else { + return null + } + } + return memoizedCreateXMLExample(schema, config) +} + +const shouldStringifyTypesConfig = [ + { + when: /json/, + shouldStringifyTypes: ["string"] + } +] + +const defaultStringifyTypes = ["object"] + +const getStringifiedSampleForSchema = (schema, config, contentType) => { + const res = memoizedSampleFromSchema(schema, config) + const resType = typeof res + + const typesToStringify = shouldStringifyTypesConfig.reduce( + (types, nextConfig) => nextConfig.when.test(contentType) + ? [...types, ...nextConfig.shouldStringifyTypes] + : types, + defaultStringifyTypes) + + return some(typesToStringify, x => x === resType) + ? JSON.stringify(res, null, 2) + : res +} + export const getSampleSchema = (schema, contentType="", config={}) => { if (/xml/.test(contentType)) { - if (!schema.xml || !schema.xml.name) { - schema.xml = schema.xml || {} - - if (schema.$$ref) { - let match = schema.$$ref.match(/\S*\/(\S+)$/) - schema.xml.name = match[1] - } else if (schema.type || schema.items || schema.properties || schema.additionalProperties) { - return "\n" - } else { - return null - } - } - return memoizedCreateXMLExample(schema, config) + return getXmlSampleSchema(schema, config) } - const res = memoizedSampleFromSchema(schema, config) - - return typeof res === "object" || typeof res === "string" - ? JSON.stringify(res, null, 2) - : res + return getStringifiedSampleForSchema(schema, config, contentType) } export const parseSearch = () => { diff --git a/test/unit/core/utils.js b/test/unit/core/utils.js index 92a46ec8..53e69018 100644 --- a/test/unit/core/utils.js +++ b/test/unit/core/utils.js @@ -1494,7 +1494,18 @@ describe("utils", () => { Date = oriDate }) - it("should stringify string values", () => { + it("should stringify string values if json content-type", () => { + // Given + const res = getSampleSchema({ + type: "string", + format: "date-time" + }, "text/json") + + // Then + expect(res).toEqual(JSON.stringify(new Date().toISOString())) + }) + + it("should not unnecessarily stringify string values for other content-types", () => { // Given const res = getSampleSchema({ type: "string", @@ -1502,7 +1513,7 @@ describe("utils", () => { }) // Then - expect(res).toEqual(JSON.stringify(new Date().toISOString())) + expect(res).toEqual(new Date().toISOString()) }) it("should not unnecessarily stringify non-object values", () => { @@ -1514,6 +1525,16 @@ describe("utils", () => { // Then expect(res).toEqual(0) }) + + it("should not unnecessarily stringify non-object values if content-type is json", () => { + // Given + const res = getSampleSchema({ + type: "number" + }, "application/json") + + // Then + expect(res).toEqual(0) + }) }) describe("paramToIdentifier", () => {