从命令行将Swagger YAML文件转换为JSON [英] Converting a Swagger YAML file to JSON from the command line

查看:673
本文介绍了从命令行将Swagger YAML文件转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从命令行将Swagger YAML文件转换为JSON.计划是在CI作业期间使用此命令行.我在Google上进行搜索,发现了许多解决方案,但是大多数解决方案都使用Python或Ruby,但我不希望使用它们.例如: http://www.commandlinefu.com/commands/view/12218/convert-yaml-to-json

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

我想不使用Python或Ruby来执行此操作,并且 我还希望能够在格式化JSON以完全匹配Swagger的编辑器输出的JSON时控制前导空格. swagger.io 编辑器,当您选择File-> Download 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",

我没有在上面的链接中尝试过Python方法,但是Ruby方法使用了两个空格填充.也许有一种方法可以控制它,但是我还是不想在此解决方案中使用Ruby或Python.

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.

我确定对此问题有很多正确"的答案.我正在寻找最少依赖项的最优雅的解决方案.理想情况下,结果JSON文件与 editor.swagger.io 生成的JSON文件之间的差异应为空.

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.

推荐答案

我认为您正在寻找 swagger-codegen 功能:

I think that you are looking for the swagger-codegen functionality:

运行

swagger-codegen generate -i swagger.yaml -l swagger

将把swagger.json放在相同的位置.

will out put a swagger.json in the same location.

更新CI: 如果您可以将其安装在您的构建机器上-对您有好处. 如果不能-github页面上有指向nodejs服务器的docker映像的链接,该链接可用(根据其他答案中的建议使用curl命令进行转换).

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).

这篇关于从命令行将Swagger YAML文件转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