Files
swagger-ui/src/core/plugins/json-schema-5-samples/fn/get-yaml-sample-schema.js
Vladimír Gorej 113996f627 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
2023-09-05 14:13:53 +02:00

35 lines
843 B
JavaScript

/**
* @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