Home > Software engineering >  How to merge multiple OpenAPI 3.0 specification files into one in Maven?
How to merge multiple OpenAPI 3.0 specification files into one in Maven?

Time:03-19

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?

CodePudding user response:

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>

CodePudding user response:

Helen's answer helped me a lot, I can generate a spec file with all components from several input spec.

But now I meet new problems. All input spec are on UTF-8, some of them have accented letters, the output file is on windows-1252, although I put UTF-8 in pom.xml:

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
...
  <configOptions>
    <outputFile>expert_api.yml</outputFile>
    <file.encoding>UTF-8</file.encoding>
  </configOptions>

I also want to generate Java server codes on Sping Boot, but it did't work, I met compile errors.

<language>spring</language>
  • Related