Skip to content

Commit

Permalink
Add vendored Swagger UI for utoipa (#941)
Browse files Browse the repository at this point in the history
Add vendored Swagger UI for utoipa. This can be enabled with `vendored`
crate feature flag of `utoipa-swagger-ui` crate.
  • Loading branch information
juhaku committed May 22, 2024
1 parent 360e23e commit c63407d
Show file tree
Hide file tree
Showing 17 changed files with 238 additions and 81 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
crate:
- utoipa
- utoipa-gen
- utoipa-swagger-ui-vendored
- utoipa-swagger-ui
- utoipa-redoc
- utoipa-rapidoc
Expand Down Expand Up @@ -49,6 +50,8 @@ jobs:
while read -r change; do
if [[ "$change" == "utoipa-gen" && "${{ matrix.crate }}" == "utoipa-gen" && $changes == false ]]; then
changes=true
elif [[ "$change" == "utoipa-swagger-ui-vendored" && "${{ matrix.crate }}" == "utoipa-swagger-ui-vendored" && $changes == false ]]; then
changes=true
elif [[ "$change" == "utoipa-swagger-ui" && "${{ matrix.crate }}" == "utoipa-swagger-ui" && $changes == false ]]; then
changes=true
elif [[ "$change" == "utoipa" && "${{ matrix.crate }}" == "utoipa" && $changes == false ]]; then
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
crate:
- utoipa
- utoipa-gen
- utoipa-swagger-ui-vendored
- utoipa-swagger-ui
- utoipa-redoc
- utoipa-rapidoc
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ resolver = "2"
members = [
"utoipa",
"utoipa-gen",
"utoipa-swagger-ui-vendored",
"utoipa-swagger-ui",
"utoipa-redoc",
"utoipa-rapidoc",
Expand All @@ -16,6 +17,7 @@ members = [
order = [
"utoipa-gen",
"utoipa",
"utoipa-swagger-ui-vendored",
"utoipa-swagger-ui",
"utoipa-redoc",
"utoipa-rapidoc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ hyper = { version = "1.0.1", features = ["full"] }
tokio = { version = "1.17", features = ["full"] }
tower = "0.4"
utoipa = { path = "../../utoipa", features = ["axum_extras"] }
utoipa-swagger-ui = { path = "../../utoipa-swagger-ui", features = ["axum"] }
utoipa-swagger-ui = { path = "../../utoipa-swagger-ui", features = ["axum", "vendored"], default-features = false }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
env_logger = "0.11"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
This example demonstrates `axum` with programmatic and macro based nesting of OpenApis
using `utoipa-swagger-ui` for visualization.

Example uses `utoipa-swagger-ui-vendored` to demonstrate vendored version of Swagger UI.

Just run command below to run the demo application and browse to `http://localhost:8080/swagger-ui/`.

```bash
Expand Down
File renamed without changes.
23 changes: 23 additions & 0 deletions scripts/update-swagger-ui.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
#
# Update Swagger UI version

set -eu -o pipefail

version="${1:-""}"
if [ -z "$version" ]; then
echo "Missing 'version' argument from commmand, run as $0 <version>" >&2 && exit 1
fi
zip_name="v$version.zip"

curl -sSL -o "$zip_name" "https://github.com/swagger-api/swagger-ui/archive/refs/tags/v$version.zip"

echo "Update vendored Swagger UI"
mv "$zip_name" ./utoipa-swagger-ui-vendored/res/
sed -i "s|version: \`.*\`|version: \`$version\`|" ./utoipa-swagger-ui-vendored/README.md
sed -i "s|version: \`.*\`|version: \`$version\`|" ./utoipa-swagger-ui-vendored/src/lib.rs

echo "Update utoipa-swagger-ui Swagger UI version"
sed -i "s|tags/v.*>|tags/v$version.zip>|" ./utoipa-swagger-ui/README.md
sed -i "s|tags/v.*>|tags/v$version.zip>|" ./utoipa-swagger-ui/src/lib.rs
sed -i "s|tags/v.*\.zip|tags/v$version.zip|" ./utoipa-swagger-ui/build.rs
16 changes: 16 additions & 0 deletions utoipa-swagger-ui-vendored/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "utoipa-swagger-ui-vendored"
description = "Vendored Swagger UI for utoipa"
license = "MIT OR Apache-2.0"
readme = "README.md"
version = "0.1.0"
edition = "2021"
keywords = ["swagger-ui", "vendored", "openapi", "documentation"]
repository = "https://github.com/juhaku/utoipa"
categories = ["web-programming"]
authors = ["Juha Kukkonen <juha7kukkonen@gmail.com>"]
rust-version.workspace = true

exclude = ["res"]

[dependencies]
1 change: 1 addition & 0 deletions utoipa-swagger-ui-vendored/LICENSE-APACHE
1 change: 1 addition & 0 deletions utoipa-swagger-ui-vendored/LICENSE-MIT
22 changes: 22 additions & 0 deletions utoipa-swagger-ui-vendored/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# utoipa-swagger-ui-vendored

[![Utoipa build](https://github.com/juhaku/utoipa/actions/workflows/build.yaml/badge.svg)](https://github.com/juhaku/utoipa/actions/workflows/build.yaml)
[![crates.io](https://img.shields.io/crates/v/utoipa-swagger-ui-vendored.svg?label=crates.io&color=orange&logo=rust)](https://crates.io/crates/utoipa-swagger-ui-vendored)
[![docs.rs](https://img.shields.io/static/v1?label=docs.rs&message=utoipa-swagger-ui-vendored&color=blue&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K)](https://docs.rs/utoipa-swagger-ui-vendored/latest/utoipa_swagger_ui-vendored/)
![rustc](https://img.shields.io/static/v1?label=rustc&message=1.75&color=orange&logo=rust)

This crate holds the [Swagger UI](https://github.com/swagger-api/swagger-ui) zip archive re-packaged as
Rust crate. The crate serves as a build dependency for `utoipa-swagger-ui` and is used to serve the
Swagger UI when `vendored` crate feature is enabled for `utoipa-swagger-ui` crate.

Vendored Swagger UI provides the means to serve Swagger UI in sandboxed environments where network access or
even other means to provide Swagger UI is not possible.

**Swagger UI version: `5.17.12`**

## License

Licensed under either of [Apache 2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT) license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate
by you, shall be dual licensed, without any additional terms or conditions.
Binary file added utoipa-swagger-ui-vendored/res/v5.17.12.zip
Binary file not shown.
19 changes: 19 additions & 0 deletions utoipa-swagger-ui-vendored/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! This crate holds the [Swagger UI](https://github.com/swagger-api/swagger-ui) zip archive re-packaged as
//! Rust crate. The crate serves as a build dependency for `utoipa-swagger-ui` and is used to serve the
//! Swagger UI when `vendored` crate feature is enabled for `utoipa-swagger-ui` crate.
//!
//! Vendored Swagger UI provides the means to serve Swagger UI in sandboxed environments where network access or
//! even other means to provide Swagger UI is not possible.
//!
//! **Swagger UI version: `5.17.12`**
//!
//! ## License
//!
//! Licensed under either of [Apache 2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT) license at your option.
//!
//! Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate
//! by you, shall be dual licensed, without any additional terms or conditions.

/// Swagger UI zip bytes
#[doc(hidden)]
pub const SWAGGER_UI_VENDORED: &[u8] = std::include_bytes!("../res/v5.17.12.zip");
8 changes: 6 additions & 2 deletions utoipa-swagger-ui/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "utoipa-swagger-ui"
description = "Swagger UI for utoipa"
version = "7.0.3"
version = "7.0.4"
edition = "2021"
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand All @@ -17,6 +17,7 @@ debug = []
debug-embed = ["rust-embed/debug-embed"]
reqwest = ["dep:reqwest"]
url = ["dep:url"]
vendored = ["dep:utoipa-swagger-ui-vendored"]

[dependencies]
rust-embed = { version = "8" }
Expand All @@ -34,7 +35,8 @@ serde_json = { version = "1.0" }
similar = "2.5"

[package.metadata.docs.rs]
features = ["actix-web", "axum", "rocket"]
features = ["actix-web", "axum", "rocket", "vendored"]
no-default-features = true
rustdoc-args = ["--cfg", "doc_cfg"]

[build-dependencies]
Expand All @@ -48,10 +50,12 @@ reqwest = { version = "0.12", features = [
"rustls-tls",
], default-features = false, optional = true }
url = { version = "2.5", optional = true }
utoipa-swagger-ui-vendored = { version = "*", path = "../utoipa-swagger-ui-vendored", optional = true }

# Windows is forced to use reqwest to download the Swagger UI.
[target.'cfg(windows)'.build-dependencies]
reqwest = { version = "0.12", features = [
"blocking",
"rustls-tls",
], default-features = false }
utoipa-swagger-ui-vendored = { version = "*", path = "../utoipa-swagger-ui-vendored", optional = true }
5 changes: 3 additions & 2 deletions utoipa-swagger-ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Utoipa build](https://github.com/juhaku/utoipa/actions/workflows/build.yaml/badge.svg)](https://github.com/juhaku/utoipa/actions/workflows/build.yaml)
[![crates.io](https://img.shields.io/crates/v/utoipa-swagger-ui.svg?label=crates.io&color=orange&logo=rust)](https://crates.io/crates/utoipa-swagger-ui)
[![docs.rs](https://img.shields.io/static/v1?label=docs.rs&message=utoipa-swagger-ui&color=blue&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K)](https://docs.rs/utoipa-swagger-ui/latest/utoipa_swagger_ui/)
![rustc](https://img.shields.io/static/v1?label=rustc&message=1.60%2B&color=orange&logo=rust)
![rustc](https://img.shields.io/static/v1?label=rustc&message=1.75&color=orange&logo=rust)

This crate implements necessary boilerplate code to serve Swagger UI via web server. It
works as a bridge for serving the OpenAPI documentation created with
Expand Down Expand Up @@ -33,6 +33,7 @@ more details at [serve](https://docs.rs/utoipa-swagger-ui/latest/utoipa_swagger_
* **reqwest** Use `reqwest` for downloading Swagger UI accoring to the `SWAGGER_UI_DOWNLOAD_URL` environment
variable. This is only enabled by default on _Windows_.
* **url** Enabled by default for parsing and encoding the download URL.
* **vendored** Enables vendored Swagger UI via `utoipa-swagger-ui-vendored` crate.

## Install

Expand Down Expand Up @@ -66,7 +67,7 @@ The following configuration env variables are available at build time:

* the url from where to download the swagger-ui zip file if starts with `http://` or `https://`
* the file path from where to copy the swagger-ui zip file if starts with `file://`
* default value: <https://github.com/swagger-api/swagger-ui/archive/refs/tags/v5.17.3.zip>
* default value: <https://github.com/swagger-api/swagger-ui/archive/refs/tags/v5.17.12.zip>
* all versions: <https://github.com/swagger-api/swagger-ui/tags>

* `SWAGGER_UI_OVERWRITE_FOLDER`:
Expand Down
Loading

0 comments on commit c63407d

Please sign in to comment.