user6063698
user6063698

Reputation:

Swagger | Convert YAML to JSON by maven command

Is there any way that I can add to my existing pom.xml file a dependency that can convert my YAMLs to JSONs? (Include validation in case on of the YAMLs are incorrect)

I am looking for something that can do the same convert (or similar) that is on http://editor.swagger.io/ (When you click on File-> Convert to JSON)

But I couldn't find any maven dependency that can do such thing

Thanks for advance!

Upvotes: 5

Views: 4946

Answers (2)

user6063698
user6063698

Reputation:

In addition to Helen's answer, I found another plugin that can generate multi-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>

Upvotes: 0

Helen
Helen

Reputation: 97540

Swagger Codegen Maven plugin can convert OpenAPI/Swagger definitions from YAML to JSON and vice versa. Use plugin version 2.x for swagger: '2.0' definitions and v. 3.x for openapi: 3.0.0 definitions.

The plugin automatically resolves external $ref references and produces a single output file.

Example for 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>

Example for 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>

Upvotes: 5

Related Questions