从命令行将 Swagger YAML 文件转换为 JSON [英] Converting a Swagger YAML file to JSON from the command line
问题描述
我想从命令行将 Swagger YAML
文件转换为 JSON
.计划是在 CI 作业期间使用此命令行.我在 google 上搜索并找到了许多解决方案,但其中大多数使用 Python 或 Ruby,我不想使用它们.例如:http://www.commandlinefu.com/commands/查看/12218/convert-yaml-to-json
我想在不使用 Python 或 Ruby 的情况下执行此操作,并且我还希望能够在格式化 JSON 以完全匹配从 Swagger 的 编辑器输出的 JSON 时控制前导空格.swagger.io 编辑器,当你选择 File
-> Download JSON
这意味着我希望空白填充为四个空格,如下所示:
<代码>{招摇":2.0",信息":{"title": "API 标题",
我没有尝试过上面链接中的 Python 方法,但是 Ruby 方法使用两个空格空白填充.也许有办法控制它,但我不想在这个解决方案中使用 Ruby 或 Python.
我确信这个问题有很多正确"的答案.我正在寻找最优雅、依赖最少的解决方案.理想情况下,生成的 JSON 文件与 editor.swagger.io 生成的 JSON 文件的差异应该为空.
我认为你正在寻找 swagger-codegen(现在 OpenApi-generator)功能:p>
跑步
swagger-codegen 生成 -i swagger.yaml -l swagger
会在同一位置放一个 swagger.json.
CI 更新:如果你可以将它安装在你的构建机器上——对你有好处.如果你不能 - github页面有一个指向带有nodejs服务器的docker镜像的链接可用(使用curl命令进行转换,如不同答案中建议的那样).
Docker 更新:如果您使用 Docker,请尝试 swaggerapi/swagger-codegen-cli,有一个 docker-compose 的例子可以帮助 Fabian &切尼.
关于 OpenApi 的更新:
这个问题是关于swagger的,而且有几年了.如果你刚开始使用 Swagger,你应该改用 OpenApi,如果你有现有的 swagger 文件,我建议 迁移.
I'd like to convert a Swagger YAML
file to JSON
from the command line. The plan is to use this command line during a CI job. I've searched on google and found many solutions, but most of them use Python or Ruby, which I'd prefer not to use. For example: http://www.commandlinefu.com/commands/view/12218/convert-yaml-to-json
I'd like to do this without using Python or Ruby, and
I'd also like to be able to control the leading whitespace when formatting the JSON to match exactly the JSON that is output from Swagger's editor.swagger.io editor, when you choose File
-> Download JSON
All this means is that I'd like the whitespace padding to be four spaces, like so:
{
"swagger": "2.0",
"info": {
"title": "API TITLE",
I haven't tried the Python method in the link above, but the Ruby method uses two space whitespace padding. Perhaps there is a way to control that, but I don't want to use Ruby or Python in this solution anyway.
I'm sure that there are many "correct" answers to this question. I am looking for the solution that is most elegant with the least number of dependencies. Ideally, a diff of the resulting JSON file against a JSON file generated by the editor.swagger.io should be empty.
I think that you are looking for the swagger-codegen (now OpenApi-generator) functionality:
Running
swagger-codegen generate -i swagger.yaml -l swagger
will out put a swagger.json in the same location.
Update For CI: If you can install it on your build machine- good for you. If you can't - the github page has a link to a docker image with a nodejs server is available (to convert using a curl command as suggested in a different answer).
Update For Docker: If you use Docker, try swaggerapi/swagger-codegen-cli, there is an example for docker-compose that might help a few answers down by Fabian & ckeeney.
Update about OpenApi:
This question is about swagger, and a few years old. If you're just starting to use Swagger you should switch to OpenApi instead, and if you have existing swagger files, i suggest migrating.
这篇关于从命令行将 Swagger YAML 文件转换为 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!