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:
@@ -541,30 +541,54 @@ export const validateParam = (param, value, { isOAS3 = false, bypassRequiredChec
|
||||
return errors
|
||||
}
|
||||
|
||||
export const getSampleSchema = (schema, contentType="", config={}) => {
|
||||
if (/xml/.test(contentType)) {
|
||||
if (!schema.xml || !schema.xml.name) {
|
||||
schema.xml = schema.xml || {}
|
||||
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 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!-- XML example cannot be generated; root element name is undefined -->"
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
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 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!-- XML example cannot be generated; root element name is undefined -->"
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
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 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)
|
||||
: res
|
||||
}
|
||||
|
||||
export const getSampleSchema = (schema, contentType="", config={}) => {
|
||||
if (/xml/.test(contentType)) {
|
||||
return getXmlSampleSchema(schema, config)
|
||||
}
|
||||
|
||||
return getStringifiedSampleForSchema(schema, config, contentType)
|
||||
}
|
||||
|
||||
export const parseSearch = () => {
|
||||
let map = {}
|
||||
let search = win.location.search
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
Reference in New Issue
Block a user