utoipa-redoc 6.0.0

Redoc for utoipa
Documentation
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#![warn(missing_docs)]
#![warn(rustdoc::broken_intra_doc_links)]
#![cfg_attr(doc_cfg, feature(doc_cfg))]
//! This crate works as a bridge between [utoipa](https://docs.rs/utoipa/latest/utoipa/) and [Redoc](https://redocly.com/) OpenAPI visualizer.
//!
//! Utoipa-redoc provides simple mechanism to transform OpenAPI spec resource to a servable HTML
//! file which can be served via [predefined framework integration][Self#examples] or used
//! [standalone][Self#using-standalone] and served manually.
//!
//! You may find fullsize examples from utoipa's Github [repository][examples].
//!
//! # Crate Features
//!
//! * **actix-web** Allows serving [`Redoc`] via _**`actix-web`**_.
//! * **rocket** Allows serving [`Redoc`] via _**`rocket`**_.
//! * **axum** Allows serving [`Redoc`] via _**`axum`**_.
//!
//! # Install
//!
//! Use Redoc only without any boiler plate implementation.
//! ```toml
//! [dependencies]
//! utoipa-redoc = "6"
//! ```
//!
//! Enable actix-web integration with Redoc.
//! ```toml
//! [dependencies]
//! utoipa-redoc = { version = "6", features = ["actix-web"] }
//! ```
//!
//! # Using standalone
//!
//! Utoipa-redoc can be used standalone as simply as creating a new [`Redoc`] instance and then
//! serving it by what ever means available as `text/html` from http handler in your favourite web
//! framework.
//!
//! [`Redoc::to_html`] method can be used to convert the [`Redoc`] instance to a servable html
//! file.
//! ```
//! # use utoipa_redoc::Redoc;
//! # use utoipa::OpenApi;
//! # use serde_json::json;
//! # #[derive(OpenApi)]
//! # #[openapi()]
//! # struct ApiDoc;
//! #
//! let redoc = Redoc::new(ApiDoc::openapi());
//!
//! // Then somewhere in your application that handles http operation.
//! // Make sure you return correct content type `text/html`.
//! let redoc_handler = move || {
//!     redoc.to_html()
//! };
//! ```
//!
//! # Customization
//!
//! Utoipa-redoc enables full customization support for [Redoc][redoc] according to what can be
//! customized by modifying the HTML template and [configuration options][Self#configuration].
//!
//! The default [HTML template][redoc_html_quickstart] can be fully overridden to ones liking with
//! [`Redoc::custom_html`] method. The HTML template **must** contain **`$spec`** and **`$config`**
//! variables which are replaced during [`Redoc::to_html`] execution.
//!
//! * **`$spec`** Will be the [`Spec`] that will be rendered via [Redoc][redoc].
//! * **`$config`** Will be the current [`Config`]. By default this is [`EmptyConfig`].
//!
//! _**Overriding the HTML template with a custom one.**_
//! ```rust
//! # use utoipa_redoc::Redoc;
//! # use utoipa::OpenApi;
//! # use serde_json::json;
//! # #[derive(OpenApi)]
//! # #[openapi()]
//! # struct ApiDoc;
//! #
//! let html = "...";
//! Redoc::new(ApiDoc::openapi()).custom_html(html);
//! ```
//!
//! # Configuration
//!
//! Redoc can be configured with JSON either inlined with the [`Redoc`] declaration or loaded from
//! user defined file with [`FileConfig`].
//!
//! * [All supported Redoc configuration options][redoc_config].
//!
//! _**Inlining the configuration.**_
//! ```rust
//! # use utoipa_redoc::Redoc;
//! # use utoipa::OpenApi;
//! # use serde_json::json;
//! # #[derive(OpenApi)]
//! # #[openapi()]
//! # struct ApiDoc;
//! #
//! Redoc::with_config(ApiDoc::openapi(), || json!({ "disableSearch": true }));
//! ```
//!
//! _**Using [`FileConfig`].**_
//! ```no_run
//! # use utoipa_redoc::{Redoc, FileConfig};
//! # use utoipa::OpenApi;
//! # use serde_json::json;
//! # #[derive(OpenApi)]
//! # #[openapi()]
//! # struct ApiDoc;
//! #
//! Redoc::with_config(ApiDoc::openapi(), FileConfig);
//! ```
//!
//! Read more details in [`Config`].
//!
//! # Examples
//!
//! _**Serve [`Redoc`] via `actix-web` framework.**_
//! ```no_run
//! use actix_web::App;
//! use utoipa_redoc::{Redoc, Servable};
//!
//! # use utoipa::OpenApi;
//! # use std::net::Ipv4Addr;
//! # #[derive(OpenApi)]
//! # #[openapi()]
//! # struct ApiDoc;
//! App::new().service(Redoc::with_url("/redoc", ApiDoc::openapi()));
//! ```
//!
//! _**Serve [`Redoc`] via `rocket` framework.**_
//! ```no_run
//! # use rocket;
//! use utoipa_redoc::{Redoc, Servable};
//!
//! # use utoipa::OpenApi;
//! # #[derive(OpenApi)]
//! # #[openapi()]
//! # struct ApiDoc;
//! rocket::build()
//!     .mount(
//!         "/",
//!         Redoc::with_url("/redoc", ApiDoc::openapi()),
//!     );
//! ```
//!
//! _**Serve [`Redoc`] via `axum` framework.**_
//!  ```no_run
//!  use axum::Router;
//!  use utoipa_redoc::{Redoc, Servable};
//!  # use utoipa::OpenApi;
//! # #[derive(OpenApi)]
//! # #[openapi()]
//! # struct ApiDoc;
//! #
//! # fn inner<S>()
//! # where
//! #     S: Clone + Send + Sync + 'static,
//! # {
//!
//!  let app = Router::<S>::new()
//!      .merge(Redoc::with_url("/redoc", ApiDoc::openapi()));
//! # }
//! ```
//!
//! _**Use [`Redoc`] to serve OpenAPI spec from url.**_
//! ```
//! # use utoipa_redoc::Redoc;
//! Redoc::new(
//!   "https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml");
//! ```
//!
//! _**Use [`Redoc`] to serve custom OpenAPI spec using serde's `json!()` macro.**_
//! ```rust
//! # use utoipa_redoc::Redoc;
//! # use serde_json::json;
//! Redoc::new(json!({"openapi": "3.1.0"}));
//! ```
//!
//! [redoc]: <https://redocly.com/>
//! [redoc_html_quickstart]: <https://redocly.com/docs/redoc/quickstart/>
//! [redoc_config]: <https://redocly.com/docs/api-reference-docs/configuration/functionality/#configuration-options-for-api-docs>
//! [examples]: <https://github.com/juhaku/utoipa/tree/master/examples>

