#[api_dto]Expand description
Generates API DTO (Data Transfer Object) boilerplate for REST API types.
This macro automatically derives the necessary traits and attributes for types used in REST API requests and responses, ensuring they follow API conventions.
§Arguments
request- Marks the type as a request DTO (addsDeserializeandRequestApiDto)response- Marks the type as a response DTO (addsSerializeandResponseApiDto)
At least one of request or response must be specified. Both can be used together
for types that serve as both request and response DTOs.
§Generated Code
The macro generates:
#[derive(serde::Serialize)]ifresponseis specified#[derive(serde::Deserialize)]ifrequestis specified#[derive(utoipa::ToSchema)]forOpenAPIschema generation#[serde(rename_all = "snake_case")]to enforcesnake_casefield namingimpl RequestApiDtoifrequestis specifiedimpl ResponseApiDtoifresponseis specified
§Examples
ⓘ
// Request-only DTO
#[api_dto(request)]
pub struct CreateUserRequest {
pub user_name: String,
pub email: String,
}
// Response-only DTO
#[api_dto(response)]
pub struct UserResponse {
pub id: String,
pub user_name: String,
}
// Both request and response
#[api_dto(request, response)]
pub struct UserDto {
pub id: String,
pub user_name: String,
}§Field Naming
All fields are automatically converted to snake_case in JSON serialization,
regardless of the Rust field name.