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")
This commit is contained in:
Mahtis Michel
2020-09-29 22:40:47 +02:00
committed by GitHub
parent 688ac909ea
commit ad630cc3e0
2 changed files with 65 additions and 20 deletions

View File

@@ -541,8 +541,7 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec
return errors return errors
} }
export const getSampleSchema = (schema, contentType="", config={}) => { const getXmlSampleSchema = (schema, config) => {
if (/xml/.test(contentType)) {
if (!schema.xml || !schema.xml.name) { if (!schema.xml || !schema.xml.name) {
schema.xml = schema.xml || {} schema.xml = schema.xml || {}
@@ -556,15 +555,40 @@ export const getSampleSchema = (schema, contentType="", config={}) => {
} }
} }
return memoizedCreateXMLExample(schema, config) return memoizedCreateXMLExample(schema, config)
}
const shouldStringifyTypesConfig = [
{
when: /json/,
shouldStringifyTypes: ["string"]
} }
]
const defaultStringifyTypes = ["object"]
const getStringifiedSampleForSchema = (schema, config, contentType) => {
const res = memoizedSampleFromSchema(schema, config) const res = memoizedSampleFromSchema(schema, config)
const resType = typeof res
return typeof res === "object" || typeof res === "string" 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) ? JSON.stringify(res, null, 2)
: res : res
} }
export const getSampleSchema = (schema, contentType="", config={}) => {
if (/xml/.test(contentType)) {
return getXmlSampleSchema(schema, config)
}
return getStringifiedSampleForSchema(schema, config, contentType)
}
export const parseSearch = () => { export const parseSearch = () => {
let map = {} let map = {}
let search = win.location.search let search = win.location.search

View File

@@ -1494,7 +1494,18 @@ describe("utils", () => {
Date = oriDate 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 // Given
const res = getSampleSchema({ const res = getSampleSchema({
type: "string", type: "string",
@@ -1502,7 +1513,7 @@ describe("utils", () => {
}) })
// Then // Then
expect(res).toEqual(JSON.stringify(new Date().toISOString())) expect(res).toEqual(new Date().toISOString())
}) })
it("should not unnecessarily stringify non-object values", () => { it("should not unnecessarily stringify non-object values", () => {
@@ -1514,6 +1525,16 @@ describe("utils", () => {
// Then // Then
expect(res).toEqual(0) 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", () => { describe("paramToIdentifier", () => {