| Crates.io | utoipa_repr |
| lib.rs | utoipa_repr |
| version | 0.1.0 |
| created_at | 2025-11-07 15:34:16.724427+00 |
| updated_at | 2025-11-07 15:34:16.724427+00 |
| description | Derive utoipa::ToSchema that uses underlying integer representation of enum for schema generation |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1921739 |
| size | 24,322 |
A derive macro for utoipa that provides ToSchema_repr functionality, similar to how serde_repr works for serde.
This crate allows you to generate OpenAPI schema for enums with #[repr(...)] attributes, representing them as their underlying integer types in the schema instead of as enum variants.
Add this to your Cargo.toml:
[dependencies]
utoipa_repr = "0.1"
utoipa = "5.0"
use utoipa_repr::ToSchema_repr;
#[derive(ToSchema_repr)]
#[repr(u8)]
enum Status {
Pending = 0,
InProgress = 1,
Completed = 2,
Failed = 3,
}
// The generated OpenAPI schema will represent this as:
// {
// "type": "integer",
// "format": "int32",
// "minimum": 0
// }
use utoipa_repr::ToSchema_repr;
#[derive(ToSchema_repr)]
#[repr(i32)]
enum Priority {
Low = -1,
Normal = 0,
High = 1,
Critical = 2,
}
// The generated OpenAPI schema will be:
// {
// "type": "integer",
// "format": "int32"
// }
The following repr types are supported:
u8, u16, u32, u64, u128, usizei8, i16, i32, i64, i128, isize"minimum": 0 constraint"format": "int32" in the generated schemarepr AttributesThe macro safely ignores other repr attributes like align and packed:
#[derive(ToSchema_repr)]
#[repr(align(8), u16)] // align(8) is ignored, u16 is used
enum AlignedEnum {
A,
B,
}
The macro will produce compile-time errors in the following cases:
#[repr(...)] attributerepr typerepr syntaxLicensed under either of Apache License, Version 2.0, or MIT License at your option.