fix(sample-gen): should return json literal example (#6827)

* fix(sample-gen): should return json literal example

* test(sample-gen): should return json literal example
This commit is contained in:
Mahtis Michel
2021-01-14 19:59:35 +01:00
committed by GitHub
parent 7087210adb
commit a2f7917661
2 changed files with 243 additions and 210 deletions

View File

@@ -203,8 +203,18 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und
// if json just return
if(!respectXML) {
// return if sample does not need any parsing
if(typeof sample !== "string") {
return sample
}
// check if sample is parsable or just a plain string
try {
return JSON.parse(sample)
} catch(e) {
// sample is just plain string return it
return sample
}
}
// recover missing type
if(!schema) {

View File

@@ -1535,6 +1535,29 @@ describe("utils", () => {
// Then
expect(res).toEqual(0)
})
it("should stringify object when literal string example is provided if json content-type", () => {
// Given
const expected = "<MyObject></MyObject>"
const res = getSampleSchema({
type: "object",
}, "text/json", {}, expected)
// Then
expect(res).toEqual(JSON.stringify(expected))
})
it("should parse valid json literal example if json content-type", () => {
// Given
const expected = {test: 123}
const res = getSampleSchema({
type: "object",
}, "text/json", {}, JSON.stringify(expected))
// Then
const actual = JSON.parse(res)
expect(actual.test).toEqual(123)
})
})
describe("paramToIdentifier", () => {