This article provides a practical guide on integrating OpenAPI documentation generation into a CI pipeline using Spring Boot and Springdoc. It highlights the importance of automated documentation for client generation, service mocking, and contract verification, emphasizing how a dedicated Maven profile can streamline this process within a Continuous Integration workflow. The core idea is to generate the OpenAPI specification during the build process, treating it as a valuable artifact for downstream development and integration tasks.
Read original on DZone MicroservicesGenerating OpenAPI documentation is crucial for modern microservice architectures, enabling seamless communication and integration between services and their consumers. This article outlines a "recipe" for automating this process within a CI/CD pipeline, moving beyond manual extraction from Swagger UI to a more robust, build-time approach.
The key to automated OpenAPI generation lies in leveraging build tools and plugins. For Spring Boot applications, the `springdoc-openapi-maven-plugin` is utilized, but it requires the application to be running to fetch the API definition. This necessitates a specific setup within the build process.
Architectural Consideration: Dedicated Profiles
The article proposes using a dedicated Spring profile (e.g., `openapi`) and a corresponding Maven profile. This isolates the configuration needed for documentation generation, such as relaxed security rules that allow access to the `/api-docs` endpoint during the build, without impacting the application's runtime security configuration. This separation of concerns is a good practice for build automation.
<profile>
<id>openapi</id>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
<build>
<plugins>
<!-- Run Spring with the openapi profile -->
<plugin>
<artifactId>spring-boot-maven-plugin</artifactId>
<groupId>org.springframework.boot</groupId>
<configuration>
<jvmArguments>
-Dspring.application.admin.enabled=true -Dspring.profiles.active=openapi
</jvmArguments>
</configuration>
<executions>
<execution>
<id>pre-integration-test</id>
<goals><goal>start</goal></goals>
</execution>
<execution>
<id>post-integration-test</id>
<goals><goal>stop</goal></goals>
</execution>
</executions>
</plugin>
<!-- Generate the OpenAPI file -->
<plugin>
<artifactId>springdoc-openapi-maven-plugin</artifactId>
<groupId>org.springdoc</groupId>
<version>1.4</version>
<configuration>
<skip>false</skip>
<apiDocsUrl>http://localhost:8080/api-docs.yaml</apiDocsUrl>
<outputDir>${project.build.directory}</outputDir>
<outputFileName>openapi.yml</outputFileName>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals><goal>generate</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>This setup allows the OpenAPI specification to be generated as a build artifact, which can then be archived, published to an artifact repository, or directly consumed by other tools in the CI/CD pipeline, facilitating robust API-first development and integration.