【发布时间】:2020-01-06 18:34:26
【问题描述】:
有什么方法可以将我的 YAML 转换为 JSON 的依赖项添加到我现有的 pom.xml 文件中? (包括验证,以防 YAML 不正确)
我正在寻找可以在http://editor.swagger.io/ 上执行相同转换(或类似)的东西(当您单击文件-> 转换为 JSON 时)
但我找不到任何可以做这种事情的 maven 依赖项
感谢您的提前!
【问题讨论】:
有什么方法可以将我的 YAML 转换为 JSON 的依赖项添加到我现有的 pom.xml 文件中? (包括验证,以防 YAML 不正确)
我正在寻找可以在http://editor.swagger.io/ 上执行相同转换(或类似)的东西(当您单击文件-> 转换为 JSON 时)
但我找不到任何可以做这种事情的 maven 依赖项
感谢您的提前!
【问题讨论】:
Swagger Codegen Maven plugin 可以将 OpenAPI/Swagger 定义从 YAML 转换为 JSON,反之亦然。对swagger: '2.0' 定义使用插件版本2.x,对openapi: 3.0.0 定义使用v. 3.x。
插件自动解析外部$ref 引用并生成单个输出文件。
OpenAPI 2.0 / Codegen 2.x 示例:
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.4.8</version>
<executions>
<execution>
<id>convert</id>
<phase>generate-resources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>https://petstore.swagger.io/v2/swagger.yaml</inputSpec>
<!-- Output directory, relative to the project directory. Default is ${project.build.directory}/generated-sources/swagger -->
<output>specs</output>
<!-- Use "swagger" to convert YAML->JSON or "swagger-yaml" to convert JSON->YAML -->
<language>swagger</language>
<configOptions>
<!-- Default output file name is swagger.json or swagger.yaml -->
<outputFile>petstore.json</outputFile>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
OpenAPI 3.0 / Codegen 3.x 示例:
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.11</version>
<executions>
<execution>
<id>convert</id>
<phase>generate-resources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/uspto.yaml</inputSpec>
<!-- Use "openapi" to convert YAML->JSON or "openapi-yaml" to convert JSON->YAML -->
<language>openapi</language>
<!-- Output directory, relative to the project directory. Default is ${project.build.directory}/generated-sources/swagger -->
<output>specs</output>
<configOptions>
<!-- Default output file name is openapi.json or openapi.yaml -->
<outputFile>uspto.json</outputFile>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
【讨论】:
除了Helen的回答,我找到了另一个可以生成多yamls的插件
<plugin>
<groupId>com.github.ngeor</groupId>
<artifactId>yak4j-json-yaml-converter-maven-plugin</artifactId>
<version>0.0.4</version>
<executions>
<execution>
<id>yaml2json</id>
<goals>
<goal>yaml2json</goal>
</goals>
<configuration>
<sourceDirectory>${basedir}/target/merged-swagger</sourceDirectory>
<includes>
<include>*.yaml</include>
</includes>
<outputDirectory>target/json</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
【讨论】: