【问题标题】:Swagger | Convert YAML to JSON by maven command招摇 |通过 maven 命令将 YAML 转换为 JSON
【发布时间】:2020-01-06 18:34:26
【问题描述】:

有什么方法可以将我的 YAML 转换为 JSON 的依赖项添加到我现有的 pom.xml 文件中? (包括验证,以防 YAML 不正确)

我正在寻找可以在http://editor.swagger.io/ 上执行相同转换(或类似)的东西(当您单击文件-> 转换为 JSON 时)

但我找不到任何可以做这种事情的 maven 依赖项

感谢您的提前!

【问题讨论】:

    标签: maven yaml swagger


    【解决方案1】:

    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>
    

    【讨论】:

    • 非常感谢您的回答,我尝试了上面的插件,它成功了! :)。
    • 我想将我的 YAML 文件转换为 JSON 的原因是 openapi-generator 不支持 YAML 别名。因此,如果您的 YAML 包含别名,则此答案将不起作用。
    • @AdrianPronk 我认为 Swagger Codegen 的解析器支持 YAML 锚点/别名。不确定 openapi-generator。
    【解决方案2】:

    除了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>
    

    【讨论】:

    • 这个工具也不处理 YAML 别名
    • 您需要确保 /target/merged-swage 路径根据您的目标构建路径(放置 swaggers 的位置)进行更改,并且 target/json 是插件构建后 JSON 所在的位置跨度>
    猜你喜欢
    • 2016-04-16
    • 2017-12-19
    • 1970-01-01
    • 2019-09-20
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    相关资源
    最近更新 更多