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
//! This projects serves to enable automatic rendering of `openapi.json` files, and provides
//! facilities to also serve the documentation alongside your api.
//!
//! # Usage
//! First, add the following lines to your `Cargo.toml`
//! ```toml
//! [dependencies]
//! rocket = { version = "0.5.1", default-features = false, features = ["json"] }
//! schemars = "0.8.16"
//! okapi = { version = "0.7.0" }
//! rocket_okapi = { version = "0.9.0", features = ["swagger"] }
//! ```
//! To add documentation to a set of endpoints, a couple of steps are required. The request and
//! response types of the endpoint must implement `JsonSchema`. Secondly, the function must be
//! marked with `#[openapi]`. After that, you can simply replace `routes!` with
//! `openapi_get_routes!`. This will append an additional route to the resulting `Vec<Route>`,
//! which contains the `openapi.json` file that is required by swagger. Now that we have the json
//! file that we need, we can serve the swagger web interface at another endpoint, and we should be
//! able to load the example in the browser!
//! ### Example
//! ```rust, no_run
//! use rocket::get;
//! use rocket::serde::json::Json;
//! use rocket_okapi::{openapi, openapi_get_routes, JsonSchema};
//! use rocket_okapi::swagger_ui::{make_swagger_ui, SwaggerUIConfig};
//!
//! #[derive(serde::Serialize, JsonSchema)]
//! struct Response {
//! reply: String,
//! }
//!
//! #[openapi]
//! #[get("/")]
//! fn my_controller() -> Json<Response> {
//! Json(Response {
//! reply: "show me the docs!".to_string(),
//! })
//! }
//!
//! fn get_docs() -> SwaggerUIConfig {
//! use rocket_okapi::settings::UrlObject;
//!
//! SwaggerUIConfig {
//! url: "/my_resource/openapi.json".to_string(),
//! ..Default::default()
//! }
//! }
//!
//! fn main() {
//! rocket::build()
//! .mount("/my_resource", openapi_get_routes![my_controller])
//! .mount("/swagger", make_swagger_ui(&get_docs()))
//! .launch();
//! }
//! ```
//!
//! This crate exposes a few macros that can be used to generate and serve routes and OpenApi objects.
//! - `mount_endpoints_and_merged_docs!{...}`: Mount endpoints and mount merged OpenAPI documentation.
//! - `openapi_get_routes![...]`: To generate and add the `openapi.json` route.
//! - `openapi_get_routes_spec![...]`: To generate and return a list of routes and the openapi spec.
//! - `openapi_get_spec![...]`: To generate and return the openapi spec.
//!
//! The last 3 macros have very similar behavior, but differ in what they return.
//! Here is a list of the marcos and what they return:
//! - `openapi_get_routes![...]`: `Vec<rocket::Route>` (adds route for `openapi.json`)
//! - `openapi_get_routes_spec![...]`: `(Vec<rocket::Route>, okapi::openapi3::OpenApi)`
//! - `openapi_get_spec![...]`: `okapi::openapi3::OpenApi`
//!
//! ## FAQ
//!
//! All FAQ questions and answers can be found in [README.md](https://github.com/GREsau/okapi/tree/master#faq).
/// Contains the `Generator` struct, which you can use to manually control the way a struct is
/// represented in the documentation.
/// Contains several `Rocket` `Handler`s, which are used for serving the json files and the swagger
/// interface.
/// Contains the functions and structs required to display the RapiDoc UI.
/// This module contains several traits that correspond to the `Rocket` traits pertaining to request
/// guards and responses
/// Contains the trait `OpenApiResponder`, meaning that a response implementing this trait can be
/// documented.
/// Contains then `OpenApiSettings` struct, which can be used to customize the behavior of a
/// `Generator`.
/// Contains the functions and structs required to display the Swagger UI.
/// Assorted function that are used throughout the application.
pub use *;
/// Re-export Okapi
pub use okapi;
pub use *;
pub use JsonSchema;
/// Contains information about an endpoint.
/// Convert OpenApi object to routable endpoint.
///
/// Used to serve an `OpenApi` object as an `openapi.json` file in Rocket.
/// Mount endpoints and mount merged OpenAPI documentation.
///
/// This marco just makes to code look cleaner and improves readability
/// for bigger codebases.
///
/// The macro expects the following arguments:
/// - rocket_builder: `Rocket<Build>`,
/// - base_path: `&str`, `String` or [`Uri`](rocket::http::uri::Uri). (Anything that implements `ToString`)
/// Anything accepted by [`mount()`](https://docs.rs/rocket/0.5.1/rocket/struct.Rocket.html#method.mount)
/// - openapi_settings: `OpenApiSettings` (use `OpenApiSettings::default()` if default settings are okay for you),
/// - List of (0 or more):
/// - path: `&str`, `String` or [`Uri`](rocket::http::uri::Uri).
/// Anything accepted by `mount()` (`base_path` should not be included).
/// - `=>`: divider
/// - route_and_docs: `(Vec<rocket::Route>, OpenApi)`
///
/// Example:
/// ```rust,ignore
/// let settings = OpenApiSettings::default();
/// let custom_route_spec = (vec![], custom_spec());
/// mount_endpoints_and_merged_docs! {
/// building_rocket, "/v1".to_owned(), settings,
/// "/" => custom_route_spec,
/// "/post" => post::get_routes_and_docs(),
/// "/message" => message::get_routes_and_docs(),
/// };
/// ```
///
/// Get and merge nested endpoints and OpenAPI documentation.
///
/// This macro enables to split endpoints definition in smaller pieces to make code look
/// cleaner and improves readability for bigger codebases.
///
/// The macro expects the following arguments:
/// - List of (0 or more):
/// - path: `&str`, `String` or [`Uri`](rocket::http::uri::Uri).
/// Anything accepted by `mount()` (`base_path` should not be included).
/// - `=>`: divider
/// - route_and_docs: `(Vec<rocket::Route>, OpenApi)`
///
/// Example:
/// ```rust,ignore
/// let settings = OpenApiSettings::default();
/// let custom_route_spec = (vec![], custom_spec());
/// mount_endpoints_and_merged_docs! {
/// building_rocket, "/v1".to_owned(), settings,
/// "/" => custom_route_spec,
/// "/api" => api::get_routes_and_docs(),
/// };
///
/// mod api {
/// pub fn get_routes_and_docs(settings: &OpenApiSettings) -> (Vec<rocket::Route>, OpenApi) {
/// get_nested_endpoints_and_docs! {
/// "/posts" => post::get_routes_and_docs(settings),
/// "/message" => message::get_routes_and_docs(settings),
/// }
/// }
/// mod posts {
/// pub fn get_routes_and_docs(settings: &OpenApiSettings) -> (Vec<rocket::Route>, OpenApi) {
/// openapi_get_routes_spec![settings: create_post, get_post]
/// }
/// }
/// mod messages {
/// pub fn get_routes_and_docs(settings: &OpenApiSettings) -> (Vec<rocket::Route>, OpenApi) {
/// openapi_get_routes_spec![settings: create_message, get_message]
/// }
/// }
/// }
/// ```
///
/// A replacement macro for `rocket::routes`. This also takes a optional settings object.
///
/// The key differences are that this macro will add an additional element to the
/// resulting `Vec<rocket::Route>`, which serves a static file called
/// `openapi.json`. This file can then be used to display the routes in the Swagger/RapiDoc UI.
///
/// Example:
/// ```rust,ignore
/// use okapi::openapi3::OpenApi;
/// let settings = rocket_okapi::settings::OpenApiSettings::new();
/// let routes: Vec<rocket::Route> =
/// openapi_get_routes![settings: create_message, get_message];
/// ```
/// Or
/// ```rust,ignore
/// use okapi::openapi3::OpenApi;
/// let routes: Vec<rocket::Route> =
/// openapi_get_routes![create_message, get_message];
/// ```
/// A replacement macro for `rocket::routes`. This parses the routes and provides
/// a tuple with 2 parts `(Vec<rocket::Route>, OpenApi)`:
/// - `Vec<rocket::Route>`: A list of all the routes that `rocket::routes![]` would have provided.
/// - `OpenApi`: The `okapi::openapi3::OpenApi` spec for all the routes.
///
/// NOTE: This marco is different from `openapi_get_routes` in that this does not add
/// the `openapi.json` file to the list of routes. This is done so the `OpenApi` spec can be changed
/// before serving it.
///
/// Example:
/// ```rust,ignore
/// use okapi::openapi3::OpenApi;
/// let settings = rocket_okapi::settings::OpenApiSettings::new();
/// let (routes, spec): (Vec<rocket::Route>, OpenApi) =
/// openapi_get_routes_spec![settings: create_message, get_message];
/// ```
/// Or
/// ```rust,ignore
/// use okapi::openapi3::OpenApi;
/// let (routes, spec): (Vec<rocket::Route>, OpenApi) =
/// openapi_get_routes_spec![create_message, get_message];
/// ```
/// Generate `OpenApi` spec only, does not generate routes.
/// This can be used in cases where you are only interested in the openAPI spec, but not in the routes.
/// A use case could be inside of `build.rs` scripts or where you want to alter OpenAPI object
/// at runtime.
///
/// Example:
/// ```rust,ignore
/// use okapi::openapi3::OpenApi;
/// let settings = rocket_okapi::settings::OpenApiSettings::new();
/// let spec: OpenApi = openapi_get_spec![settings: create_message, get_message];
/// ```
/// Or
/// ```rust,ignore
/// use okapi::openapi3::OpenApi;
/// let spec: OpenApi = openapi_get_spec![create_message, get_message];
/// ```