Skip to content

Commit a97234f

Browse files
committed
Fix definition override for scope's route
1 parent 079852e commit a97234f

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

apistos/src/internal/actix/scope.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ use actix_web::Error;
1010
use apistos_models::components::Components;
1111
use apistos_models::paths::PathItem;
1212
use indexmap::IndexMap;
13-
use std::collections::BTreeMap;
1413
use std::fmt::Debug;
1514
use std::future::Future;
1615

1716
pub struct Scope<S = actix_web::Scope> {
18-
pub(crate) item_map: BTreeMap<String, PathItem>,
17+
pub(crate) item_map: IndexMap<String, PathItem>,
1918
pub(crate) components: Vec<Components>,
2019
tags: Vec<String>,
2120
path: String,
@@ -175,9 +174,10 @@ where
175174
}
176175

177176
fn update_from_def_holder<D: DefinitionHolder>(&mut self, dh: &mut D) {
178-
self.components.extend(dh.components());
179-
let mut item_map = IndexMap::new();
177+
let mut item_map: IndexMap<String, PathItem> = IndexMap::new();
180178
dh.update_path_items(&mut item_map);
179+
180+
self.components.extend(dh.components());
181181
for (path, mut path_item) in item_map {
182182
let p = [self.path.clone(), path]
183183
.iter()
@@ -191,7 +191,9 @@ where
191191
operation.tags.append(&mut self.tags.clone());
192192
}
193193

194-
self.item_map.insert(p, path_item);
194+
let op_map = self.item_map.entry(p).or_default();
195+
//@todo we should probably merge the path items together instead but for now only operations can be defined using apistos here
196+
op_map.operations.extend(path_item.operations);
195197
}
196198
}
197199
}

apistos/tests/routing.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ use actix_web::middleware::Logger;
22
use actix_web::web::{Json, Path};
33
use actix_web::{App, Error};
44

5+
use actix_web::test::{call_service, init_service, try_read_body_json, TestRequest};
56
use apistos::app::OpenApiWrapper;
67
use apistos::spec::Spec;
78
use apistos::web::{delete, get, patch, post, put, resource, scope, tagged_resource, tagged_scope, ServiceConfig};
89
use apistos_gen::api_operation;
910
use apistos_models::info::Info;
1011
use apistos_models::tag::Tag;
12+
use apistos_models::OpenApi;
1113

1214
#[actix_web::test]
1315
async fn actix_routing() {
@@ -62,7 +64,8 @@ async fn actix_routing() {
6264
.route("test3", patch().to(test))
6365
.route("test4/{test_id}", patch().to(test))
6466
.app_data("")
65-
.configure(my_routes),
67+
.configure(my_routes)
68+
.service(scope("test5").route("", post().to(test)).route("", get().to(test))),
6669
)
6770
.build("/openapi.json");
6871
let app = init_service(app).await;
@@ -80,19 +83,20 @@ async fn actix_routing() {
8083
"/test/test2/",
8184
"/test/test3",
8285
"/test/test4/{test_id}",
86+
"/test/test5",
8387
"/test/users/{user_id}",
8488
"/test/{plop_id}/{clap_name}",
8589
];
8690

87-
assert_eq!(paths, expected_paths)
91+
assert_eq!(paths, expected_paths);
92+
93+
assert_eq!(body.paths.paths.values().flat_map(|v| v.operations.values()).count(), 8);
8894
}
8995

9096
// Imports bellow aim at making clippy happy. Those dependencies are necessary for integration-test.
9197
use actix_service as _;
92-
use actix_web::test::{call_service, init_service, try_read_body_json, TestRequest};
9398
use actix_web_lab as _;
9499
use apistos_core as _;
95-
use apistos_models::OpenApi;
96100
use apistos_plugins as _;
97101
use apistos_rapidoc as _;
98102
use apistos_redoc as _;

0 commit comments

Comments
 (0)