Skip to content
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
39 changes: 37 additions & 2 deletions apistos/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,20 @@ pub struct App<T> {
#[derive(Default)]
pub struct BuildConfig {
ui_plugin_configs: Vec<Box<dyn UIPluginConfig>>,
spec_path: Option<String>,
}

impl BuildConfig {
pub fn with<T: UIPluginConfig + 'static>(mut self, plugin: T) -> Self {
self.ui_plugin_configs.push(Box::new(plugin));
self
}

/// Override the openapi spec path
pub fn with_spec_path<T: Into<String>>(mut self, spec_path: T) -> Self {
self.spec_path = Some(spec_path.into());
self
}
}

impl<T> OpenApiWrapper<T> for actix_web::App<T> {
Expand Down Expand Up @@ -240,8 +247,10 @@ where

let mut actix_app = self.inner.expect("Missing app");

let spec_path = config.spec_path.as_ref().map_or(openapi_path, String::as_str);

for plugin in config.ui_plugin_configs {
actix_app = actix_app.service(UIPluginWrapper::from(plugin.build(openapi_path)))
actix_app = actix_app.service(UIPluginWrapper::from(plugin.build(spec_path)))
}

actix_app.service(resource(openapi_path).route(get().to(OASHandler::new(open_api_spec))))
Expand Down Expand Up @@ -365,7 +374,7 @@ mod test {
use crate::app::{BuildConfig, OpenApiWrapper, build_operation_id};
use crate::spec::Spec;
use actix_web::App;
use actix_web::test::{TestRequest, call_service, init_service, try_read_body_json};
use actix_web::test::{TestRequest, call_and_read_body, call_service, init_service, try_read_body_json};
use apistos_models::OpenApi;
use apistos_models::info::Info;
use apistos_models::paths::OperationType;
Expand Down Expand Up @@ -599,4 +608,30 @@ mod test {
let operation_id = build_operation_id("/api/v1/plip/{test_id}/test/", OperationType::Get);
assert_eq!(operation_id, "get_api-v1-plip-f5c9e39d7a1acb928c72745f3893bce8")
}

#[actix_web::test]
async fn test_prefixed_openapi_path() {
let openapi_path = "/test.json";
let rapidoc_path = "/rapidoc";
let spec_path = "/myservice/test.json";
let expected_spec_url = "spec-url=\"/myservice/test.json\"";

let app = App::new().document(Spec::default()).build_with(
openapi_path,
BuildConfig::default()
.with_spec_path(spec_path)
.with(RapidocConfig::new(&rapidoc_path)),
);
let app = init_service(app).await;

let req = TestRequest::get().uri(openapi_path).to_request();
let resp = call_service(&app, req).await;
assert!(resp.status().is_success());

let req = TestRequest::get().uri(rapidoc_path).to_request();
let body = call_and_read_body(&app, req).await;

let body = String::from_utf8(body.to_vec()).expect("Unable to convert body to string");
assert!(body.contains(expected_spec_url), "Body: {body}");
}
}
1 change: 1 addition & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ allow = [
"MIT",
"Apache-2.0",
"BSD-3-Clause",
"CDLA-Permissive-2.0",
"Unicode-DFS-2016",
"Unicode-3.0",
"ISC",
Expand Down