循环访问 Swagger YAML 文件以动态生成属性列表



我正在尝试创建一个可以接受大摇大摆的yml文件的脚本(如示例petstore.yaml),并从该文件生成已用"属性"的列表。

这涉及解析 yaml,然后迭代 json 对象中的所有元素以返回所需的数据。我打算遍历所有路径以识别有效的响应,但现在我只想过滤掉 yaml 文件中指定的所有定义,然后为每个 def 输出属性列表。

可以在此处下载示例 YAML 文件

归根结底,我想为每个属性生成一个字符串,显示类似

<filename>~Pet~id~integer~int64
<filename>~Pet~name~string~
<filename>~Pet~tag~string~

为此,我需要找到"定义"节点,并遍历所有子节点以读取信息。

我正在努力为yaml样式的文件获得正确的逻辑。下面是我的工作代码。

对我来说,感觉我让迭代循环过于复杂(也许更好的解决方案是使用正则表达式?

索引.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<title>Read YAML</title>
</script><script src='https://cdnjs.cloudflare.com/ajax/libs/js-yaml/3.13.1/js-yaml.min.js'>            
</script>
<body>
<input type="file" id="file-input" />
<h3>Contents of the file:</h3>
<pre id="file-content"></pre>
<div id='output'></div>
</body>
<script>
var yaml = window.jsyaml;
</script>
<script src="app.js"></script>
</html>

我的 JavaScript 文件

var body = '';
var mystring = '' 
function readSingleFile(e) {
var file = e.target.files[0];
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
displayContents(contents);
};
reader.readAsText(file);
}
function displayContents(contents) {
var element = document.getElementById('file-content');
var doc = yaml.load(contents);
// Process the file.
// Process only the definitions section of the file
var definition = doc.definitions
console.log (definition) ;
for(var def in definition) {
if(definition.hasOwnProperty(def)) {
var node = definition[def]
if (typeof(node) === 'object') {
// loop through the definitions
processContents(node)
}
}
}
function processContents(obj) {
for (var item in obj) {
var definitions = obj[item]
if (typeof definitions === 'object'){
for (var attribute in definitions) {
// HELP -- if there is an attribute called "properties"  then iterate over all properties and create the concatenated string
// Broken from here !!!!
if (attribute.properties) {
for (var param  in attribute.properties) {
mystring = param.name + '~' + param.type + '~' + param.description + '~' + param.format
}
}
}
}
}
}
document.getElementById('output').innerHTML = body;
}
document.getElementById('file-input')
.addEventListener('change', readSingleFile, false);

我的时间不多了,所以我把它留在这里。它不像现在那么精致,但我希望它对你有所帮助。

所以我做了一个递归函数来遍历对象。该函数接受一个对象和一个前缀。前缀将用于打印详细信息。排除前缀用于不显示某些字段名称,排除类型用于不打印某些类型。循环遍历对象的字段,包括捕获格式、类型、描述以及要捕获的任何内容。循环对象字段后,检查类型字段是否已填充。如果是,则记录参数详细信息。

var body = '';
var mystring = '' 
function readSingleFile(e) {
var file = e.target.files[0];
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
displayContents(contents);
};
reader.readAsText(file);
}
function displayContents(contents) {
console.log('---contents---')
console.log(contents)
var element = document.getElementById('file-content');
console.log('---element----')
console.log(element)
var doc = yaml.load(contents);
console.log('---doc')
console.log(doc)
// Process the file.
// Process only the definitions section of the file
var definition = doc.definitions
console.log('---definition---')
console.log (definition) ;
processContents2(doc.definitions)
function processContents2(obj, prefix) {
const excludePrefixes = ['properties']
const excludeTypes = ['object', 'array']
let format = ''
let type = ''
let description = ''
let count = 0;
for (var field in obj) {
if (typeof obj[field] === 'object') {
processContents2(obj[field], (prefix || '') + (excludePrefixes.indexOf(field) === -1 ? '~' + field : ''))
} else {
if (field === 'format') {
format = obj[field]
} else if (field === 'type') {
type = obj[field]
} else if (field === 'description') {
description = obj[field]
}
}
}
if (type && (excludeTypes.indexOf(type) === -1)) {
console.log((prefix || '') + '~' + type  + (description ? '~' + description : '') + (format ? '~' + format : ''))
} 
}
document.getElementById('output').innerHTML = body;
}
document.getElementById('file-input')
.addEventListener('change', readSingleFile, false);

最新更新