pub struct OpenApiBuilder { /* private fields */ }Expand description
Builder for OpenAPI specifications.
Implementations§
Source§impl OpenApiBuilder
impl OpenApiBuilder
Sourcepub fn new(title: impl Into<String>, version: impl Into<String>) -> Self
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
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}Sourcepub fn description(self, desc: impl Into<String>) -> Self
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
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}Sourcepub fn register<T: ToSchema>(self) -> Self
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
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}Sourcepub fn schema(self, name: impl Into<String>, schema: Schema) -> Self
pub fn schema(self, name: impl Into<String>, schema: Schema) -> Self
Manually add a schema with a custom name.
Sourcepub fn build(self) -> OpenApiSpec
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
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§
impl Freeze for OpenApiBuilder
impl RefUnwindSafe for OpenApiBuilder
impl Send for OpenApiBuilder
impl Sync for OpenApiBuilder
impl Unpin for OpenApiBuilder
impl UnsafeUnpin for OpenApiBuilder
impl UnwindSafe for OpenApiBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more