rambler-digital-solutions / actix-web-validator

Rust library for providing validation mechanism to actix-web with Validator crate.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't use #[api_v2_operation] + actix_web_validator::Path

telezhnaya opened this issue · comments

Hey,
I guess we are doing something wrong, but I can't google it

I have a structure

#[derive(Validate, Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, Apiv2Schema)]
pub struct BalanceRequest {
    #[validate(custom = "crate::errors::validate_account_id")]
    pub account_id: types::AccountId,
}

And the handler

#[api_v2_operation]
pub async fn get_near_balance(
    request: actix_web_validator::Path<schemas::BalanceRequest>,
) -> crate::Result<Json<schemas::NearBalanceResponse>> {
  ...
}

And, it does not compile, giving 4 similar errors for each handler, saying that I have to implement 4 methods: update_parameter, update_security, update_security_definitions, update_definitions.

error[E0599]: no function or associated item named `update_parameter` found for struct `actix_web_validator::Path<BalanceRequest>` in the current scope
  --> src/modules/coin/resources.rs:10:1
   |
10 | #[api_v2_operation]
   | ^^^^^^^^^^^^^^^^^^^ function or associated item not found in `actix_web_validator::Path<BalanceRequest>`
   |

While googling, I've found no examples of using validator + macro #[api_v2_operation]
Should this combo work at all?

The whole code is here https://github.com/neeeekitos/near-enhanced-api-server
FYI @neeeekitos

Hi @telezhnaya! Can you provide MRE?

We have finally found the workaround, we needed to downgrade the dependencies a bit.
It works with:

actix-web-validator = "3.0.0"
paperclip = { version = "0.7.1", features = ["v2", "v3", "actix4", "actix4-validator"] }
validator = { version = "0.14", features = ["derive"] }

Our current (working, but not polished) version is here: near/near-enhanced-api-server#22

Oh, ok you are right. Paperclip doesn't use actix-web-validator 4.x
https://github.com/paperclip-rs/paperclip/blob/master/core/Cargo.toml#L36-L37

I had a very similar issue with almost the same setup, so I'll post here another workaround 'cause it may be useful for someone else, or may give another idea for the issue author.

If someone reaches here and doesn't like the idea of using the older versions as mentioned above, another workaround would be to forget about Paperclip's actix4-validator integration and do the validation simply by calling the validator methods directly.

For example, in my case, I was trying to use actix_web_validator::Query, but was facing problems with #[api_v2_operation]. In this case, I just changed it to actix_web::web::Query and called the validation methods right at the beginning of the function:

use paperclip::actix::{api_v2_operation, Apiv2Schema};
use serde::Deserialize;
use validator::Validate;

#[derive(Apiv2Schema, Deserialize, Validate)]
pub struct MyRequest {
    #[validate(range(min = 0, max = 100))]
    percentage: u8,
}

#[api_v2_operation]
pub fn my_request_handler(my_request: actix_web::web::Query<MyRequest>) -> HttpResponse {
    if let Err(error) = my_request.validate() {
        return actix_web::ResponseError::error_response(&actix_web_validator::Error::from(error));
    }

    // and go on...
    todo!()
}

And my Cargo.toml:

actix-web = "4"
actix-web-validator = "5"
serde = { version = "1", features = ["derive"] }
validator = { version = "0.16", features = ["derive"] }
paperclip = { version = "0.8", features = ["paperclip-actix", "actix4", "swagger-ui", "url", "uuid"] }

Note that it only works with paperclip 0.8.

Thanks!