actix-web-validator
actix-web-validator copied to clipboard
Name conflict with actix::web::Json
Current suggested way is to import json extractor as use actix_web_validator::Json
and ValidatedJson
is deprecated.
The problem with this approach actix::web::Json
used not only as Extractor
, but also as Responder
and usual signature is
pub(crate) async fn create(
req: web::Json<CreateRequest>,
) -> Result<web::Json<CreateResponse>> {}
As a result it can't be in-place replacement and i have use actix_web_validator::Json as ValidatedJson
in near all my endpoints.
You can steel use web::Json
for actix Json. Something like this:
use actix_web::web;
use actix_web_validator::Json;
async fn create(
req: Json<CreateRequest>,
) -> Result<web::Json<CreateResponse>> {}
or
use actix_web::web::Json;
async fn decreate(
req: actix_web_validator::Json<CreateRequest>,
) -> Result<Json<CreateResponse>> {}
@singulared Sure, the issue is more aesthetic, but there are still some practical aspects:
- In first option you are provided we mix prefixed and non-prefixed names. Practical problem here is it quite easy to use
web::Json
instead ofJson
by mistake and hard to find on code review based on diff. Code will compile well, but there will be significant issue. -
actix_web_validator
just too long name for prefixed import. I stopped onuse actix_web_validator as web_validator
Btw, you can create a type alias for actix_web_validator::Json in your own code:
type ValidatedJson<T> = actix_web_validator::Json<T>;
Btw, you can create a type alias for actix_web_validator::Json in your own code
Usually if i need define my own types it is just sign of not well defined interface. This issue is a proposal to solve this.
In fact, this is not a new type, it is just an alias to an existing one.
use std::any::TypeId;
struct A;
type B = A;
fn main() {
assert!(TypeId::of::<A>() == TypeId::of::<B>());
}