salvo_oapi/swagger_ui/config.rs
1use std::borrow::Cow;
2
3use serde::Serialize;
4
5use super::oauth;
6use crate::swagger_ui::Url;
7
8const SWAGGER_STANDALONE_LAYOUT: &str = "StandaloneLayout";
9const SWAGGER_BASE_LAYOUT: &str = "BaseLayout";
10
11/// Object used to alter Swagger UI settings.
12///
13/// Config struct provides [Swagger UI configuration](https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md)
14/// for settings which could be altered with **docker variables**.
15///
16/// # Examples
17///
18/// In simple case create config directly from url that points to the api doc json.
19/// ```rust
20/// # use salvo_oapi::swagger_ui::Config;
21/// let config = Config::from("/api-doc.json");
22/// ```
23///
24/// If there is multiple api docs to serve config can be also directly created with [`Config::new`]
25/// ```rust
26/// # use salvo_oapi::swagger_ui::Config;
27/// let config = Config::new(["/api-docs/openapi1.json", "/api-docs/openapi2.json"]);
28/// ```
29///
30/// Or same as above but more verbose syntax.
31/// ```rust
32/// # use salvo_oapi::swagger_ui::{Config, Url};
33/// let config = Config::new([
34/// Url::new("api1", "/api-docs/openapi1.json"),
35/// Url::new("api2", "/api-docs/openapi2.json")
36/// ]);
37/// ```
38///
39/// With oauth config.
40/// ```rust
41/// # use salvo_oapi::swagger_ui::{Config, oauth};
42/// let config = Config::with_oauth_config(
43/// ["/api-docs/openapi1.json", "/api-docs/openapi2.json"],
44/// oauth::Config::new(),
45/// );
46/// ```
47#[non_exhaustive]
48#[derive(Serialize, Clone, Debug)]
49#[serde(rename_all = "camelCase")]
50pub struct Config<'a> {
51 /// Url to fetch external configuration from.
52 #[serde(skip_serializing_if = "Option::is_none")]
53 pub(crate) config_url: Option<String>,
54
55 /// Id of the DOM element where `Swagger UI` will put it's user interface.
56 #[serde(skip_serializing_if = "Option::is_none")]
57 #[serde(rename = "dom_id")]
58 pub(crate) dom_id: Option<String>,
59
60 /// [`Url`] the Swagger UI is serving.
61 #[serde(skip_serializing_if = "Option::is_none")]
62 pub(crate) url: Option<String>,
63
64 /// Name of the primary url if any.
65 #[serde(skip_serializing_if = "Option::is_none", rename = "urls.primaryName")]
66 pub(crate) urls_primary_name: Option<String>,
67
68 /// [`Url`]s the Swagger UI is serving.
69 #[serde(skip_serializing_if = "Vec::is_empty")]
70 pub(crate) urls: Vec<Url<'a>>,
71
72 /// Enables overriding configuration parameters with url query parameters.
73 #[serde(skip_serializing_if = "Option::is_none")]
74 pub(crate) query_config_enabled: Option<bool>,
75
76 /// Controls whether [deep linking](https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/deep-linking.md)
77 /// is enabled in OpenAPI spec.
78 ///
79 /// Deep linking automatically scrolls and expands UI to given url fragment.
80 #[serde(skip_serializing_if = "Option::is_none")]
81 pub(crate) deep_linking: Option<bool>,
82
83 /// Controls whether operation id is shown in the operation list.
84 #[serde(skip_serializing_if = "Option::is_none")]
85 pub(crate) display_operation_id: Option<bool>,
86
87 /// Default models expansion depth; -1 will completely hide the models.
88 #[serde(skip_serializing_if = "Option::is_none")]
89 pub(crate) default_models_expand_depth: Option<isize>,
90
91 /// Default model expansion depth from model example section.
92 #[serde(skip_serializing_if = "Option::is_none")]
93 pub(crate) default_model_expand_depth: Option<isize>,
94
95 /// Defines how models is show when API is first rendered.
96 #[serde(skip_serializing_if = "Option::is_none")]
97 pub(crate) default_model_rendering: Option<String>,
98
99 /// Define whether request duration in milliseconds is displayed for "Try it out" requests.
100 #[serde(skip_serializing_if = "Option::is_none")]
101 pub(crate) display_request_duration: Option<bool>,
102
103 /// Controls default expansion for operations and tags.
104 #[serde(skip_serializing_if = "Option::is_none")]
105 pub(crate) doc_expansion: Option<String>,
106
107 /// Defines is filtering of tagged operations allowed with edit box in top bar.
108 #[serde(skip_serializing_if = "Option::is_none")]
109 pub(crate) filter: Option<bool>,
110
111 /// Controls how many tagged operations are shown. By default all operations are shown.
112 #[serde(skip_serializing_if = "Option::is_none")]
113 pub(crate) max_displayed_tags: Option<usize>,
114
115 /// Defines whether extensions are shown.
116 #[serde(skip_serializing_if = "Option::is_none")]
117 pub(crate) show_extensions: Option<bool>,
118
119 /// Defines whether common extensions are shown.
120 #[serde(skip_serializing_if = "Option::is_none")]
121 pub(crate) show_common_extensions: Option<bool>,
122
123 /// Defines whether "Try it out" section should be enabled by default.
124 #[serde(skip_serializing_if = "Option::is_none")]
125 pub(crate) try_it_out_enabled: Option<bool>,
126
127 /// Defines whether request snippets section is enabled. If disabled legacy curl snipped
128 /// will be used.
129 #[serde(skip_serializing_if = "Option::is_none")]
130 pub(crate) request_snippets_enabled: Option<bool>,
131
132 /// Oauth redirect url.
133 #[serde(skip_serializing_if = "Option::is_none")]
134 pub(crate) oauth2_redirect_url: Option<String>,
135
136 /// Defines whether request mutated with `requestInterceptor` will be used to produce curl command
137 /// in the UI.
138 #[serde(skip_serializing_if = "Option::is_none")]
139 pub(crate) show_mutated_request: Option<bool>,
140
141 /// Define supported http request submit methods.
142 #[serde(skip_serializing_if = "Option::is_none")]
143 pub(crate) supported_submit_methods: Option<Vec<String>>,
144
145 /// Define validator url which is used to validate the Swagger spec. By default the validator swagger.io's
146 /// online validator is used. Setting this to none will disable spec validation.
147 #[serde(skip_serializing_if = "Option::is_none")]
148 pub(crate) validator_url: Option<String>,
149
150 /// Enables passing credentials to CORS requests as defined
151 /// [fetch standards](https://fetch.spec.whatwg.org/#credentials).
152 #[serde(skip_serializing_if = "Option::is_none")]
153 pub(crate) with_credentials: Option<bool>,
154
155 /// Defines whether authorizations is persisted throughout browser refresh and close.
156 #[serde(skip_serializing_if = "Option::is_none")]
157 pub(crate) persist_authorization: Option<bool>,
158
159 /// [`oauth::Config`] the Swagger UI is using for auth flow.
160 #[serde(skip)]
161 pub(crate) oauth: Option<oauth::Config>,
162
163 /// The layout of Swagger UI uses, default is `"StandaloneLayout"`
164 pub(crate) layout: &'a str,
165}
166
167impl<'a> Config<'a> {
168 fn new_<I: IntoIterator<Item = U>, U: Into<Url<'a>>>(urls: I, oauth_config: Option<oauth::Config>) -> Self {
169 let urls = urls.into_iter().map(Into::into).collect::<Vec<Url<'a>>>();
170 let urls_len = urls.len();
171
172 Self {
173 oauth: oauth_config,
174 ..if urls_len == 1 {
175 Self::new_config_with_single_url(urls)
176 } else {
177 Self::new_config_with_multiple_urls(urls)
178 }
179 }
180 }
181
182 fn new_config_with_multiple_urls(urls: Vec<Url<'a>>) -> Self {
183 let primary_name = urls.iter().find(|url| url.primary).map(|url| url.name.to_string());
184
185 Self {
186 urls_primary_name: primary_name,
187 urls: urls
188 .into_iter()
189 .map(|mut url| {
190 if url.name.is_empty() {
191 url.name = Cow::Owned(String::from(&url.url[..]));
192
193 url
194 } else {
195 url
196 }
197 })
198 .collect(),
199 ..Default::default()
200 }
201 }
202
203 fn new_config_with_single_url(mut urls: Vec<Url<'a>>) -> Self {
204 let url = urls.get_mut(0).map(std::mem::take).expect("urls should not be empty");
205 let primary_name = if url.primary { Some(url.name.to_string()) } else { None };
206
207 Self {
208 urls_primary_name: primary_name,
209 url: if url.name.is_empty() {
210 Some(url.url.to_string())
211 } else {
212 None
213 },
214 urls: if !url.name.is_empty() { vec![url] } else { Vec::new() },
215 ..Default::default()
216 }
217 }
218
219 /// Constructs a new [`Config`] from [`Iterator`] of [`Url`]s.
220 ///
221 /// [`Url`]s provided to the [`Config`] will only change the urls Swagger UI is going to use to
222 /// fetch the API document.
223 ///
224 /// # Examples
225 /// Create new config with 2 api doc urls.
226 /// ```rust
227 /// # use salvo_oapi::swagger_ui::Config;
228 /// let config = Config::new(["/api-docs/openapi1.json", "/api-docs/openapi2.json"]);
229 /// ```
230 pub fn new<I: IntoIterator<Item = U>, U: Into<Url<'a>>>(urls: I) -> Self {
231 Self::new_(urls, None)
232 }
233
234 /// Constructs a new [`Config`] from [`Iterator`] of [`Url`]s.
235 ///
236 /// # Examples
237 /// Create new config with oauth config.
238 /// ```rust
239 /// # use salvo_oapi::swagger_ui::{Config, oauth};
240 /// let config = Config::with_oauth_config(
241 /// ["/api-docs/openapi1.json", "/api-docs/openapi2.json"],
242 /// oauth::Config::new(),
243 /// );
244 /// ```
245 pub fn with_oauth_config<I: IntoIterator<Item = U>, U: Into<Url<'a>>>(
246 urls: I,
247 oauth_config: oauth::Config,
248 ) -> Self {
249 Self::new_(urls, Some(oauth_config))
250 }
251
252 /// Add url to fetch external configuration from.
253 ///
254 /// # Examples
255 ///
256 /// Set external config url.
257 /// ```rust
258 /// # use salvo_oapi::swagger_ui::Config;
259 /// let config = Config::new(["/api-docs/openapi.json"])
260 /// .config_url("http://url.to.external.config");
261 /// ```
262 pub fn config_url<S: Into<String>>(mut self, config_url: S) -> Self {
263 self.config_url = Some(config_url.into());
264
265 self
266 }
267
268 /// Add id of the DOM element where `Swagger UI` will put it's user interface.
269 ///
270 /// The default value is `#swagger-ui`.
271 ///
272 /// # Examples
273 ///
274 /// Set custom dom id where the Swagger UI will place it's content.
275 /// ```rust
276 /// # use salvo_oapi::swagger_ui::Config;
277 /// let config = Config::new(["/api-docs/openapi.json"]).dom_id("#my-id");
278 /// ```
279 pub fn dom_id<S: Into<String>>(mut self, dom_id: S) -> Self {
280 self.dom_id = Some(dom_id.into());
281
282 self
283 }
284
285 /// Set `query_config_enabled` to allow overriding configuration parameters via url `query`
286 /// parameters.
287 ///
288 /// Default value is `false`.
289 ///
290 /// # Examples
291 ///
292 /// Enable query config.
293 /// ```rust
294 /// # use salvo_oapi::swagger_ui::Config;
295 /// let config = Config::new(["/api-docs/openapi.json"])
296 /// .query_config_enabled(true);
297 /// ```
298 pub fn query_config_enabled(mut self, query_config_enabled: bool) -> Self {
299 self.query_config_enabled = Some(query_config_enabled);
300
301 self
302 }
303
304 /// Set `deep_linking` to allow deep linking tags and operations.
305 ///
306 /// Deep linking will automatically scroll to and expand operation when Swagger UI is
307 /// given corresponding url fragment. See more at
308 /// [deep linking docs](https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/deep-linking.md).
309 ///
310 /// Deep linking is enabled by default.
311 ///
312 /// # Examples
313 ///
314 /// Disable the deep linking.
315 /// ```rust
316 /// # use salvo_oapi::swagger_ui::Config;
317 /// let config = Config::new(["/api-docs/openapi.json"])
318 /// .deep_linking(false);
319 /// ```
320 pub fn deep_linking(mut self, deep_linking: bool) -> Self {
321 self.deep_linking = Some(deep_linking);
322
323 self
324 }
325
326 /// Set `display_operation_id` to `true` to show operation id in the operations list.
327 ///
328 /// Default value is `false`.
329 ///
330 /// # Examples
331 ///
332 /// Allow operation id to be shown.
333 /// ```rust
334 /// # use salvo_oapi::swagger_ui::Config;
335 /// let config = Config::new(["/api-docs/openapi.json"])
336 /// .display_operation_id(true);
337 /// ```
338 pub fn display_operation_id(mut self, display_operation_id: bool) -> Self {
339 self.display_operation_id = Some(display_operation_id);
340
341 self
342 }
343
344 /// Set 'layout' to 'BaseLayout' to only use the base swagger layout without a search header.
345 ///
346 /// Default value is 'StandaloneLayout'.
347 ///
348 /// # Examples
349 ///
350 /// Configure Swagger to use Base Layout instead of Standalone
351 /// ```rust
352 /// # use salvo_oapi::swagger_ui::Config;
353 /// let config = Config::new(["/api-docs/openapi.json"])
354 /// .use_base_layout();
355 /// ```
356 pub fn use_base_layout(mut self) -> Self {
357 self.layout = SWAGGER_BASE_LAYOUT;
358
359 self
360 }
361
362 /// Add default models expansion depth.
363 ///
364 /// Setting this to `-1` will completely hide the models.
365 ///
366 /// # Examples
367 ///
368 /// Hide all the models.
369 /// ```rust
370 /// # use salvo_oapi::swagger_ui::Config;
371 /// let config = Config::new(["/api-docs/openapi.json"])
372 /// .default_models_expand_depth(-1);
373 /// ```
374 pub fn default_models_expand_depth(mut self, default_models_expand_depth: isize) -> Self {
375 self.default_models_expand_depth = Some(default_models_expand_depth);
376
377 self
378 }
379
380 /// Add default model expansion depth for model on the example section.
381 ///
382 /// # Examples
383 ///
384 /// ```rust
385 /// # use salvo_oapi::swagger_ui::Config;
386 /// let config = Config::new(["/api-docs/openapi.json"])
387 /// .default_model_expand_depth(1);
388 /// ```
389 pub fn default_model_expand_depth(mut self, default_model_expand_depth: isize) -> Self {
390 self.default_model_expand_depth = Some(default_model_expand_depth);
391
392 self
393 }
394
395 /// Add `default_model_rendering` to set how models is show when API is first rendered.
396 ///
397 /// The user can always switch the rendering for given model by clicking the `Model` and `Example Value` links.
398 ///
399 /// * `example` Makes example rendered first by default.
400 /// * `model` Makes model rendered first by default.
401 ///
402 /// # Examples
403 ///
404 /// ```rust
405 /// # use salvo_oapi::swagger_ui::Config;
406 /// let config = Config::new(["/api-docs/openapi.json"])
407 /// .default_model_rendering(r#"["example"*, "model"]"#);
408 /// ```
409 pub fn default_model_rendering<S: Into<String>>(mut self, default_model_rendering: S) -> Self {
410 self.default_model_rendering = Some(default_model_rendering.into());
411
412 self
413 }
414
415 /// Set to `true` to show request duration of _**'Try it out'**_ requests _**(in milliseconds)**_.
416 ///
417 /// Default value is `false`.
418 ///
419 /// # Examples
420 /// Enable request duration of the _**'Try it out'**_ requests.
421 /// ```rust
422 /// # use salvo_oapi::swagger_ui::Config;
423 /// let config = Config::new(["/api-docs/openapi.json"])
424 /// .display_request_duration(true);
425 /// ```
426 pub fn display_request_duration(mut self, display_request_duration: bool) -> Self {
427 self.display_request_duration = Some(display_request_duration);
428
429 self
430 }
431
432 /// Add `doc_expansion` to control default expansion for operations and tags.
433 ///
434 /// * `list` Will expand only tags.
435 /// * `full` Will expand tags and operations.
436 /// * `none` Will expand nothing.
437 ///
438 /// # Examples
439 ///
440 /// ```rust
441 /// # use salvo_oapi::swagger_ui::Config;
442 /// let config = Config::new(["/api-docs/openapi.json"])
443 /// .doc_expansion(r#"["list"*, "full", "none"]"#);
444 /// ```
445 pub fn doc_expansion<S: Into<String>>(mut self, doc_expansion: S) -> Self {
446 self.doc_expansion = Some(doc_expansion.into());
447
448 self
449 }
450
451 /// Add `filter` to allow filtering of tagged operations.
452 ///
453 /// When enabled top bar will show and edit box that can be used to filter visible tagged operations.
454 /// Filter behaves case sensitive manner and matches anywhere inside the tag.
455 ///
456 /// Default value is `false`.
457 ///
458 /// # Examples
459 ///
460 /// Enable filtering.
461 /// ```rust
462 /// # use salvo_oapi::swagger_ui::Config;
463 /// let config = Config::new(["/api-docs/openapi.json"])
464 /// .filter(true);
465 /// ```
466 pub fn filter(mut self, filter: bool) -> Self {
467 self.filter = Some(filter);
468
469 self
470 }
471
472 /// Add `max_displayed_tags` to restrict shown tagged operations.
473 ///
474 /// By default all operations are shown.
475 ///
476 /// # Examples
477 ///
478 /// Display only 4 operations.
479 /// ```rust
480 /// # use salvo_oapi::swagger_ui::Config;
481 /// let config = Config::new(["/api-docs/openapi.json"])
482 /// .max_displayed_tags(4);
483 /// ```
484 pub fn max_displayed_tags(mut self, max_displayed_tags: usize) -> Self {
485 self.max_displayed_tags = Some(max_displayed_tags);
486
487 self
488 }
489
490 /// Set `show_extensions` to adjust whether vendor extension _**`(x-)`**_ fields and values
491 /// are shown for operations, parameters, responses and schemas.
492 ///
493 /// # Example
494 ///
495 /// Show vendor extensions.
496 /// ```rust
497 /// # use salvo_oapi::swagger_ui::Config;
498 /// let config = Config::new(["/api-docs/openapi.json"])
499 /// .show_extensions(true);
500 /// ```
501 pub fn show_extensions(mut self, show_extensions: bool) -> Self {
502 self.show_extensions = Some(show_extensions);
503
504 self
505 }
506
507 /// Add `show_common_extensions` to define whether common extension
508 /// _**`(pattern, maxLength, minLength, maximum, minimum)`**_ fields and values are shown
509 /// for parameters.
510 ///
511 /// # Examples
512 ///
513 /// Show common extensions.
514 /// ```rust
515 /// # use salvo_oapi::swagger_ui::Config;
516 /// let config = Config::new(["/api-docs/openapi.json"])
517 /// .show_common_extensions(true);
518 /// ```
519 pub fn show_common_extensions(mut self, show_common_extensions: bool) -> Self {
520 self.show_common_extensions = Some(show_common_extensions);
521
522 self
523 }
524
525 /// Add `try_it_out_enabled` to enable _**'Try it out'**_ section by default.
526 ///
527 /// Default value is `false`.
528 ///
529 /// # Examples
530 ///
531 /// Enable _**'Try it out'**_ section by default.
532 /// ```rust
533 /// # use salvo_oapi::swagger_ui::Config;
534 /// let config = Config::new(["/api-docs/openapi.json"])
535 /// .try_it_out_enabled(true);
536 /// ```
537 pub fn try_it_out_enabled(mut self, try_it_out_enabled: bool) -> Self {
538 self.try_it_out_enabled = Some(try_it_out_enabled);
539
540 self
541 }
542
543 /// Set `request_snippets_enabled` to enable request snippets section.
544 ///
545 /// If disabled legacy curl snipped will be used.
546 ///
547 /// Default value is `false`.
548 ///
549 /// # Examples
550 ///
551 /// Enable request snippets section.
552 /// ```rust
553 /// # use salvo_oapi::swagger_ui::Config;
554 /// let config = Config::new(["/api-docs/openapi.json"])
555 /// .request_snippets_enabled(true);
556 /// ```
557 pub fn request_snippets_enabled(mut self, request_snippets_enabled: bool) -> Self {
558 self.request_snippets_enabled = Some(request_snippets_enabled);
559
560 self
561 }
562
563 /// Add oauth redirect url.
564 ///
565 /// # Examples
566 ///
567 /// Add oauth redirect url.
568 /// ```rust
569 /// # use salvo_oapi::swagger_ui::Config;
570 /// let config = Config::new(["/api-docs/openapi.json"])
571 /// .oauth2_redirect_url("http://my.oauth2.redirect.url");
572 /// ```
573 pub fn oauth2_redirect_url<S: Into<String>>(mut self, oauth2_redirect_url: S) -> Self {
574 self.oauth2_redirect_url = Some(oauth2_redirect_url.into());
575
576 self
577 }
578
579 /// Add `show_mutated_request` to use request returned from `requestInterceptor`
580 /// to produce curl command in the UI. If set to `false` the request before `requestInterceptor`
581 /// was applied will be used.
582 ///
583 /// # Examples
584 ///
585 /// Use request after `requestInterceptor` to produce the curl command.
586 /// ```rust
587 /// # use salvo_oapi::swagger_ui::Config;
588 /// let config = Config::new(["/api-docs/openapi.json"])
589 /// .show_mutated_request(true);
590 /// ```
591 pub fn show_mutated_request(mut self, show_mutated_request: bool) -> Self {
592 self.show_mutated_request = Some(show_mutated_request);
593
594 self
595 }
596
597 /// Add supported http methods for _**'Try it out'**_ operation.
598 ///
599 /// _**'Try it out'**_ will be enabled based on the given list of http methods when
600 /// the operation's http method is included within the list.
601 /// By giving an empty list will disable _**'Try it out'**_ from all operations but it will
602 /// **not** filter operations from the UI.
603 ///
604 /// By default all http operations are enabled.
605 ///
606 /// # Examples
607 ///
608 /// Set allowed http methods explicitly.
609 /// ```rust
610 /// # use salvo_oapi::swagger_ui::Config;
611 /// let config = Config::new(["/api-docs/openapi.json"])
612 /// .supported_submit_methods(["get", "put", "post", "delete", "options", "head", "patch", "trace"]);
613 /// ```
614 ///
615 /// Allow _**'Try it out'**_ for only GET operations.
616 /// ```rust
617 /// # use salvo_oapi::swagger_ui::Config;
618 /// let config = Config::new(["/api-docs/openapi.json"])
619 /// .supported_submit_methods(["get"]);
620 /// ```
621 pub fn supported_submit_methods<I: IntoIterator<Item = S>, S: Into<String>>(
622 mut self,
623 supported_submit_methods: I,
624 ) -> Self {
625 self.supported_submit_methods = Some(
626 supported_submit_methods
627 .into_iter()
628 .map(|method| method.into())
629 .collect(),
630 );
631
632 self
633 }
634
635 /// Add validator url which is used to validate the Swagger spec.
636 ///
637 /// This can also be set to use locally deployed validator for example see
638 /// [Validator Badge](https://github.com/swagger-api/validator-badge) for more details.
639 ///
640 /// By default swagger.io's online validator _**`(https://validator.swagger.io/validator)`**_ will be used.
641 /// Setting this to `none` will disable the validator.
642 ///
643 /// # Examples
644 ///
645 /// Disable the validator.
646 /// ```rust
647 /// # use salvo_oapi::swagger_ui::Config;
648 /// let config = Config::new(["/api-docs/openapi.json"])
649 /// .validator_url("none");
650 /// ```
651 pub fn validator_url<S: Into<String>>(mut self, validator_url: S) -> Self {
652 self.validator_url = Some(validator_url.into());
653
654 self
655 }
656
657 /// Set `with_credentials` to enable passing credentials to CORS requests send by browser as defined
658 /// [fetch standards](https://fetch.spec.whatwg.org/#credentials).
659 ///
660 /// **Note**: that Swagger UI cannot currently set cookies cross-domain
661 /// (see [swagger-js#1163](https://github.com/swagger-api/swagger-js/issues/1163)) -
662 /// as a result, you will have to rely on browser-supplied cookies (which this setting enables sending)
663 /// that Swagger UI cannot control.
664 ///
665 /// # Examples
666 ///
667 /// Enable passing credentials to CORS requests.
668 /// ```rust
669 /// # use salvo_oapi::swagger_ui::Config;
670 /// let config = Config::new(["/api-docs/openapi.json"])
671 /// .with_credentials(true);
672 /// ```
673 pub fn with_credentials(mut self, with_credentials: bool) -> Self {
674 self.with_credentials = Some(with_credentials);
675
676 self
677 }
678
679 /// Set to `true` to enable authorizations to be persisted throughout browser refresh and close.
680 ///
681 /// Default value is `false`.
682 ///
683 ///
684 /// # Examples
685 ///
686 /// Persists authorization throughout browser close and refresh.
687 /// ```rust
688 /// # use salvo_oapi::swagger_ui::Config;
689 /// let config = Config::new(["/api-docs/openapi.json"])
690 /// .persist_authorization(true);
691 /// ```
692 pub fn persist_authorization(mut self, persist_authorization: bool) -> Self {
693 self.persist_authorization = Some(persist_authorization);
694
695 self
696 }
697}
698
699impl Default for Config<'_> {
700 fn default() -> Self {
701 Self {
702 config_url: Default::default(),
703 dom_id: Some("#swagger-ui".to_string()),
704 url: Default::default(),
705 urls_primary_name: Default::default(),
706 urls: Default::default(),
707 query_config_enabled: Default::default(),
708 deep_linking: Some(true),
709 display_operation_id: Default::default(),
710 default_models_expand_depth: Default::default(),
711 default_model_expand_depth: Default::default(),
712 default_model_rendering: Default::default(),
713 display_request_duration: Default::default(),
714 doc_expansion: Default::default(),
715 filter: Default::default(),
716 max_displayed_tags: Default::default(),
717 show_extensions: Default::default(),
718 show_common_extensions: Default::default(),
719 try_it_out_enabled: Default::default(),
720 request_snippets_enabled: Default::default(),
721 oauth2_redirect_url: Default::default(),
722 show_mutated_request: Default::default(),
723 supported_submit_methods: Default::default(),
724 validator_url: Default::default(),
725 with_credentials: Default::default(),
726 persist_authorization: Default::default(),
727 oauth: Default::default(),
728 layout: SWAGGER_STANDALONE_LAYOUT,
729 }
730 }
731}
732
733impl<'a> From<&'a str> for Config<'a> {
734 fn from(s: &'a str) -> Self {
735 Self::new([s])
736 }
737}
738
739impl From<String> for Config<'_> {
740 fn from(s: String) -> Self {
741 Self::new([s])
742 }
743}