use std::fs::OpenOptions;
use std::{borrow::Cow, env};

use serde::Serialize;
use serde_json::{json, Value};
use utoipa::openapi::OpenApi;

mod actix;
mod axum;
mod rocket;

const DEFAULT_HTML: &str = include_str!("../res/redoc.html");

/// Trait makes [`Redoc`] to accept an _`URL`_ the [Redoc][redoc] will be served via predefined web
/// server.
///
/// This is used **only** with **`actix-web`**, **`rocket`** or **`axum`** since they have implicit
/// implementation for serving the [`Redoc`] via the _`URL`_.
///
/// [redoc]: <https://redocly.com/>
#[cfg(any(feature = "actix-web", feature = "rocket", feature = "axum"))]
#[cfg_attr(
    doc_cfg,
    doc(cfg(any(feature = "actix-web", feature = "rocket", feature = "axum")))
)]
pub trait Servable<S>
where
    S: Spec,
{
    /// Construct a new [`Servable`] instance of _`openapi`_ with given _`url`_.
    ///
    /// * **url** Must point to location where the [`Servable`] is served.
    /// * **openapi** Is [`Spec`] that is served via this [`Servable`] from the _**url**_.
    fn with_url<U: Into<Cow<'static, str>>>(url: U, openapi: S) -> Self;

    /// Construct a new [`Servable`] instance of _`openapi`_ with given _`url`_ and _`config`_.
    ///
    /// * **url** Must point to location where the [`Servable`] is served.
    /// * **openapi** Is [`Spec`] that is served via this [`Servable`] from the _**url**_.
    /// * **config** Is custom [`Config`] that is used to configure the [`Servable`].
    fn with_url_and_config<U: Into<Cow<'static, str>>, C: Config>(
        url: U,
        openapi: S,
        config: C,
    ) -> Self;
}

#[cfg(any(feature = "actix-web", feature = "rocket", feature = "axum"))]
impl<S: Spec> Servable<S> for Redoc<S> {
    fn with_url<U: Into<Cow<'static, str>>>(url: U, openapi: S) -> Self {
        Self::with_url_and_config(url, openapi, EmptyConfig)
    }

    fn with_url_and_config<U: Into<Cow<'static, str>>, C: Config>(
        url: U,
        openapi: S,
        config: C,
    ) -> Self {
        Self {
            url: url.into(),
            html: Cow::Borrowed(DEFAULT_HTML),
            openapi,
            config: config.load(),
        }
    }
}

