OpenApiBuilder

Struct OpenApiBuilder 

Source
pub struct OpenApiBuilder { /* private fields */ }
Expand description

Builder for OpenAPI specifications.

Implementations§

Source§

impl OpenApiBuilder

Source

pub fn new(title: impl Into<String>, version: impl Into<String>) -> Self

Create a new OpenAPI builder.

§Example
use domainstack_schema::OpenApiBuilder;

let spec = OpenApiBuilder::new("My API", "1.0.0")
    .description("A sample API")
    .build();
Examples found in repository?
examples/user_api.rs (line 134)
132fn main() {
133    // Build OpenAPI specification
134    let spec = OpenApiBuilder::new("User Management API", "1.0.0")
135        .description("API for managing users, addresses, and teams with validation")
136        .register::<User>()
137        .register::<Address>()
138        .register::<Team>()
139        .build();
140
141    // Output as JSON
142    let json = spec.to_json().expect("Failed to serialize to JSON");
143    println!("{}", json);
144
145    println!("\n=== Schema Generation Complete ===");
146    println!("Registered schemas:");
147    println!("  - User (with email, age, name, status)");
148    println!("  - Address (with street, zip code, city)");
149    println!("  - Team (with name and members array)");
150    println!("\nValidation constraints mapped to OpenAPI:");
151    println!("  [ok] email → format: email");
152    println!("  [ok] range(min, max) → minimum, maximum");
153    println!("  [ok] length(min, max) → minLength, maxLength");
154    println!("  [ok] one_of → enum");
155    println!("  [ok] min_items, max_items → minItems, maxItems");
156    println!("  [ok] numeric_string → pattern: ^[0-9]+$");
157}
More examples
Hide additional examples
examples/v08_features.rs (line 245)
244fn main() {
245    let spec = OpenApiBuilder::new("v0.8 Features Demo", "1.0.0")
246        .description("Demonstrates OpenAPI v0.8 features: composition, metadata, and extensions")
247        .register::<PaymentMethod>()
248        .register::<AdminUser>()
249        .register::<User>()
250        .register::<UserSettings>()
251        .register::<UserAccount>()
252        .register::<DateRange>()
253        .register::<OrderForm>()
254        .build();
255
256    println!("{}", spec.to_json().expect("Failed to serialize"));
257
258    println!("\n=== v0.8 Features Demonstrated ===");
259    println!("[ok] anyOf: PaymentMethod (union of card | cash)");
260    println!("[ok] allOf: AdminUser (extends User)");
261    println!("[ok] default: UserSettings.theme = 'auto'");
262    println!("[ok] example: UserSettings.theme example = 'dark'");
263    println!("[ok] examples: UserSettings.language examples = ['en', 'es', 'fr']");
264    println!("[ok] readOnly: UserAccount.id, createdAt (response only)");
265    println!("[ok] writeOnly: UserAccount.password (request only)");
266    println!("[ok] deprecated: UserAccount.oldUsername");
267    println!("[ok] vendor extensions: DateRange, OrderForm (x-domainstack-validations)");
268    println!("\nAll v0.8 features working correctly!");
269}
Source

pub fn description(self, desc: impl Into<String>) -> Self

Set the API description.

Examples found in repository?
examples/user_api.rs (line 135)
132fn main() {
133    // Build OpenAPI specification
134    let spec = OpenApiBuilder::new("User Management API", "1.0.0")
135        .description("API for managing users, addresses, and teams with validation")
136        .register::<User>()
137        .register::<Address>()
138        .register::<Team>()
139        .build();
140
141    // Output as JSON
142    let json = spec.to_json().expect("Failed to serialize to JSON");
143    println!("{}", json);
144
145    println!("\n=== Schema Generation Complete ===");
146    println!("Registered schemas:");
147    println!("  - User (with email, age, name, status)");
148    println!("  - Address (with street, zip code, city)");
149    println!("  - Team (with name and members array)");
150    println!("\nValidation constraints mapped to OpenAPI:");
151    println!("  [ok] email → format: email");
152    println!("  [ok] range(min, max) → minimum, maximum");
153    println!("  [ok] length(min, max) → minLength, maxLength");
154    println!("  [ok] one_of → enum");
155    println!("  [ok] min_items, max_items → minItems, maxItems");
156    println!("  [ok] numeric_string → pattern: ^[0-9]+$");
157}
More examples
Hide additional examples
examples/v08_features.rs (line 246)
244fn main() {
245    let spec = OpenApiBuilder::new("v0.8 Features Demo", "1.0.0")
246        .description("Demonstrates OpenAPI v0.8 features: composition, metadata, and extensions")
247        .register::<PaymentMethod>()
248        .register::<AdminUser>()
249        .register::<User>()
250        .register::<UserSettings>()
251        .register::<UserAccount>()
252        .register::<DateRange>()
253        .register::<OrderForm>()
254        .build();
255
256    println!("{}", spec.to_json().expect("Failed to serialize"));
257
258    println!("\n=== v0.8 Features Demonstrated ===");
259    println!("[ok] anyOf: PaymentMethod (union of card | cash)");
260    println!("[ok] allOf: AdminUser (extends User)");
261    println!("[ok] default: UserSettings.theme = 'auto'");
262    println!("[ok] example: UserSettings.theme example = 'dark'");
263    println!("[ok] examples: UserSettings.language examples = ['en', 'es', 'fr']");
264    println!("[ok] readOnly: UserAccount.id, createdAt (response only)");
265    println!("[ok] writeOnly: UserAccount.password (request only)");
266    println!("[ok] deprecated: UserAccount.oldUsername");
267    println!("[ok] vendor extensions: DateRange, OrderForm (x-domainstack-validations)");
268    println!("\nAll v0.8 features working correctly!");
269}
Source

