feat(json-schema): expose API that generates examples from JSON Schema (#9190)
This allows to use the samples API in a static way without fully instantiating SwaggerUI. Refs #9188
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* @prettier
|
||||
*/
|
||||
import YAML, { JSON_SCHEMA } from "js-yaml"
|
||||
|
||||
const makeGetYamlSampleSchema =
|
||||
(getSystem) => (schema, config, contentType, exampleOverride) => {
|
||||
const { fn } = getSystem()
|
||||
const jsonExample = fn.getJsonSampleSchema(
|
||||
schema,
|
||||
config,
|
||||
contentType,
|
||||
exampleOverride
|
||||
)
|
||||
let yamlString
|
||||
try {
|
||||
yamlString = YAML.dump(
|
||||
YAML.load(jsonExample),
|
||||
{
|
||||
lineWidth: -1, // don't generate line folds
|
||||
},
|
||||
{ schema: JSON_SCHEMA }
|
||||
)
|
||||
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 default makeGetYamlSampleSchema
|
||||
Reference in New Issue
Block a user