从命令行将 Swagger YAML 文件转换为 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34733253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:13:21  来源:igfitidea点击:

Converting a Swagger YAML file to JSON from the command line

jsoncommand-lineyamlswagger

提问by mkrufky

I'd like to convert a Swagger YAMLfile to JSONfrom 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

我想将 SwaggerYAML文件转换为JSON命令行。计划是在 CI 作业期间使用此命令行。我在谷歌上搜索并找到了很多解决方案,但大多数都使用 Python 或 Ruby,我不想使用它们。例如: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.ioeditor, when you choose File-> Download JSON

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

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

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.ioshould be empty.

我相信这个问题有很多“正确”的答案。我正在寻找最优雅且依赖项最少的解决方案。理想情况下,生成的 JSON 文件与由editor.swagger.io生成的 JSON 文件的差异应该为空。

回答by Liel

I think that you are looking for the swagger-codegenfunctionality:

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

Running

跑步

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

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

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

将在同一位置放置一个 swagger.json。

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

更新 CI:如果您可以将它安装在您的构建机器上 - 对您有好处。如果你不能 - github 页面有一个指向带有 nodejs 服务器的 docker 图像的链接可用(使用不同答案中建议的 curl 命令进行转换)。

回答by mkrufky

Using yamljs:

使用yamljs

yaml2json swagger.yaml -p -i4

The output from this command diff'd against the JSON output from editor.swagger.ioproduces an empty diff.

此命令的输出与editor.swagger.io的 JSON 输出进行了比较,产生了一个空的差异。

This is indeed what I'm looking for, but it brings in a huge dependency (node). I'm hoping for something even lighter, yet equally as elegant as this.

这确实是我正在寻找的,但它带来了巨大的依赖(节点)。我希望有更轻巧但同样优雅的东西。

回答by fehguy

You can use the online swagger codegen project to do this:

您可以使用在线 swagger codegen 项目来执行此操作:

curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d "{
  \"spec\": {}
}" "https://generator.swagger.io/api/gen/clients/swagger-yaml"

Put the value of your swagger definition in the specobject. You'll get a link to download the converted & validated spec, in yaml format.

将 swagger 定义的值放入spec对象中。您将获得一个链接,用于下载 yaml 格式的转换和验证的规范。

For options, take a look here:

有关选项,请查看此处:

http://generator.swagger.io/

http://generator.swagger.io/

回答by ckeeney

swagger-codegen cli interface

swagger-codegen cli 界面

As Liel has already pointed out, you can run

正如Liel 已经指出的,你可以运行

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

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

Docker

码头工人

If you use Docker, then I suggest you try swaggerapi/swagger-codegen-cli.

如果您使用 Docker,那么我建议您尝试swaggerapi/swagger-codegen-cli

You can generate a json file using docker with the following command:

您可以使用 docker 使用以下命令生成一个 json 文件:

docker run -v ./docs:/docs swaggerapi/swagger-codegen-cli generate -i /docs/swagger.yaml -l swagger -o /docs

docker run -v ./docs:/docs swaggerapi/swagger-codegen-cli generate -i /docs/swagger.yaml -l swagger -o /docs

I like to setup a docker-compose.ymlto "alias" this command for easy reuse:

我喜欢设置一个docker-compose.yml“别名”这个命令以便于重用:

version: "2"
services:
  gen-swagger:
    volumes:
      - ./docs:/docs
    image: swaggerapi/swagger-codegen-cli
    command: generate -i /docs/swagger.yaml -l swagger -o /docs

And now I can just run docker-compose run gen-swagger

现在我可以跑了 docker-compose run gen-swagger

回答by kuzdu

For version swagger-codegen 3.0.4

对于版本swagger-codegen 3.0.4

Use

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

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

to get a .json.

得到一个.json.

回答by Sebastian

Another possibility to convert a swagger.yml file to swagger.json is a NPM package called swagger-cli.

将 swagger.yml 文件转换为 swagger.json 的另一种可能性是名为swagger-cli的 NPM 包。

npm install -g swagger-cli

Then you can convert a yml to json file:

然后你可以将 yml 转换为 json 文件:

swagger-cli bundle -o api-spec.json api-spec.yml