fix(sample-gen): case yaml parsed example is number but string schema (#6872)
This commit is contained in:
@@ -203,8 +203,12 @@ export const sampleFromSchemaGeneric = (schema, config={}, exampleOverride = und
|
|||||||
|
|
||||||
// if json just return
|
// if json just return
|
||||||
if(!respectXML) {
|
if(!respectXML) {
|
||||||
|
// spacial case yaml parser can not know about
|
||||||
|
if(typeof sample === "number" && type === "string") {
|
||||||
|
return `${sample}`
|
||||||
|
}
|
||||||
// return if sample does not need any parsing
|
// return if sample does not need any parsing
|
||||||
if(typeof sample !== "string") {
|
if(typeof sample !== "string" || type === "string") {
|
||||||
return sample
|
return sample
|
||||||
}
|
}
|
||||||
// check if sample is parsable or just a plain string
|
// check if sample is parsable or just a plain string
|
||||||
|
|||||||
@@ -1582,6 +1582,42 @@ describe("utils", () => {
|
|||||||
expect(actual.test).toEqual(123)
|
expect(actual.test).toEqual(123)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should handle number example with string schema as string", () => {
|
||||||
|
// Given
|
||||||
|
const expected = 123
|
||||||
|
const res = getSampleSchema({
|
||||||
|
type: "string",
|
||||||
|
}, "text/json", {}, expected)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
const actual = JSON.parse(res)
|
||||||
|
expect(actual).toEqual("123")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should handle number literal example with string schema as string", () => {
|
||||||
|
// Given
|
||||||
|
const expected = "123"
|
||||||
|
const res = getSampleSchema({
|
||||||
|
type: "string",
|
||||||
|
}, "text/json", {}, expected)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
const actual = JSON.parse(res)
|
||||||
|
expect(actual).toEqual("123")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should handle number literal example with number schema as number", () => {
|
||||||
|
// Given
|
||||||
|
const expected = "123"
|
||||||
|
const res = getSampleSchema({
|
||||||
|
type: "number",
|
||||||
|
}, "text/json", {}, expected)
|
||||||
|
|
||||||
|
// Then
|
||||||
|
const actual = JSON.parse(res)
|
||||||
|
expect(actual).toEqual(123)
|
||||||
|
})
|
||||||
|
|
||||||
it("should return yaml example if yaml is contained in the content-type", () => {
|
it("should return yaml example if yaml is contained in the content-type", () => {
|
||||||
const res = getSampleSchema({
|
const res = getSampleSchema({
|
||||||
type: "object",
|
type: "object",
|
||||||
|
|||||||
Reference in New Issue
Block a user