从命令行将虚张声势的YAML文件转换为JSON

时间:2022-08-30 00:15:51

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

我想从命令行将一个虚张声势的YAML文件转换为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.io editor, when you choose File -> Download JSON

我希望在不使用Python或Ruby的情况下实现这一点,并且我还希望在格式化JSON时能够控制领先的空格,以便与Swagger的editor.swagger输出的JSON匹配。io编辑器,选择File ->下载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.io should be empty.

我相信这个问题有很多“正确”的答案。我正在寻找一种最优雅、依赖关系最少的解决方案。理想情况下,生成的JSON文件与编辑器.swagger生成的JSON文件的差值。io应该是空的。

4 个解决方案

#1


20  

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

我想你是在寻找虚张声势的代码功能:

Running

运行

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

swagger-codegen生成-我昂首阔步。yaml - l大摇大摆

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

威尔大摇大摆地走了出来。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页面上有一个链接到docker镜像和nodejs服务器(可以用一个不同的答案来转换使用curl命令)。

#2


5  

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

你可以使用网上招摇过市的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 spec object. You'll get a link to download the converted & validated spec, in yaml format.

在spec对象中输入虚张声势定义的值。您将获得一个以yaml格式下载转换和验证规范的链接。

For options, take a look here:

选择的话,请看这里:

http://generator.swagger.io/

http://generator.swagger.io/

#3


4  

Using yamljs:

使用yamljs:

yaml2json swagger.yaml -p -i4

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

这个命令的输出与编辑器的JSON输出相冲突。io产生一个空的diff。

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.

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

#4


4  

swagger-codegen cli interface

As Liel has already pointed out, you can run

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

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

swagger-codegen生成-我昂首阔步。yaml - l大摇大摆

Docker

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

如果您使用Docker,那么我建议您尝试swaggerapi/swagger-code -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运行-v ./docs:/docs swaggerapi/swagger-code -cli生成i/ docs/swagger。yaml -l大摇大摆的-o /docs。

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

我喜欢设置docker- composer。yml to“alias”此命令易于重用:

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

现在,我可以开始了

#1


20  

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

我想你是在寻找虚张声势的代码功能:

Running

运行

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

swagger-codegen生成-我昂首阔步。yaml - l大摇大摆

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

威尔大摇大摆地走了出来。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页面上有一个链接到docker镜像和nodejs服务器(可以用一个不同的答案来转换使用curl命令)。

#2


5  

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

你可以使用网上招摇过市的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 spec object. You'll get a link to download the converted & validated spec, in yaml format.

在spec对象中输入虚张声势定义的值。您将获得一个以yaml格式下载转换和验证规范的链接。

For options, take a look here:

选择的话,请看这里:

http://generator.swagger.io/

http://generator.swagger.io/

#3


4  

Using yamljs:

使用yamljs:

yaml2json swagger.yaml -p -i4

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

这个命令的输出与编辑器的JSON输出相冲突。io产生一个空的diff。

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.

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

#4


4  

swagger-codegen cli interface

As Liel has already pointed out, you can run

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

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

swagger-codegen生成-我昂首阔步。yaml - l大摇大摆

Docker

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

如果您使用Docker,那么我建议您尝试swaggerapi/swagger-code -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运行-v ./docs:/docs swaggerapi/swagger-code -cli生成i/ docs/swagger。yaml -l大摇大摆的-o /docs。

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

我喜欢设置docker- composer。yml to“alias”此命令易于重用:

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

现在,我可以开始了