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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// SPDX-FileCopyrightText: 2025-2026 RAprogramm <andrey.rozanov.vl@gmail.com>
// SPDX-License-Identifier: MIT
//! OpenAPI struct generation for utoipa 5.x.
//!
//! This module generates complete OpenAPI documentation structs that implement
//! `utoipa::OpenApi` for seamless Swagger UI integration. It leverages the
//! `Modify` trait pattern to dynamically add security schemes, paths, and
//! additional components at runtime.
//!
//! # Architecture Overview
//!
//! The generation process produces two interconnected components:
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────┐
//! │ OpenAPI Generation │
//! ├─────────────────────────────────────────────────────────────────┤
//! │ │
//! │ EntityDef ─────────────────────────────────────────────────┐ │
//! │ │ │ │
//! │ ▼ │ │
//! │ ┌─────────────────┐ ┌────────────────────────────────┐ │ │
//! │ │ {Entity}Api │────>│ {Entity}ApiModifier │ │ │
//! │ │ #[OpenApi] │ │ impl Modify │ │ │
//! │ │ - schemas │ │ - add_security_scheme() │ │ │
//! │ │ - modifiers │ │ - add_path_operation() │ │ │
//! │ │ - tags │ │ - insert schemas │ │ │
//! │ └─────────────────┘ └────────────────────────────────┘ │ │
//! │ │ │
//! │ Generated at │ │
//! │ compile time │ │
//! └─────────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Generated Code
//!
//! For a `User` entity with CRUD handlers and bearer security:
//!
//! ```rust,ignore
//! /// OpenAPI modifier for User entity.
//! ///
//! /// Implements utoipa's Modify trait to dynamically configure
//! /// the OpenAPI specification at runtime.
//! struct UserApiModifier;
//!
//! impl utoipa::Modify for UserApiModifier {
//! fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
//! use utoipa::openapi::*;
//!
//! // Configure API metadata
//! openapi.info.title = "User API".to_string();
//! openapi.info.version = "1.0.0".to_string();
//!
//! // Add bearer authentication scheme
//! if let Some(components) = openapi.components.as_mut() {
//! components.add_security_scheme("bearerAuth",
//! security::SecurityScheme::Http(
//! security::HttpBuilder::new()
//! .scheme(security::HttpAuthScheme::Bearer)
//! .bearer_format("JWT")
//! .build()
//! )
//! );
//!
//! // Add ErrorResponse and PaginationQuery schemas
//! components.schemas.insert("ErrorResponse".to_string(), ...);
//! components.schemas.insert("PaginationQuery".to_string(), ...);
//! }
//!
//! // Add CRUD path operations
//! // POST /users - Create user
//! // GET /users - List users
//! // GET /users/{id} - Get user by ID
//! // PATCH /users/{id} - Update user
//! // DELETE /users/{id} - Delete user
//! }
//! }
//!
//! /// OpenAPI documentation for User entity endpoints.
//! ///
//! /// # Usage
//! ///
//! /// ```rust,ignore
//! /// use utoipa::OpenApi;
//! /// let openapi = UserApi::openapi();
//! /// ```
//! #[derive(utoipa::OpenApi)]
//! #[openapi(
//! components(schemas(UserResponse, CreateUserRequest, UpdateUserRequest)),
//! modifiers(&UserApiModifier),
//! tags((name = "Users", description = "User management"))
//! )]
//! pub struct UserApi;
//! ```
//!
//! # Module Structure
//!
//! | Module | Purpose |
//! |--------|---------|
//! | [`info`] | API metadata (title, version, contact, license) |
//! | [`paths`] | CRUD operation paths with parameters and responses |
//! | [`schemas`] | DTO schemas and common types (ErrorResponse) |
//! | [`security`] | Authentication schemes (bearer, cookie, api_key) |
//!
//! # Swagger UI Integration
//!
//! The generated `{Entity}Api` struct can be served via utoipa-swagger-ui:
//!
//! ```rust,ignore
//! use utoipa::OpenApi;
//! use utoipa_swagger_ui::SwaggerUi;
//!
//! let app = Router::new()
//! .merge(SwaggerUi::new("/swagger-ui")
//! .url("/api-docs/openapi.json", UserApi::openapi()));
//! ```
//!
//! # Conditional Generation
//!
//! OpenAPI struct is only generated when either:
//! - CRUD handlers are enabled via `api(handlers)` or `api(handlers(...))`
//! - Custom commands are defined via `#[command(...)]`
//!
//! If neither is present, `generate()` returns an empty `TokenStream`.
use TokenStream;
use ;
pub use ;
pub use ;
use crateEntityDef;
/// Generates the complete OpenAPI documentation struct with modifier.
///
/// This is the main entry point for OpenAPI generation. It produces:
///
/// 1. A modifier struct implementing `utoipa::Modify`
/// 2. An API struct deriving `utoipa::OpenApi`
///
/// # Arguments
///
/// * `entity` - The parsed entity definition containing API configuration
///
/// # Returns
///
/// A `TokenStream` containing both the modifier and API structs, or an empty
/// stream if no handlers or commands are configured.
///
/// # Generation Flow
///
/// ```text
/// EntityDef
/// │
/// ├─► has_crud? ────────────────────────────────────────┐
/// │ │ │
/// ├─► has_commands? ────────────────────────────────────┤
/// │ │
/// │ Neither? ─► Return empty TokenStream │
/// │ │
/// └───────────────────────────────────────────────────────┘
/// │
/// ▼
/// ┌─────────────────────┐
/// │ Generate components │
/// │ - schema_types │
/// │ - modifier_impl │
/// │ - api_struct │
/// └─────────────────────┘
/// ```
///
/// # Generated Components
///
/// | Component | Naming | Purpose |
/// |-----------|--------|---------|
/// | Modifier | `{Entity}ApiModifier` | Runtime OpenAPI customization |
/// | API struct | `{Entity}Api` | Main OpenAPI entry point |
/// | Tag | Configured or entity name | API grouping in Swagger UI |
///
/// # Example Output
///
/// For `User` entity with all handlers enabled:
///
/// ```rust,ignore
/// struct UserApiModifier;
/// impl utoipa::Modify for UserApiModifier { ... }
///
/// #[derive(utoipa::OpenApi)]
/// #[openapi(
/// components(schemas(UserResponse, CreateUserRequest, UpdateUserRequest)),
/// modifiers(&UserApiModifier),
/// tags((name = "Users", description = "User management"))
/// )]
/// pub struct UserApi;
/// ```
/// Generates the modifier struct with `utoipa::Modify` implementation.
///
/// The modifier pattern allows runtime customization of the OpenAPI spec
/// that cannot be expressed through derive macros alone. This includes:
///
/// - Dynamic security scheme configuration
/// - Additional schemas not derived from struct definitions
/// - Path operations with complex parameter types
/// - API info metadata (title, version, contact)
///
/// # Arguments
///
/// * `entity` - The parsed entity definition
/// * `modifier_name` - The identifier for the modifier struct
///
/// # Returns
///
/// A `TokenStream` containing:
/// - The modifier struct definition
/// - The `impl utoipa::Modify` block
///
/// # Modifier Responsibilities
///
/// ```text
/// ┌────────────────────────────────────────────────────────────┐
/// │ {Entity}ApiModifier::modify() │
/// ├────────────────────────────────────────────────────────────┤
/// │ │
/// │ 1. Info Configuration │
/// │ ├─► title, version, description │
/// │ ├─► license (name, URL) │
/// │ └─► contact (name, email, URL) │
/// │ │
/// │ 2. Security Schemes │
/// │ ├─► Bearer JWT (Authorization header) │
/// │ ├─► Cookie authentication (HTTP-only cookie) │
/// │ └─► API Key (X-API-Key header) │
/// │ │
/// │ 3. Common Schemas │
/// │ ├─► ErrorResponse (RFC 7807 Problem Details) │
/// │ └─► PaginationQuery (limit, offset) │
/// │ │
/// │ 4. CRUD Paths │
/// │ ├─► POST /entities (create) │
/// │ ├─► GET /entities (list) │
/// │ ├─► GET /entities/{id} (get) │
/// │ ├─► PATCH /entities/{id} (update) │
/// │ └─► DELETE /entities/{id} (delete) │
/// │ │
/// └────────────────────────────────────────────────────────────┘
/// ```
///
/// # Generated Structure
///
/// ```rust,ignore
/// /// OpenAPI modifier for User entity.
/// struct UserApiModifier;
///
/// impl utoipa::Modify for UserApiModifier {
/// fn modify(&self, openapi: &mut utoipa::openapi::OpenApi) {
/// use utoipa::openapi::*;
///
/// // Info configuration code
/// // Security scheme code
/// // Common schemas code
/// // CRUD paths code
/// }
/// }
/// ```