Skip to content

Commit

Permalink
Fix parameter inline for tuple path params (#1014)
Browse files Browse the repository at this point in the history
This commit fixes bug where already defined parameter schema were
overridden by external parameter schema in all cases. This caused an
issue where sometimes inline information was lost which caused parameter
render as reference schema instead.

Fixes #995
  • Loading branch information
juhaku committed Sep 1, 2024
1 parent 3225e4d commit 8948d34
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
9 changes: 7 additions & 2 deletions utoipa-gen/src/path/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ impl<'p> Parameter<'p> {
match (self, other) {
(Self::Value(value), Parameter::Value(other)) => {
let (schema_features, _) = &value.features;
value.parameter_schema = other.parameter_schema;
// if value parameter schema has not been defined use the external one
if value.parameter_schema.is_none() {
value.parameter_schema = other.parameter_schema;
}

if let Some(parameter_schema) = &mut value.parameter_schema {
parameter_schema.features.clone_from(schema_features);
Expand Down Expand Up @@ -274,7 +277,9 @@ impl Parse for ValueParameter<'_> {

if input.fork().parse::<ParameterIn>().is_ok() {
parameter.parameter_in = input.parse()?;
input.parse::<Token![,]>()?;
if !input.is_empty() {
input.parse::<Token![,]>()?;
}
}

let (schema_features, parameter_features) = input
Expand Down
55 changes: 55 additions & 0 deletions utoipa-gen/tests/path_derive_axum_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,3 +752,58 @@ fn derive_path_with_validation_attributes_axum() {
config
);
}

#[test]
fn path_derive_inline_with_tuple() {
#[derive(utoipa::ToSchema)]
#[allow(unused)]
pub enum ResourceType {
Type1,
Type2,
}

#[utoipa::path(
get,
path = "/test_2params_separated/{resource_type}/{id}",
params(
("resource_type" = inline(ResourceType), Path),
("id" = String, Path)
)
)]
#[allow(unused)]
pub async fn inline_tuple(
Path((resource_type, id)): axum::extract::Path<(ResourceType, String)>,
) {
}

use utoipa::Path;
let value = __path_inline_tuple::operation();
let value = serde_json::to_value(value).expect("operation should serialize to json");

assert_json_eq!(
value,
json!({
"operationId": "inline_tuple",
"parameters": [
{
"in": "path",
"name": "resource_type",
"required": true,
"schema": {
"enum": ["Type1", "Type2"],
"type": "string"
},
},
{
"in": "path",
"name": "id",
"required": true,
"schema": {
"type": "string"
},
}
],
"responses": {}
})
)
}

0 comments on commit 8948d34

Please sign in to comment.