This document shows how to update the code from API contract. Following the idea of API Contracts First we have always the changes done in a definition file then we generate the code. Appear examples for spring-boot and micronaut. We can choose different delivery mechanism for our application, in this case we have two implementation for a REST API. But even we can choose use a gRPC,GraphQL u others methods, and all have respect the boundaries between core and data providers
- Get you new API contract version and put in contract dir
- Get codegen cli jar if you don't have yet. Put in tools dir
- Choose your language and you configuration
- Execute the interface generation over existing code with the new contract. Example:
java -jar tools/swagger-codegen-cli-3.0.34.jar generate -i contract/adidas-APIContractFirst-Inventory-1.0.0-swagger.yaml -c tools/openAPIAutoGenConfig/springBootConfigGenerator.json -l spring -o testOuPut
- Implement the new interfaces add your news dto
In this section we are going to explain how we are using the command for more information you can go here
Command help good to see the supported language
java -jar tools/swagger-codegen-cli-3.0.34.jar -h
Use generate to autogenerate the code
-i route to contract
-c route to configuration
-l language to autogenerated. Doesn't identify stricly the language java mean a java client spring a server
-o output directory
See what parameters are allowed for the language that you choose "spring" in the example
java -jar tools/swagger-codegen-cli-3.0.34.jar config-help -l spring
The output in that parameter tell you what configuration you can do for the specific language, example library for spring
library
library template (sub-template) to use (Default: spring-boot)
spring-boot - Spring-boot Server application using the SpringFox integration.
spring-mvc - Spring-MVC Server application using the SpringFox integration.
spring-cloud - Spring-Cloud-Feign client with Spring-Boot auto-configured settings.
Example of configuration for spring-boot:
{
"invokerPackage": "com.adidas.apiteam",
"groupId": "com.adidas.apiteam",
"artifactId": "inventory",
"modelPackage": "com.adidas.apiteam.springboot.model",
"apiPackage": "com.adidas.apiteam.springboot.api",
"dateLibrary": "java11",
"java11": "true",
"dateLibrary": "spring-boot",
"interfaceOnly": "true"
}
We use interfaceOnly to only generate the model and the method in the API.
In the output directory we have a file .swagger-codegen-ingore to avoid override Readme and pom in every generation
Execute over the new contract to add the new features in API Contract
java -jar tools/swagger-codegen-cli-3.0.34.jar generate -i contract/adidas-APIContractFirst-Inventory-1.0.0-swagger.yaml -c tools/openAPIAutoGenConfig/springBootConfigGenerator.json -l spring -o springboot
Start the server
mvn -f .\springboot\pom.xml spring-boot:run
https://guides.micronaut.io/latest/creating-your-first-micronaut-app-maven-java.html
java -jar tools/swagger-codegen-cli-3.0.34.jar generate -i contract/adidas-APIContractFirst-Inventory-1.0.0-swagger.yaml -c tools/openAPIAutoGenConfig/micronautConfigGenerator.json -l micronaut -o micronaut
Start the server
mvn -f .\micronaut\pom.xml mn:run