wonder garance
wonder garance

Reputation: 349

How to merge multiple OpenAPI 3.0 specification files into one in Maven?

In a Maven project with Spring Boot, there are several OpenAPI 3.0 specification files. One spec defines all HTTP errors (errors.yml), and the components of errors.yml are referenced in other spec. I want to generate an output spec with all components of errors.yml inside.

Input spec:

  schema:
    $ref: "errors.yml#/components/schemas/Error"

Wished output spec:

schema:
  $ref: "#/components/schemas/Error"
  ...
Error:
  ...

I can do it with swagger-codegen-cli:

java -Dfile.encoding=UTF-8 -jar swagger-codegen-cli-3.0.33.jar generate -l openapi-yaml -i search-api-contract/target/expert_api.yml -o . -DoutputFile=search-api-contract/target/expert_api.yml

How to generate one spec with Maven pom.xml?

Upvotes: 8

Views: 11259

Answers (1)

Helen
Helen

Reputation: 98022

Swagger Codegen has a Maven plugin. You can use it like this:

<plugin>
    <groupId>io.swagger.codegen.v3</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>3.0.33</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>path/to/main/openapi.yaml</inputSpec>

                <!-- Use 'openapi-yaml' to get resolved YAML or 'openapi' to get resolved JSON -->
                <language>openapi-yaml</language>

                <!-- Default is ${project.build.directory}/generated-sources/swagger -->
                <output>path/to/output/folder</output>

                <configOptions>
                    <!-- Default output file name is 'openapi.yaml' or 'openapi.json' -->
                    <outputFile>myapi.yaml</outputFile>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

Upvotes: 6

Related Questions