Skip to content

Commit

Permalink
fix: sample schema should stringify string values for content-type: t…
Browse files Browse the repository at this point in the history
…ext/json (#6431)

* reverts #6412 which stringified string value if content-type: text/plain 
* enable stringify string values matching `/json/` and allowList of `shouldStringifyTypes` ("string")
  • Loading branch information
mathis-m committed Sep 29, 2020
1 parent 688ac90 commit ad630cc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
56 changes: 40 additions & 16 deletions src/core/utils.js
Expand Up @@ -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 || {}

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
}
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
}
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

const typesToStringify = shouldStringifyTypesConfig.reduce(
(types, nextConfig) => nextConfig.when.test(contentType)
? [...types, ...nextConfig.shouldStringifyTypes]
: types,
defaultStringifyTypes)

return typeof res === "object" || typeof res === "string"
? JSON.stringify(res, null, 2)
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
Expand Down
25 changes: 23 additions & 2 deletions test/unit/core/utils.js
Expand Up @@ -1494,17 +1494,28 @@ 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",
format: "date-time"
})

// Then
expect(res).toEqual(new Date().toISOString())
})

it("should not unnecessarily stringify non-object values", () => {
// Given
const res = getSampleSchema({
Expand All @@ -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", () => {
Expand Down

0 comments on commit ad630cc

Please sign in to comment.