feat(sample-gen): yaml sample generation (#6858)
* feat(sample-gen): yaml sample generation if content / media type matches yaml or yml it will generate stringified sample like for json in addition to that it will generate yaml out of the json sample Signed-off-by: mathis-m <mathis.michel@outlook.de> * test(unit-jest): getSampleSchema should handle yaml
This commit is contained in:
@@ -25,6 +25,7 @@ import cssEscape from "css.escape"
|
|||||||
import getParameterSchema from "../helpers/get-parameter-schema"
|
import getParameterSchema from "../helpers/get-parameter-schema"
|
||||||
import randomBytes from "randombytes"
|
import randomBytes from "randombytes"
|
||||||
import shaJs from "sha.js"
|
import shaJs from "sha.js"
|
||||||
|
import YAML from "js-yaml"
|
||||||
|
|
||||||
|
|
||||||
const DEFAULT_RESPONSE_KEY = "default"
|
const DEFAULT_RESPONSE_KEY = "default"
|
||||||
@@ -581,6 +582,25 @@ const getStringifiedSampleForSchema = (schema, config, contentType, exampleOverr
|
|||||||
: res
|
: res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getYamlSampleSchema = (schema, config, contentType, exampleOverride) => {
|
||||||
|
const jsonExample = getStringifiedSampleForSchema(schema, config, contentType, exampleOverride)
|
||||||
|
let yamlString
|
||||||
|
try {
|
||||||
|
yamlString = YAML.safeDump(YAML.safeLoad(jsonExample), {
|
||||||
|
|
||||||
|
lineWidth: -1 // don't generate line folds
|
||||||
|
})
|
||||||
|
if(yamlString[yamlString.length - 1] === "\n") {
|
||||||
|
yamlString = yamlString.slice(0, yamlString.length - 1)
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
return "error: could not generate yaml example"
|
||||||
|
}
|
||||||
|
return yamlString
|
||||||
|
.replace(/\t/g, " ")
|
||||||
|
}
|
||||||
|
|
||||||
export const getSampleSchema = (schema, contentType="", config={}, exampleOverride = undefined) => {
|
export const getSampleSchema = (schema, contentType="", config={}, exampleOverride = undefined) => {
|
||||||
if(schema && isFunc(schema.toJS))
|
if(schema && isFunc(schema.toJS))
|
||||||
schema = schema.toJS()
|
schema = schema.toJS()
|
||||||
@@ -590,7 +610,9 @@ export const getSampleSchema = (schema, contentType="", config={}, exampleOverri
|
|||||||
if (/xml/.test(contentType)) {
|
if (/xml/.test(contentType)) {
|
||||||
return getXmlSampleSchema(schema, config, exampleOverride)
|
return getXmlSampleSchema(schema, config, exampleOverride)
|
||||||
}
|
}
|
||||||
|
if (/(yaml|yml)/.test(contentType)) {
|
||||||
|
return getYamlSampleSchema(schema, config, contentType, exampleOverride)
|
||||||
|
}
|
||||||
return getStringifiedSampleForSchema(schema, config, contentType, exampleOverride)
|
return getStringifiedSampleForSchema(schema, config, contentType, exampleOverride)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1581,6 +1581,22 @@ describe("utils", () => {
|
|||||||
const actual = JSON.parse(res)
|
const actual = JSON.parse(res)
|
||||||
expect(actual.test).toEqual(123)
|
expect(actual.test).toEqual(123)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should return yaml example if yaml is contained in the content-type", () => {
|
||||||
|
const res = getSampleSchema({
|
||||||
|
type: "object",
|
||||||
|
}, "text/yaml", {}, {test: 123})
|
||||||
|
|
||||||
|
expect(res).toEqual("test: 123")
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should return yaml example if yml is contained in the content-type", () => {
|
||||||
|
const res = getSampleSchema({
|
||||||
|
type: "object",
|
||||||
|
}, "text/yml", {}, {test: 123})
|
||||||
|
|
||||||
|
expect(res).toEqual("test: 123")
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("paramToIdentifier", () => {
|
describe("paramToIdentifier", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user