Skip to content

Commit

Permalink
Refactor ReDoc to take Cow<'static, str> instead of borrowed str (
Browse files Browse the repository at this point in the history
#869)

* Refactor RapiDoc to take `Cow<'static, str>` instead of borrowed `str`

Made the fields match the existing example in `utoipa_swagger_ui::SwaggerUi` (field `path`).

Fixes #805

* Refactor `ReDoc` to take `Cow<'static, str>` instead of borrowed `str`
  • Loading branch information
simongoricar authored Feb 19, 2024
1 parent 5c6b0e2 commit 7b74942
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
4 changes: 2 additions & 2 deletions utoipa-redoc/src/actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use actix_web::{HttpResponse, Resource, Responder};

use crate::{Redoc, Spec};

impl<'s, 'u, S: Spec> HttpServiceFactory for Redoc<'s, 'u, S> {
impl<S: Spec> HttpServiceFactory for Redoc<S> {
fn register(self, config: &mut actix_web::dev::AppService) {
let html = self.to_html();

Expand All @@ -17,7 +17,7 @@ impl<'s, 'u, S: Spec> HttpServiceFactory for Redoc<'s, 'u, S> {
.body(redoc.to_string())
}

Resource::new(self.url)
Resource::new(self.url.as_ref())
.guard(Get())
.app_data(Data::new(html))
.to(serve_redoc)
Expand Down
10 changes: 6 additions & 4 deletions utoipa-redoc/src/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use axum::{routing, Router};

use crate::{Redoc, Spec};

impl<'s, 'u, S: Spec, R> From<Redoc<'s, 'u, S>> for Router<R>
impl<S: Spec, R> From<Redoc<S>> for Router<R>
where
R: Clone + Send + Sync + 'static,
's: 'static,
{
fn from(value: Redoc<'s, 'u, S>) -> Self {
fn from(value: Redoc<S>) -> Self {
let html = value.to_html();
Router::<R>::new().route(value.url, routing::get(move || async { Html(html) }))
Router::<R>::new().route(
value.url.as_ref(),
routing::get(move || async { Html(html) }),
)
}
}
52 changes: 25 additions & 27 deletions utoipa-redoc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@
//! [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::env;
use std::fs::OpenOptions;
use std::{borrow::Cow, env};

use serde::Serialize;
use serde_json::{json, Value};
Expand All @@ -206,44 +206,42 @@ const DEFAULT_HTML: &str = include_str!("../res/redoc.html");
doc_cfg,
doc(cfg(any(feature = "actix-web", feature = "rocket", feature = "axum")))
)]
pub trait Servable<'u, 's, S>
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(url: &'u str, openapi: S) -> Self
where
'u: 's;
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<C: Config>(url: &'u str, openapi: S, config: C) -> Self
where
'u: 's;
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<'u, 's, S: Spec> Servable<'u, 's, S> for Redoc<'u, 's, S> {
fn with_url(url: &'u str, openapi: S) -> Self
where
'u: 's,
{
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<C: Config>(url: &'u str, openapi: S, config: C) -> Self
where
'u: 's,
{
fn with_url_and_config<U: Into<Cow<'static, str>>, C: Config>(
url: U,
openapi: S,
config: C,
) -> Self {
Self {
url,
html: DEFAULT_HTML,
url: url.into(),
html: Cow::Borrowed(DEFAULT_HTML),
openapi,
config: config.load(),
}
Expand All @@ -259,15 +257,15 @@ impl<'u, 's, S: Spec> Servable<'u, 's, S> for Redoc<'u, 's, S> {
/// [redoc]: <https://redocly.com/>
#[non_exhaustive]
#[derive(Clone)]
pub struct Redoc<'s, 'u, S: Spec> {
pub struct Redoc<S: Spec> {
#[allow(unused)]
url: &'u str,
html: &'s str,
url: Cow<'static, str>,
html: Cow<'static, str>,
openapi: S,
config: Value,
}

impl<'s, 'u, S: Spec> Redoc<'s, 'u, S> {
impl<S: Spec> Redoc<S> {
/// Constructs a new [`Redoc`] instance for given _`openapi`_ [`Spec`].
///
/// This will create [`Redoc`] with [`EmptyConfig`].
Expand Down Expand Up @@ -296,8 +294,8 @@ impl<'s, 'u, S: Spec> Redoc<'s, 'u, S> {
/// ```
pub fn with_config<C: Config>(openapi: S, config: C) -> Self {
Self {
html: DEFAULT_HTML,
url: "",
html: Cow::Borrowed(DEFAULT_HTML),
url: Cow::Borrowed(""),
openapi,
config: config.load(),
}
Expand All @@ -308,8 +306,8 @@ impl<'s, 'u, S: Spec> Redoc<'s, 'u, S> {
///
/// [redoc_html_quickstart]: <https://redocly.com/docs/redoc/quickstart/>
/// [customization]: index.html#customization
pub fn custom_html(mut self, html: &'s str) -> Self {
self.html = html;
pub fn custom_html<H: Into<Cow<'static, str>>>(mut self, html: H) -> Self {
self.html = html.into();

self
}
Expand Down
6 changes: 3 additions & 3 deletions utoipa-redoc/src/rocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use rocket::{Data, Request, Route};

use crate::{Redoc, Spec};

impl<'s, 'u, S: Spec> From<Redoc<'s, 'u, S>> for Vec<Route> {
fn from(value: Redoc<'s, 'u, S>) -> Self {
impl<S: Spec> From<Redoc<S>> for Vec<Route> {
fn from(value: Redoc<S>) -> Self {
vec![Route::new(
Method::Get,
value.url,
value.url.as_ref(),
RedocHandler(value.to_html()),
)]
}
Expand Down

0 comments on commit 7b74942

Please sign in to comment.