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:
@@ -203,7 +203,17 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und
|
|||||||
|
|
||||||
// if json just return
|
// if json just return
|
||||||
if(!respectXML) {
|
if(!respectXML) {
|
||||||
return sample
|
// 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
|
// recover missing type
|
||||||
|
|||||||
@@ -1535,6 +1535,29 @@ describe("utils", () => {
|
|||||||
// Then
|
// Then
|
||||||
expect(res).toEqual(0)
|
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", () => {
|
describe("paramToIdentifier", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user