utoipa_repr

Crates.ioutoipa_repr
lib.rsutoipa_repr
version0.1.0
created_at2025-11-07 15:34:16.724427+00
updated_at2025-11-07 15:34:16.724427+00
descriptionDerive utoipa::ToSchema that uses underlying integer representation of enum for schema generation
homepage
repository
max_upload_size
id1921739
size24,322
(inomata137)

documentation

README

utoipa_repr

Crates.io Documentation License: MIT OR Apache-2.0

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.

Installation

Add this to your Cargo.toml:

[dependencies]
utoipa_repr = "0.1"
utoipa = "5.0"

Usage

Basic Example

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
// }

With Signed Integer Types

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"
// }

Supported Types

The following repr types are supported:

  • Unsigned integers: u8, u16, u32, u64, u128, usize
  • Signed integers: i8, i16, i32, i64, i128, isize

Schema Generation

  • Unsigned types generate schemas with "minimum": 0 constraint
  • Signed types generate schemas without minimum constraint
  • All types currently use "format": "int32" in the generated schema

Additional repr Attributes

The 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,
}

Error Handling

The macro will produce compile-time errors in the following cases:

  • Missing #[repr(...)] attribute
  • Unsupported repr type
  • Invalid repr syntax

License

Licensed under either of Apache License, Version 2.0, or MIT License at your option.

Commit count: 0

cargo fmt