pub fn register<T: ToSchema>(self) -> Self

Register a type that implements ToSchema.

§Example
use domainstack_schema::{OpenApiBuilder, ToSchema, Schema};

struct User;
impl ToSchema for User {
    fn schema_name() -> &'static str { "User" }
    fn schema() -> Schema { Schema::object() }
}

let spec = OpenApiBuilder::new("API", "1.0")
    .register::<User>()
    .build();
Examples found in repository?
examples/user_api.rs (line 136)
132fn main() {
133    // Build OpenAPI specification
134    let spec = OpenApiBuilder::new("User Management API", "1.0.0")
135        .description("API for managing users, addresses, and teams with validation")
136        .register::<User>()
137        .register::<Address>()
138        .register::<Team>()
139        .build();
140
141    // Output as JSON
142    let json = spec.to_json().expect("Failed to serialize to JSON");
143    println!("{}", json);
144
145    println!("\n=== Schema Generation Complete ===");
146    println!("Registered schemas:");
147    println!("  - User (with email, age, name, status)");
148    println!("  - Address (with street, zip code, city)");
149    println!("  - Team (with name and members array)");
150    println!("\nValidation constraints mapped to OpenAPI:");
151    println!("  [ok] email → format: email");
152    println!("  [ok] range(min, max) → minimum, maximum");
153    println!("  [ok] length(min, max) → minLength, maxLength");
154    println!("  [ok] one_of → enum");
155    println!("  [ok] min_items, max_items → minItems, maxItems");
156    println!("  [ok] numeric_string → pattern: ^[0-9]+$");
157}
More examples
Hide additional examples
examples/v08_features.rs (line 247)
244fn main() {
245    let spec = OpenApiBuilder::new("v0.8 Features Demo", "1.0.0")
246        .description("Demonstrates OpenAPI v0.8 features: composition, metadata, and extensions")
247        .register::<PaymentMethod>()
248        .register::<AdminUser>()
249        .register::<User>()
250        .register::<UserSettings>()
251        .register::<UserAccount>()
252        .register::<DateRange>()
253        .register::<OrderForm>()
254        .build();
255
256    println!("{}", spec.to_json().expect("Failed to serialize"));
257
258    println!("\n=== v0.8 Features Demonstrated ===");
259    println!("[ok] anyOf: PaymentMethod (union of card | cash)");
260    println!("[ok] allOf: AdminUser (extends User)");
261    println!("[ok] default: UserSettings.theme = 'auto'");
262    println!("[ok] example: UserSettings.theme example = 'dark'");
263    println!("[ok] examples: UserSettings.language examples = ['en', 'es', 'fr']");
264    println!("[ok] readOnly: UserAccount.id, createdAt (response only)");
265    println!("[ok] writeOnly: UserAccount.password (request only)");
266    println!("[ok] deprecated: UserAccount.oldUsername");
267    println!("[ok] vendor extensions: DateRange, OrderForm (x-domainstack-validations)");
268    println!("\nAll v0.8 features working correctly!");
269}
Source

pub fn schema(self, name: impl Into<String>, schema: Schema) -> Self

Manually add a schema with a custom name.

Source

pub fn build(self) -> OpenApiSpec

Build the final OpenAPI specification.

Examples found in repository?
examples/user_api.rs (line 139)
132fn main() {
133    // Build OpenAPI specification
134    let spec = OpenApiBuilder::new("User Management API", "1.0.0")
135        .description("API for managing users, addresses, and teams with validation")
136        .register::<User>()
137        .register::<Address>()
138        .register::<Team>()
139        .build();
140
141    // Output as JSON
142    let json = spec.to_json().expect("Failed to serialize to JSON");
143    println!("{}", json);
144
145    println!("\n=== Schema Generation Complete ===");
146    println!("Registered schemas:");
147    println!("  - User (with email, age, name, status)");
148    println!("  - Address (with street, zip code, city)");
149    println!("  - Team (with name and members array)");
150    println!("\nValidation constraints mapped to OpenAPI:");
151    println!("  [ok] email → format: email");
152    println!("  [ok] range(min, max) → minimum, maximum");
153    println!("  [ok] length(min, max) → minLength, maxLength");
154    println!("  [ok] one_of → enum");
155    println!("  [ok] min_items, max_items → minItems, maxItems");
156    println!("  [ok] numeric_string → pattern: ^[0-9]+$");
157}
More examples
Hide additional examples
examples/v08_features.rs (line 254)
244fn main() {
245    let spec = OpenApiBuilder::new("v0.8 Features Demo", "1.0.0")
246        .description("Demonstrates OpenAPI v0.8 features: composition, metadata, and extensions")
247        .register::<PaymentMethod>()
248        .register::<AdminUser>()
249        .register::<User>()
250        .register::<UserSettings>()
251        .register::<UserAccount>()
252        .register::<DateRange>()
253        .register::<OrderForm>()
254        .build();
255
256    println!("{}", spec.to_json().expect("Failed to serialize"));
257
258    println!("\n=== v0.8 Features Demonstrated ===");
259    println!("[ok] anyOf: PaymentMethod (union of card | cash)");
260    println!("[ok] allOf: AdminUser (extends User)");
261    println!("[ok] default: UserSettings.theme = 'auto'");
262    println!("[ok] example: UserSettings.theme example = 'dark'");
263    println!("[ok] examples: UserSettings.language examples = ['en', 'es', 'fr']");
264    println!("[ok] readOnly: UserAccount.id, createdAt (response only)");
265    println!("[ok] writeOnly: UserAccount.password (request only)");
266    println!("[ok] deprecated: UserAccount.oldUsername");
267    println!("[ok] vendor extensions: DateRange, OrderForm (x-domainstack-validations)");
268    println!("\nAll v0.8 features working correctly!");
269}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.