Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix find actual request body TypeTree #977

Merged
merged 1 commit into from
Jul 24, 2024
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
11 changes: 9 additions & 2 deletions utoipa-gen/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ impl<'t> From<TypeTree<'t>> for RequestBody<'t> {

impl ToTokensDiagnostics for RequestBody<'_> {
fn to_tokens(&self, tokens: &mut TokenStream) -> Result<(), Diagnostics> {
let mut actual_body = get_actual_body_type(&self.ty).unwrap().clone();
let mut actual_body = get_actual_body_type(&self.ty)
.expect("should have found actual requst body TypeTree")
.clone();

if let Some(option) = find_option_type_tree(&self.ty) {
let path = option.path.clone();
Expand Down Expand Up @@ -195,7 +197,12 @@ fn get_actual_body_type<'t>(ty: &'t TypeTree<'t>) -> Option<&'t TypeTree<'t>> {
.expect("Option must have one child"),
),
"Bytes" => Some(ty),
_ => None,
_ => match ty.children {
Some(ref children) => get_actual_body_type(children.first().expect(
"Must have first child when children has been defined in get_actual_body_type",
)),
None => None,
},
})
}

Expand Down
50 changes: 50 additions & 0 deletions utoipa-gen/tests/path_derive_actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,56 @@ macro_rules! test_derive_path_operations {
};
}

#[test]
fn path_derive_custom_generic_wrapper() {
#[derive(utoipa::ToSchema, serde::Serialize, serde::Deserialize)]
struct Validated<T>(T);

impl<T> FromRequest for Validated<T> {
type Error = actix_web::Error;

type Future = Ready<Result<Self, Self::Error>>;

fn from_request(
_req: &actix_web::HttpRequest,
_payload: &mut actix_web::dev::Payload,
) -> Self::Future {
todo!()
}
}

#[derive(utoipa::ToSchema, serde::Serialize, serde::Deserialize)]
struct Item(String);

#[utoipa::path()]
#[post("/item")]
async fn post_item(_body: Validated<Json<Item>>) -> Json<Item> {
Json(Item(String::new()))
}

#[derive(utoipa::OpenApi)]
#[openapi(paths(post_item))]
struct Doc;

let doc = serde_json::to_value(Doc::openapi()).unwrap();
let operation = doc.pointer("/paths/~1item/post").unwrap();

assert_json_eq!(
&operation.pointer("/requestBody"),
json!({
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Item"
}
}
},
"required": true,
})
)
}

test_derive_path_operations! {
derive_path_operation_post, mod_test_post: post
derive_path_operation_get, mod_test_get: get
Expand Down
Loading