Skip to content

Commit

Permalink
Fix scope overriding same path operations (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlebran authored Aug 23, 2024
2 parents 4e37325 + b0a94b8 commit b2f5746
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ documentation = "https://docs.rs/apistos/"
license = "MIT OR Apache-2.0"
rust-version = "1.75"
publish = true
version = "0.3.5"
version = "0.3.6"

[workspace.dependencies]
actix-service = "2"
Expand Down
2 changes: 1 addition & 1 deletion apistos-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ serde_qs = { workspace = true, features = ["actix4"], optional = true }
uuid = { workspace = true, optional = true }
url = { workspace = true, optional = true }

apistos-models = { path = "../apistos-models", version = "0.3.5", features = ["deserialize"] }
apistos-models = { path = "../apistos-models", version = "0.3.6", features = ["deserialize"] }

[dev-dependencies]
assert-json-diff = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions apistos-gen-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ assert-json-diff = { workspace = true }
chrono = { workspace = true, features = ["serde"] }
futures-core = { workspace = true }
apistos = { path = "../apistos", features = ["multipart", "uuid"] }
apistos-core = { path = "../apistos-core", version = "0.3.5", features = ["actix-web-grants"] }
apistos-gen = { path = "../apistos-gen", version = "0.3.5" }
apistos-core = { path = "../apistos-core", version = "0.3.6", features = ["actix-web-grants"] }
apistos-gen = { path = "../apistos-gen", version = "0.3.6" }
# we use the "preserve_order" feature from schemars here following https://github.com/netwo-io/apistos/pull/78
schemars = { workspace = true, features = ["preserve_order"] }
serde = { workspace = true, features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion apistos-rapidoc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rust-version.workspace = true
license.workspace = true

[dependencies]
apistos-plugins = { path = "../apistos-plugins", version = "0.3.5" }
apistos-plugins = { path = "../apistos-plugins", version = "0.3.6" }

[lints]
workspace = true
2 changes: 1 addition & 1 deletion apistos-redoc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rust-version.workspace = true
license.workspace = true

[dependencies]
apistos-plugins = { path = "../apistos-plugins", version = "0.3.5" }
apistos-plugins = { path = "../apistos-plugins", version = "0.3.6" }

[lints]
workspace = true
2 changes: 1 addition & 1 deletion apistos-scalar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rust-version.workspace = true
license.workspace = true

[dependencies]
apistos-plugins = { path = "../apistos-plugins", version = "0.3.5" }
apistos-plugins = { path = "../apistos-plugins", version = "0.3.6" }

[lints]
workspace = true
2 changes: 1 addition & 1 deletion apistos-shuttle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ actix-web = { workspace = true }
num_cpus = { workspace = true }
shuttle-runtime = { workspace = true }

apistos = { path = "../apistos", version = "0.3.5" }
apistos = { path = "../apistos", version = "0.3.6" }

[lints]
workspace = true
2 changes: 1 addition & 1 deletion apistos-swagger-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rust-version.workspace = true
license.workspace = true

[dependencies]
apistos-plugins = { path = "../apistos-plugins", version = "0.3.5" }
apistos-plugins = { path = "../apistos-plugins", version = "0.3.6" }

[lints]
workspace = true
26 changes: 13 additions & 13 deletions apistos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ schemars = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }

apistos-core = { path = "../apistos-core", version = "0.3.5" }
apistos-gen = { path = "../apistos-gen", version = "0.3.5" }
apistos-models = { path = "../apistos-models", version = "0.3.5" }
apistos-plugins = { path = "../apistos-plugins", version = "0.3.5" }
apistos-rapidoc = { path = "../apistos-rapidoc", version = "0.3.5", optional = true }
apistos-redoc = { path = "../apistos-redoc", version = "0.3.5", optional = true }
apistos-scalar = { path = "../apistos-scalar", version = "0.3.5", optional = true }
apistos-swagger-ui = { path = "../apistos-swagger-ui", version = "0.3.5", optional = true }
apistos-core = { path = "../apistos-core", version = "0.3.6" }
apistos-gen = { path = "../apistos-gen", version = "0.3.6" }
apistos-models = { path = "../apistos-models", version = "0.3.6" }
apistos-plugins = { path = "../apistos-plugins", version = "0.3.6" }
apistos-rapidoc = { path = "../apistos-rapidoc", version = "0.3.6", optional = true }
apistos-redoc = { path = "../apistos-redoc", version = "0.3.6", optional = true }
apistos-scalar = { path = "../apistos-scalar", version = "0.3.6", optional = true }
apistos-swagger-ui = { path = "../apistos-swagger-ui", version = "0.3.6", optional = true }


[dev-dependencies]
actix-web-lab = { workspace = true }
garde-actix-web = { workspace = true }

apistos-models = { path = "../apistos-models", version = "0.3.5", features = ["deserialize"] }
apistos-rapidoc = { path = "../apistos-rapidoc", version = "0.3.5" }
apistos-redoc = { path = "../apistos-redoc", version = "0.3.5" }
apistos-scalar = { path = "../apistos-scalar", version = "0.3.5" }
apistos-swagger-ui = { path = "../apistos-swagger-ui", version = "0.3.5" }
apistos-models = { path = "../apistos-models", version = "0.3.6", features = ["deserialize"] }
apistos-rapidoc = { path = "../apistos-rapidoc", version = "0.3.6" }
apistos-redoc = { path = "../apistos-redoc", version = "0.3.6" }
apistos-scalar = { path = "../apistos-scalar", version = "0.3.6" }
apistos-swagger-ui = { path = "../apistos-swagger-ui", version = "0.3.6" }

[lints]
workspace = true
Expand Down
13 changes: 8 additions & 5 deletions apistos/src/internal/actix/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ use actix_web::Error;
use apistos_models::components::Components;
use apistos_models::paths::PathItem;
use indexmap::IndexMap;
use std::collections::BTreeMap;
use std::fmt::Debug;
use std::future::Future;

pub struct Scope<S = actix_web::Scope> {
pub(crate) item_map: BTreeMap<String, PathItem>,
pub(crate) item_map: IndexMap<String, PathItem>,
pub(crate) components: Vec<Components>,
tags: Vec<String>,
path: String,
Expand Down Expand Up @@ -175,9 +174,11 @@ where
}

fn update_from_def_holder<D: DefinitionHolder>(&mut self, dh: &mut D) {
self.components.extend(dh.components());
let mut item_map = IndexMap::new();
let mut item_map: IndexMap<String, PathItem> = IndexMap::new();
dh.update_path_items(&mut item_map);

self.components.extend(dh.components());

for (path, mut path_item) in item_map {
let p = [self.path.clone(), path]
.iter()
Expand All @@ -191,7 +192,9 @@ where
operation.tags.append(&mut self.tags.clone());
}

self.item_map.insert(p, path_item);
let op_map = self.item_map.entry(p).or_default();
//@todo we should probably merge the path items together instead but for now only operations can be defined using apistos here
op_map.operations.extend(path_item.operations);
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions apistos/tests/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ async fn actix_routing() {
.route("test3", patch().to(test))
.route("test4/{test_id}", patch().to(test))
.app_data("")
.configure(my_routes),
.configure(my_routes)
.service(scope("test5").route("", post().to(test)).route("", get().to(test)))
.service(resource("test6").route(post().to(test)))
.service(resource("test6").route(get().to(test))),
)
.build("/openapi.json");
let app = init_service(app).await;
Expand All @@ -80,11 +83,18 @@ async fn actix_routing() {
"/test/test2/",
"/test/test3",
"/test/test4/{test_id}",
"/test/test5",
"/test/test6",
"/test/users/{user_id}",
"/test/{plop_id}/{clap_name}",
];

assert_eq!(paths, expected_paths)
assert_eq!(paths, expected_paths);

assert_eq!(
body.paths.paths.values().flat_map(|v| v.operations.values()).count(),
10
);
}

// Imports bellow aim at making clippy happy. Those dependencies are necessary for integration-test.
Expand Down

0 comments on commit b2f5746

Please sign in to comment.