从 YAML OpenAPI 规范中删除字段
Remove a field from YAML OpenAPI specification
我需要从我的 OpenAPI 规范中的每个方法中删除 tags
字段。
规范必须采用 YAML 格式,因为转换为 JSON 会导致稍后发布时出现问题。
我找不到现成的工具,我的编程技能也不够。我用 ruamel.yaml 尝试了 Python,但什么也做不了。
我愿意接受任何关于如何解决这个问题的建议 - 一个带有现成工具的回购协议,关于在 Python 中尝试什么的提示......我没有自己的想法。
也许是一个正则表达式,它可以捕获 tags
的所有实例的所有情况,这样我就可以进行搜索并用 Python 替换,什么都不替换?空行似乎不会破坏发布引擎。
这是一个示例 YAML 片段(我知道这不是一个合适的规范,只是想展示 YAML tags
所在的位置)
openapi: 3.0.0
info:
title: ""
description: ""
paths:
/endpoint
get:
tags:
-
tag1
-
tag3
#rest of GET definition
post:
tags:
- tag2
/anotherEndpoint
post:
tags:
- tag1
我需要完全删除所有 tags
数组(不仅仅是让它们为空)
我不确定为什么您无法使用 Python + ruamel.yaml 取得任何成就。确保您的规格
在文件 input.yaml
:
中
import sys
from pathlib import Path
import ruamel.yaml
in_file = Path('input.yaml')
out_file = Path('output.yaml')
yaml = ruamel.yaml.YAML()
yaml.indent(mapping=4, sequence=4, offset=2)
yaml.preserve_quotes = True
data = yaml.load(in_file)
# if you only have the three instances of 'tags', you can hard-code them:
# del data['paths']['/endpoint']['get']['tags']
# del data['paths']['/endpoint']['post']['tags']
# del data['paths']['/anotherEndpoint']['post']['tags']
# generic recursive removal of any key names 'tags' in the datastructure:
def rm_tags(d):
if isinstance(d, dict):
if 'tags' in d:
del d['tags']
for k in d:
rm_tags(d[k])
elif isinstance(d, list):
for item in d:
rm_tags(item)
rm_tags(data)
yaml.dump(data, out_file)
给出 output.yaml
:
openapi: 3.0.0
info:
title: ""
description: ""
paths:
/endpoint:
get: {}
post: {}
/anotherEndpoint:
post: {}
如果需要,您可以将 data
写回 input.yaml
。
请注意,通常注释 #rest of GET definition
会被保留,但是
现在不是,因为它在加载期间与它之前的密钥关联并且该密钥被删除。
我需要从我的 OpenAPI 规范中的每个方法中删除 tags
字段。
规范必须采用 YAML 格式,因为转换为 JSON 会导致稍后发布时出现问题。
我找不到现成的工具,我的编程技能也不够。我用 ruamel.yaml 尝试了 Python,但什么也做不了。
我愿意接受任何关于如何解决这个问题的建议 - 一个带有现成工具的回购协议,关于在 Python 中尝试什么的提示......我没有自己的想法。
也许是一个正则表达式,它可以捕获 tags
的所有实例的所有情况,这样我就可以进行搜索并用 Python 替换,什么都不替换?空行似乎不会破坏发布引擎。
这是一个示例 YAML 片段(我知道这不是一个合适的规范,只是想展示 YAML tags
所在的位置)
openapi: 3.0.0
info:
title: ""
description: ""
paths:
/endpoint
get:
tags:
-
tag1
-
tag3
#rest of GET definition
post:
tags:
- tag2
/anotherEndpoint
post:
tags:
- tag1
我需要完全删除所有 tags
数组(不仅仅是让它们为空)
我不确定为什么您无法使用 Python + ruamel.yaml 取得任何成就。确保您的规格
在文件 input.yaml
:
import sys
from pathlib import Path
import ruamel.yaml
in_file = Path('input.yaml')
out_file = Path('output.yaml')
yaml = ruamel.yaml.YAML()
yaml.indent(mapping=4, sequence=4, offset=2)
yaml.preserve_quotes = True
data = yaml.load(in_file)
# if you only have the three instances of 'tags', you can hard-code them:
# del data['paths']['/endpoint']['get']['tags']
# del data['paths']['/endpoint']['post']['tags']
# del data['paths']['/anotherEndpoint']['post']['tags']
# generic recursive removal of any key names 'tags' in the datastructure:
def rm_tags(d):
if isinstance(d, dict):
if 'tags' in d:
del d['tags']
for k in d:
rm_tags(d[k])
elif isinstance(d, list):
for item in d:
rm_tags(item)
rm_tags(data)
yaml.dump(data, out_file)
给出 output.yaml
:
openapi: 3.0.0
info:
title: ""
description: ""
paths:
/endpoint:
get: {}
post: {}
/anotherEndpoint:
post: {}
如果需要,您可以将 data
写回 input.yaml
。
请注意,通常注释 #rest of GET definition
会被保留,但是
现在不是,因为它在加载期间与它之前的密钥关联并且该密钥被删除。