Skip to content

Commit

Permalink
Disable automatic parameter recognition (#696)
Browse files Browse the repository at this point in the history
Temporarily disable automatic parameter recognition. This is to mitigate
the current issues in `IntoParams` implementation furhter discussed in
issues #675 #677 #687. This commit restores the old behavior where the
parameters must be declared with `params(...)` attribute within the
`#[utoipa::path(...)]` attribute macro. The temporary disablement is
to allow furhter releases not be blocked by the changes in the master
branch.

The automatic parameter recognition still needs some work and most
likely another approach need to experiemented before it can be finally
completed.
  • Loading branch information
juhaku authored Jul 22, 2023
1 parent 8424b97 commit 73fd3ea
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 45 deletions.
22 changes: 11 additions & 11 deletions utoipa-gen/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,20 @@ impl<'p> PathAttr<'p> {
) {
let ext_params = ext_parameters.into_iter();

if self.params.is_empty() {
self.params = ext_params.collect();
} else {
let (existing_params, new_params): (Vec<Parameter>, Vec<Parameter>) =
ext_params.partition(|param| self.params.iter().any(|p| p == param));
let (existing_params, new_params): (Vec<Parameter>, Vec<Parameter>) =
ext_params.partition(|param| self.params.iter().any(|p| p == param));

for existing in existing_params {
if let Some(param) = self.params.iter_mut().find(|p| **p == existing) {
param.merge(existing);
}
for existing in existing_params {
if let Some(param) = self.params.iter_mut().find(|p| **p == existing) {
param.merge(existing);
}

self.params.extend(new_params.into_iter());
}

self.params.extend(
new_params
.into_iter()
.filter(|param| !matches!(param, Parameter::IntoParamsIdent(_))),
);
}
}

Expand Down
22 changes: 12 additions & 10 deletions utoipa-gen/tests/path_derive_actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,8 @@ fn path_with_all_args() {
status: String,
}

#[utoipa::path]
// NOTE! temporarily disable automatic parameter recognition
#[utoipa::path(params(Filter))]
#[post("/item/{id}/{name}")]
async fn post_item(
_path: Path<(i32, String)>,
Expand All @@ -813,34 +814,34 @@ fn path_with_all_args() {
&operation.pointer("/parameters").unwrap(),
json!([
{
"in": "path",
"name": "id",
"in": "query",
"name": "age",
"required": true,
"schema": {
"format": "int32",
"type": "integer"
}
},
{
"in": "path",
"name": "name",
"in": "query",
"name": "status",
"required": true,
"schema": {
"type": "string"
}
},
{
"in": "query",
"name": "age",
"in": "path",
"name": "id",
"required": true,
"schema": {
"format": "int32",
"type": "integer"
}
},
{
"in": "query",
"name": "status",
"in": "path",
"name": "name",
"required": true,
"schema": {
"type": "string"
Expand Down Expand Up @@ -958,7 +959,8 @@ fn path_with_all_args_using_custom_uuid() {
}
}

#[utoipa::path]
// NOTE! temporarily disable automatic parameter recognition
#[utoipa::path(params(Id))]
#[post("/item/{custom_uuid}")]
async fn post_item(_path: Path<Id>, _body: Json<Item>) -> Result<Json<Item>, Error> {
Ok(Json(Item(String::new())))
Expand Down
18 changes: 9 additions & 9 deletions utoipa-gen/tests/path_derive_axum_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ fn path_with_path_query_body_resolved() {
status: String,
}

#[utoipa::path(path = "/item/{id}/{name}", post)]
#[utoipa::path(path = "/item/{id}/{name}", params(Filter), post)]
#[allow(unused)]
async fn post_item(
_path: Path<(i32, String)>,
Expand All @@ -407,34 +407,34 @@ fn path_with_path_query_body_resolved() {
&operation.pointer("/parameters").unwrap(),
json!([
{
"in": "path",
"name": "id",
"in": "query",
"name": "age",
"required": true,
"schema": {
"format": "int32",
"type": "integer"
}
},
{
"in": "path",
"name": "name",
"in": "query",
"name": "status",
"required": true,
"schema": {
"type": "string"
}
},
{
"in": "query",
"name": "age",
"in": "path",
"name": "id",
"required": true,
"schema": {
"format": "int32",
"type": "integer"
}
},
{
"in": "query",
"name": "status",
"in": "path",
"name": "name",
"required": true,
"schema": {
"type": "string"
Expand Down
37 changes: 22 additions & 15 deletions utoipa-gen/tests/path_derive_rocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,13 +440,15 @@ fn path_with_all_args_and_body() {
bar: i64,
}

// NOTE! temporarily disable automatic parameter recognition
#[utoipa::path(
responses(
(
status = 200, description = "Hello from server")
),
params(
("id", description = "Hello id"),
QueryParams
)
)]
#[post("/hello/<id>/<name>?<colors>&<rest..>", data = "<hello>")]
Expand Down Expand Up @@ -484,40 +486,41 @@ fn path_with_all_args_and_body() {
},
{
"in": "query",
"name": "colors",
"name": "foo",
"required": true,
"schema": {
"type": "array",
"items": {
"type": "string"
}
"type": "string"
}
},
{
"in": "path",
"name": "name",
"in": "query",
"name": "bar",
"required": true,
"schema": {
"type": "string"
"format": "int64",
"type": "integer"
}
},
{
"in": "query",
"name": "foo",
"name": "colors",
"required": true,
"schema": {
"type": "string"
"type": "array",
"items": {
"type": "string"
}
}
},
{
"in": "query",
"name": "bar",
"in": "path",
"name": "name",
"required": true,
"schema": {
"format": "int64",
"type": "integer"
"type": "string"
}
},
}

])
);
assert_json_eq!(
Expand Down Expand Up @@ -569,9 +572,13 @@ fn path_with_enum_path_param() {
}
}

// NOTE! temporarily disable automatic parameter recognition
#[utoipa::path(
post,
path = "/item",
params(
ApiVersion
),
responses(
(status = 201, description = "Item created successfully"),
),
Expand Down

0 comments on commit 73fd3ea

Please sign in to comment.