/// Is standalone instance of [Redoc UI][redoc].
///
/// This can be used together with predefined web framework integration or standalone with
/// framework of your choice. [`Redoc::to_html`] method will convert this [`Redoc`] instance to
/// servable HTML file.
///
/// [redoc]: <https://redocly.com/>
#[non_exhaustive]
#[derive(Clone)]
pub struct Redoc<S: Spec> {
    #[allow(unused)]
    url: Cow<'static, str>,
    html: Cow<'static, str>,
    openapi: S,
    config: Value,
}

impl<S: Spec> Redoc<S> {
    /// Constructs a new [`Redoc`] instance for given _`openapi`_ [`Spec`].
    ///
    /// This will create [`Redoc`] with [`EmptyConfig`].
    ///
    /// # Examples
    ///
    /// _**Create new [`Redoc`] instance with [`EmptyConfig`].**_
    /// ```
    /// # use utoipa_redoc::Redoc;
    /// # use serde_json::json;
    /// Redoc::new(json!({"openapi": "3.1.0"}));
    /// ```
    pub fn new(openapi: S) -> Self {
        Self::with_config(openapi, EmptyConfig)
    }

    /// Constructs a new [`Redoc`] instance for given _`openapi`_ [`Spec`] and _`config`_ [`Config`] of choice.
    ///
    /// # Examples
    ///
    /// _**Create new [`Redoc`] instance with [`FileConfig`].**_
    /// ```no_run
    /// # use utoipa_redoc::{Redoc, FileConfig};
    /// # use serde_json::json;
    /// Redoc::with_config(json!({"openapi": "3.1.0"}), FileConfig);
    /// ```
    pub fn with_config<C: Config>(openapi: S, config: C) -> Self {
        Self {
            html: Cow::Borrowed(DEFAULT_HTML),
            url: Cow::Borrowed(""),
            openapi,
            config: config.load(),
        }
    }

    /// Override the [default HTML template][redoc_html_quickstart] with new one. See
    /// [customization] for more details.
    ///
    /// [redoc_html_quickstart]: <https://redocly.com/docs/redoc/quickstart/>
    /// [customization]: index.html#customization
    pub fn custom_html<H: Into<Cow<'static, str>>>(mut self, html: H) -> Self {
        self.html = html.into();

        self
    }

    /// Converts this [`Redoc`] instance to servable HTML file.
    ///
    /// This will replace _**`$config`**_ variable placeholder with [`Config`] of this instance and
    /// _**`$spec`**_ with [`Spec`] provided to this instance serializing it to JSON from the HTML
    /// template used with the [`Redoc`]. If HTML template is not overridden with
    /// [`Redoc::custom_html`] then the [default HTML template][redoc_html_quickstart] will be used.
    ///
    /// See more details in [customization][customization].
    ///
    /// [redoc_html_quickstart]: <https://redocly.com/docs/redoc/quickstart/>
    /// [customization]: index.html#customization
    pub fn to_html(&self) -> String {
        self.html
            .replace("$config", &self.config.to_string())
            .replace(
                "$spec",
                &serde_json::to_string(&self.openapi).expect(
                    "Invalid OpenAPI spec, expected OpenApi, String, &str or serde_json::Value",
                ),
            )
    }
}

/// Trait defines OpenAPI spec resource types supported by [`Redoc`].
///
/// By default this trait is implemented for [`utoipa::openapi::OpenApi`], [`String`], [`&str`] and
/// [`serde_json::Value`].
///
/// * **OpenApi** implementation allows using utoipa's OpenApi struct as a OpenAPI spec resource
///   for the [`Redoc`].
/// * **String** and **&str** implementations allows defining HTTP URL for [`Redoc`] to load the
///   OpenAPI spec from.
/// * **Value** implementation enables the use of arbitrary JSON values with serde's `json!()`
///   macro as a OpenAPI spec for the [`Redoc`].
///
/// # Examples
///
/// _**Use [`Redoc`] to serve utoipa's OpenApi.**_
/// ```no_run
/// # use utoipa_redoc::Redoc;
/// # use utoipa::openapi::OpenApiBuilder;
/// #
/// Redoc::new(OpenApiBuilder::new().build());
/// ```
///
/// _**Use [`Redoc`] to serve OpenAPI spec from url.**_
/// ```
/// # use utoipa_redoc::Redoc;
/// Redoc::new(
///   "https://github.com/swagger-api/swagger-petstore/blob/master/src/main/resources/openapi.yaml");
/// ```
///
/// _**Use [`Redoc`] to serve custom OpenAPI spec using serde's `json!()` macro.**_
/// ```rust
/// # use utoipa_redoc::Redoc;
/// # use serde_json::json;
/// Redoc::new(json!({"openapi": "3.1.0"}));
/// ```
pub trait Spec: Serialize {}

