Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion utoipa-scalar/res/scalar.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!doctype html>
<html>
<head>
<title>Scalar</title>
<title>$title</title>
<meta charset="utf-8"/>
<meta
name="viewport"
Expand Down
26 changes: 23 additions & 3 deletions utoipa-scalar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@
//! * [Configuration options][configuration]
//! * [Themes][themes]
//!
//! The HTML template must contain **`$spec`** variable which will be overridden during
//! [`Scalar::to_html`] execution.
//! The HTML template supports the following variables which will be replaced during
//! [`Scalar::to_html`] execution:
//!
//! * **`$spec`** Will be the [`Spec`] that will be rendered via [Scalar][scalar].
//! * **`$title`** Will be the page title configured via [`Scalar::title`].
//!
//! _**Overriding the HTML template with a custom one.**_
//! ```rust
Expand Down Expand Up @@ -187,6 +188,7 @@ impl<S: Spec> Servable<S> for Scalar<S> {
html: Cow::Borrowed(DEFAULT_HTML),
url: url.into(),
openapi,
title: Cow::Borrowed("Scalar"),
}
}
}
Expand All @@ -205,6 +207,7 @@ pub struct Scalar<S: Spec> {
url: Cow<'static, str>,
html: Cow<'static, str>,
openapi: S,
title: Cow<'static, str>,
}

impl<S: Spec> Scalar<S> {
Expand All @@ -223,6 +226,7 @@ impl<S: Spec> Scalar<S> {
html: Cow::Borrowed(DEFAULT_HTML),
url: Cow::Borrowed("/"),
openapi,
title: Cow::Borrowed("Scalar"),
}
}

Expand All @@ -235,7 +239,7 @@ impl<S: Spec> Scalar<S> {
/// At this point in time, it is not possible to customize the HTML template used by the
/// [`Scalar`] instance.
pub fn to_html(&self) -> String {
self.html.replace(
self.html.replace("$title", &self.title).replace(
"$spec",
&serde_json::to_string(&self.openapi).expect(
"Invalid OpenAPI spec, expected OpenApi, String, &str or serde_json::Value",
Expand All @@ -253,6 +257,22 @@ impl<S: Spec> Scalar<S> {

self
}

/// Set a custom title for the HTML page.
///
/// # Examples
///
/// _**Set custom title for [`Scalar`].**_
/// ```
/// # use utoipa_scalar::Scalar;
/// # use serde_json::json;
/// Scalar::new(json!({"openapi": "3.1.0"})).title("My API");
/// ```
pub fn title<T: Into<Cow<'static, str>>>(mut self, title: T) -> Self {
self.title = title.into();

self
}
}

/// Trait defines OpenAPI spec resource types supported by [`Scalar`].
Expand Down