This guide will help you set up and use the OpenAPI Model Generator plugin in your project.
Add both the OpenAPI Generator plugin and this plugin to your build.gradle:
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.1'
id 'org.openapi.generator' version '7.14.0' // Must be applied first
id 'com.guidedbyte.openapi-modelgen' version '@version@' // Apply second
}
Important: The OpenAPI Generator plugin must be applied before this plugin.
dependencies {
// Spring Boot dependencies
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-validation'
// OpenAPI/Swagger annotations
implementation 'io.swagger.core.v3:swagger-annotations:2.2.19'
// Lombok (optional but recommended)
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
Create a directory structure and add your OpenAPI specification:
src/
└── main/
└── resources/
└── openapi/
└── api.yaml
Example minimal specification (src/main/resources/openapi/api.yaml):
openapi: 3.0.3
info:
title: My API
version: 1.0.0
paths: {} # Paths not needed for model generation
components:
schemas:
User:
type: object
properties:
id:
type: integer
format: int64
name:
type: string
email:
type: string
format: email
required:
- name
- email
CreateUserRequest:
type: object
properties:
name:
type: string
email:
type: string
format: email
required:
- name
- email
Add the minimal configuration to your build.gradle:
openapiModelgen {
specs {
myApi {
inputSpec "src/main/resources/openapi/api.yaml"
modelPackage "com.example.model"
}
}
}
Note: Use method call syntax (no equals signs) for all configuration.
Run the generation task:
./gradlew generateMyApi
Or generate all configured specifications:
./gradlew generateAllModels
The generated models will be in:
build/
└── generated/
└── sources/
└── openapi/
└── src/
└── main/
└── java/
└── com/
└── example/
└── model/
├── UserDto.java
└── CreateUserRequestDto.java
Example generated code:
package com.example.model;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.experimental.SuperBuilder;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import jakarta.validation.constraints.*;
@Data
@Accessors(fluent = true)
@SuperBuilder
@NoArgsConstructor(force = true)
@AllArgsConstructor
public class UserDto implements Serializable {
private Long id;
@NotNull
private String name;
@NotNull
@Email
private String email;
}
Add the generated source directory to your IDE and use the models:
@RestController
public class UserController {
@PostMapping("/users")
public ResponseEntity<UserDto> createUser(@Valid @RequestBody CreateUserRequestDto request) {
UserDto user = UserDto.builder()
.name(request.name())
.email(request.email())
.build();
// Save user logic here
return ResponseEntity.ok(user);
}
}
openapiModelgen {
specs {
users {
inputSpec "src/main/resources/openapi/users.yaml"
modelPackage "com.example.users.model"
}
orders {
inputSpec "src/main/resources/openapi/orders.yaml"
modelPackage "com.example.orders.model"
}
}
}
openapiModelgen {
defaults {
outputDir "src/generated/java"
}
specs {
myApi {
inputSpec "src/main/resources/openapi/api.yaml"
modelPackage "com.example.model"
}
}
}
openapiModelgen {
defaults {
validateSpec true // Validate OpenAPI spec before generation
}
specs {
myApi {
inputSpec "src/main/resources/openapi/api.yaml"
modelPackage "com.example.model"
}
}
}
The plugin includes several performance optimizations enabled by default:
For configuration cache compatibility (faster builds):
./gradlew generateAllModels --configuration-cache
build/generated/sources/openapi/src/main/java.vscode/settings.json:{
"java.compile.sourcePaths": [
"src/main/java",
"build/generated/sources/openapi/src/main/java"
]
}
Make compilation depend on model generation:
compileJava.dependsOn 'generateAllModels'
Clean generated sources:
clean {
delete 'build/generated'
}
This completes the basic setup! Your project now generates type-safe Java DTOs from OpenAPI specifications with Lombok support and enterprise-grade performance optimizations.