Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make layout configurable between Standalone and Base #233

Merged
merged 5 commits into from
Jul 26, 2022
Merged
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
3 changes: 2 additions & 1 deletion utoipa-swagger-ui/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ fn replace_default_url_with_config(target_dir: &str) {
.iter()
.collect::<PathBuf>();

let swagger_initializer = fs::read_to_string(&path).unwrap();
let mut swagger_initializer = fs::read_to_string(&path).unwrap();
swagger_initializer = swagger_initializer.replace("layout: \"StandaloneLayout\"", "");

let replaced_swagger_initializer = RE.replace(&swagger_initializer, "{{config}},");

Expand Down
41 changes: 34 additions & 7 deletions utoipa-swagger-ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,10 @@ impl<'a> From<Cow<'static, str>> for Url<'a> {
}
}

pub const SWAGGER_STANDALONE_LAYOUT: &str = "StandaloneLayout";
pub const SWAGGER_BASE_LAYOUT: &str = "BaseLayout";
fn swagger_default_layout() -> &'static str { SWAGGER_STANDALONE_LAYOUT }

/// Object used to alter Swagger UI settings.
///
/// Config struct provides [Swagger UI configuration](https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md)
Expand Down Expand Up @@ -539,6 +543,10 @@ pub struct Config<'a> {
/// [`oauth::Config`] the Swagger UI is using for auth flow.
#[serde(skip)]
oauth: Option<oauth::Config>,

/// [ layout ] the layout of Swagger UI uses, default is "StandaloneLayout"
#[serde(default = "swagger_default_layout")]
layout: &'a str,
}

impl<'a> Config<'a> {
Expand All @@ -553,6 +561,7 @@ impl<'a> Config<'a> {
oauth: oauth_config,
deep_linking: Some(true),
dom_id: Some("#swagger-ui".to_string()),
layout: SWAGGER_STANDALONE_LAYOUT,
..if urls_len == 1 {
Self::new_config_with_single_url(urls)
} else {
Expand Down Expand Up @@ -758,6 +767,24 @@ impl<'a> Config<'a> {
self
}

/// Set 'layout' to 'BaseLayout' to only use the base swagger layout without a search header.
///
/// Default value is 'StandaloneLayout'.
///
/// # Examples
///
/// Configure Swagger to use Base Layout instead of Standalone
/// ```rust
/// # use utoipa_swagger_ui::Config;
/// let config = Config::new(["/api-doc/openapi.json"])
/// .use_base_layout();
/// ```
pub fn use_base_layout(mut self) -> Self {
self.layout = SWAGGER_BASE_LAYOUT;

self
}

/// Add default models expansion depth.
///
/// Setting this to `-1` will completely hide the models.
Expand Down Expand Up @@ -1234,7 +1261,6 @@ window.ui = SwaggerUIBundle({
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});"#;

#[test]
Expand All @@ -1252,14 +1278,14 @@ window.ui = SwaggerUIBundle({
"dom_id": "#swagger-ui",
"url": "/api-doc/openapi1.json",
"deepLinking": true,
"layout": "StandaloneLayout",
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});"###;

assert_diff_equal(EXPECTED, &formatted_config)
Expand All @@ -1285,14 +1311,14 @@ window.ui = SwaggerUIBundle({
}
],
"deepLinking": true,
"layout": "StandaloneLayout",
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});"###;

assert_diff_equal(EXPECTED, &formatted_config);
Expand Down Expand Up @@ -1323,14 +1349,14 @@ window.ui = SwaggerUIBundle({
}
],
"deepLinking": true,
"layout": "StandaloneLayout",
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});"###;

assert_diff_equal(EXPECTED, &formatted_config);
Expand Down Expand Up @@ -1364,14 +1390,14 @@ window.ui = SwaggerUIBundle({
}
],
"deepLinking": true,
"layout": "StandaloneLayout",
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});"###;

assert_diff_equal(EXPECTED, &formatted_config);
Expand Down Expand Up @@ -1401,14 +1427,14 @@ window.ui = SwaggerUIBundle({
}
],
"deepLinking": true,
"layout": "StandaloneLayout",
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});"###;

assert_diff_equal(EXPECTED, &formatted_config);
Expand All @@ -1426,6 +1452,7 @@ window.ui = SwaggerUIBundle({
.display_operation_id(true)
.display_request_duration(true)
.filter(true)
.use_base_layout()
.doc_expansion(r#"["list"*]"#)
.max_displayed_tags(1)
.oauth2_redirect_url("http://auth")
Expand Down Expand Up @@ -1471,14 +1498,14 @@ window.ui = SwaggerUIBundle({
"validatorUrl": "none",
"withCredentials": true,
"persistAuthorization": true,
"layout": "BaseLayout",
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});"###;

assert_diff_equal(EXPECTED, &formatted_config);
Expand Down