1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//! Generate typed Rust traits from `OpenAPI` specifications.
//!
//! This crate exposes the [`axum`] and [`client`] attribute macros, which read
//! an `OpenAPI` specification file at compile time and generate inside the
//! annotated `mod`.
//!
//! # Examples
//!
//! ```rust
//! #[openapi_trait::axum("assets/testdata/petstore.openapi.yaml")]
//! pub mod petstore {}
//!
//! use petstore::PetstoreApi as _;
//!
//! #[derive(Clone)]
//! struct MyServer;
//!
//! #[derive(Clone)]
//! struct AppState;
//!
//! impl petstore::PetstoreApi<AppState> for MyServer {
//! type Error = petstore::NotImplemented;
//!
//! async fn get_pet_by_id(
//! &self,
//! req: petstore::GetPetByIdRequest,
//! _auth: petstore::ApiKey,
//! _state: axum::extract::State<AppState>,
//! _headers: axum::http::HeaderMap,
//! ) -> Result<petstore::GetPetByIdResponse, Self::Error> {
//! Ok(petstore::GetPetByIdResponse::Status200(petstore::Pet {
//! id: Some(req.pet_id),
//! name: "doggie".into(),
//! photo_urls: vec![],
//! category: None,
//! tags: None,
//! status: None,
//! }))
//! }
//! }
//!
//! let app: axum::Router = MyServer.router().with_state(AppState);
//! ```
//!
//! The generated trait names come from the annotated module name, so `mod petstore {}`
//! produces `petstore::PetstoreApi` and `petstore::PetstoreClient`.
//!
//! The `reqwest-client` feature is enabled by default. It adds [`ReqwestClient`],
//! [`ReqwestClientCore`], and the [`reqwest`] re-export used by the generated blanket
//! client implementation.
//!
//! # Validation
//!
//! The non-default `validation` feature makes every generated model type derive
//! [`serde_valid::Validate`](https://docs.rs/serde_valid) and gain
//! `#[validate(...)]` field attributes reflecting the schema's constraints
//! (`minLength`, `minimum`, `pattern`, `minItems`, `uniqueItems`, …). Bring the
//! `Validate` trait into scope (`serde_valid` is re-exported by this crate under
//! the feature) and call `.validate()`:
//!
//! ```toml
//! openapi-trait = { version = "0.1", features = ["validation"] }
//! ```
//!
//! ```ignore
//! use openapi_trait::serde_valid::Validate as _;
//! widget.validate()?; // Err if any declared constraint is violated
//! ```
//!
//! Validation is opt-in and non-enforcing — nothing calls `.validate()` for you,
//! and with the feature off the generated code is unchanged.
pub use openapi_trait as axum;
pub use openapi_trait as client;
/// Derive support for user-owned reqwest client carrier structs.
///
/// The derive looks for fields named `client` and `base_url` by default.
/// Override those conventions with `#[openapi_trait(client)]` and
/// `#[openapi_trait(base_url)]` on the corresponding fields.
pub use ReqwestClient;
/// Shared accessors used by generated reqwest client implementations.
/// Per-request transport options applied on top of the operation's own
/// parameters.
///
/// Every generated client method takes a `RequestOptions` argument, letting you
/// attach extra HTTP headers or authentication to a single request without
/// re-instantiating the underlying client. Pass [`RequestOptions::default`]
/// (or [`RequestOptions::new`]) when you have nothing to add.
///
/// The builder methods are chainable:
///
/// ```rust
/// # #[cfg(feature = "reqwest-client")] {
/// let options = openapi_trait::RequestOptions::new()
/// .bearer_auth("token-123")
/// .header("X-Request-Id", "abc");
/// # let _ = options;
/// # }
/// ```
///
/// Extra [`header`]s are applied after the operation's declared headers, so a
/// header set here is sent in addition to (and after) any same-named operation
/// header. Authentication set via [`bearer_auth`] or [`basic_auth`], by
/// contrast, *replaces* the `Authorization` header from a configured security
/// scheme, so per-request credentials deterministically win.
///
/// [`header`]: Self::header
/// [`bearer_auth`]: Self::bearer_auth
/// [`basic_auth`]: Self::basic_auth
/// Set `value` as the request's `Authorization` header, replacing any value an
/// earlier layer (such as a security scheme) already set rather than appending a
/// duplicate. Applying a single-entry [`HeaderMap`](reqwest::header::HeaderMap)
/// via `RequestBuilder::headers` uses reqwest's replace semantics.
/// Sibling of [`ReqwestClientCore`] for clients that carry credentials.
///
/// Implemented automatically by [`ReqwestClient`] when the carrier struct has
/// a field annotated `#[openapi_trait(auth)]` (or named `auth`). The generic
/// `A` is the generated `{Mod}AuthState` struct for the spec.
pub use percent_encoding;
pub use reqwest;
pub use base64;
pub use chrono;
/// Re-export of [`form_urlencoded`], used by generated axum server code to
/// decode raw query strings when applying `OpenAPI` `style`/`explode` rules.
pub use form_urlencoded;
pub use uuid;
/// Re-export of [`serde_valid`], backing the `#[validate(...)]` attributes on
/// generated model types when the `validation` feature is enabled.
///
/// Bring [`serde_valid::Validate`] into scope to call `model.validate()` on the
/// generated structs and enums.
pub use serde_valid;