Generate Code from the Contract
Why do We Want to Generate Code from the Contract?
If we were to write by hand one Primary Adapter that has some few endpoints and its necessary models (called Schemas
in the OpenAPI Specification) it may not be that complicated.

But imagine if it was a big project, with a few dozen Primary Adapters… Doing this manually is prone to error and consumes a lot of time.

OpenAPI Generator
Let’s save us some problems by using one of the greatest libraries to ever exist: openapi-generator.
-
Add the plugin in the plugins section (usually at the start of
build.gradle
):build.gradleid 'org.openapi.generator' version '7.12.0'
-
We need Swagger Core Jakarta, JsonNullable Jackson Module, and Spring Boot Starter Validation dependencies. Add them in the
build.gradle
dependencies section:build.gradleimplementation 'io.swagger.core.v3:swagger-core-jakarta:2.2.28'
implementation 'org.openapitools:jackson-databind-nullable:0.2.6'
implementation 'org.springframework.boot:spring-boot-starter-validation' -
Configure the openapi-generator (bottom of
build.gradle
):build.gradleopenApiGenerate {
apiPackage = "${project.group}.${project.name}.adapter.in.rest.api".toString()
configOptions = [
interfaceOnly: "true",
skipOperationExample: "true",
useEnumCaseInsensitive: "true",
useSpringBoot3: "true"
]
generateApiTests = false
generateApiDocumentation = false
generateModelTests = false
generateModelDocumentation = false
generatorName = "spring"
inputSpec = "$rootDir/src/main/resources/openapi/users_manager.yaml".toString()
modelPackage = "${project.group}.${project.name}.adapter.in.rest.dto".toString()
outputDir = layout.buildDirectory.dir("generated/sources/openapi").get().asFile.toString()
}- You can find more information about the different possible configurations in the OpenAPI Generator Gradle Plugin GitHub page.
- It is important to make sure that
inputspec
is pointing to the desired OpenAPI Specification YAML file.
-
Add the
sourceSets
configuration. This tells Gradle where to find the generated Java sources. Place the following code immediately below theplugins
section:build.gradlesourceSets {
main {
java {
srcDir(layout.buildDirectory.dir("generated/sources/openapi/src/main/java"))
}
}
} -
Generate code on compilation by adding a new task:
build.gradletasks.named('compileJava') {
dependsOn 'openApiGenerate'
} -
Now that everything is set up, run the Build Task. When the task finishes, check the
build\generated\sources\openapi
folder. You’ll find the representation of the OpenAPI Specification (our contract) in Java code, ready to be used.
Commit the progress so far.
git add .
git commit -m "generated primary adapter code from an openapi specification"