impl Spec for OpenApi {}

impl Spec for String {}

impl Spec for &str {}

impl Spec for Value {}

/// Trait defines configuration options for [`Redoc`].
///
/// There are 3 configuration methods [`EmptyConfig`], [`FileConfig`] and [`FnOnce`] closure
/// config. The [`Config`] must be able to load and serialize valid JSON.
///
/// * **EmptyConfig** is the default config and serializes to empty JSON object _`{}`_.
/// * **FileConfig** Allows [`Redoc`] to be configured via user defined file which serializes to
///   JSON.
/// * **FnOnce** closure config allows inlining JSON serializable config directly to [`Redoc`]
///   declaration.
///
/// Configuration format and allowed options can be found from Redocly's own API documentation.
///
/// * [All supported Redoc configuration options][redoc_config].
///
/// **Note!** There is no validity check for configuration options and all options provided are
/// serialized as is to the [Redoc][redoc]. It is users own responsibility to check for possible
/// misspelled configuration options against the valid configuration options.
///
/// # Examples
///
/// _**Using [`FnOnce`] closure config.**_
/// ```rust
/// # use utoipa_redoc::Redoc;
/// # use utoipa::OpenApi;
/// # use serde_json::json;
/// # #[derive(OpenApi)]
/// # #[openapi()]
/// # struct ApiDoc;
/// #
/// Redoc::with_config(ApiDoc::openapi(), || json!({ "disableSearch": true }));
/// ```
///
/// _**Using [`FileConfig`].**_
/// ```no_run
/// # use utoipa_redoc::{Redoc, FileConfig};
/// # use utoipa::OpenApi;
/// # use serde_json::json;
/// # #[derive(OpenApi)]
/// # #[openapi()]
/// # struct ApiDoc;
/// #
/// Redoc::with_config(ApiDoc::openapi(), FileConfig);
/// ```
///
/// [redoc]: <https://redocly.com/>
/// [redoc_config]: <https://redocly.com/docs/api-reference-docs/configuration/functionality/#configuration-options-for-api-docs>
pub trait Config {
    /// Implementor must implement the logic which loads the configuration of choice and converts it
    /// to serde's [`serde_json::Value`].
    fn load(self) -> Value;
}

impl<S: Serialize, F: FnOnce() -> S> Config for F {
    fn load(self) -> Value {
        json!(self())
    }
}

/// Makes [`Redoc`] load it's configuration from a user defined file.
///
/// The config file must be defined via _**`UTOIPA_REDOC_CONFIG_FILE`**_ env variable for your
/// application. It can either be defined in runtime before the [`Redoc`] declaration or before
/// application startup or at compile time via `build.rs` file.
///
/// The file must be located relative to your application runtime directory.
///
/// The file must be loadable via [`Config`] and it must return a JSON object representing the
/// [Redoc configuration][redoc_config].
///
/// # Examples
///
/// _**Using a `build.rs` file to define the config file.**_
/// ```rust
/// # fn main() {
/// println!("cargo:rustc-env=UTOIPA_REDOC_CONFIG_FILE=redoc.config.json");
/// # }
/// ```
///
/// _**Defining config file at application startup.**_
/// ```bash
/// UTOIPA_REDOC_CONFIG_FILE=redoc.config.json cargo run
/// ```
///
/// [redoc_config]: <https://redocly.com/docs/api-reference-docs/configuration/functionality/#configuration-options-for-api-docs>
pub struct FileConfig;

impl Config for FileConfig {
    fn load(self) -> Value {
        let path = env::var("UTOIPA_REDOC_CONFIG_FILE")
            .expect("Missing `UTOIPA_REDOC_CONFIG_FILE` env variable, cannot load file config.");

        let file = OpenOptions::new()
            .read(true)
            .open(&path)
            .unwrap_or_else(|_| panic!("File `{path}` is not readable or does not exist."));
        serde_json::from_reader(file).expect("Config file cannot be parsed to JSON")
    }
}

/// Is the default configuration and serializes to empty JSON object _`{}`_.
pub struct EmptyConfig;

impl Config for EmptyConfig {
    fn load(self) -> Value {
        json!({})
    }
}