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

Name conflict with actix::web::Json #25

Open
vimmerru opened this issue Jun 9, 2021 · 5 comments
Open

Name conflict with actix::web::Json #25

vimmerru opened this issue Jun 9, 2021 · 5 comments

Comments

@vimmerru
Copy link

vimmerru commented Jun 9, 2021

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.

@singulared
Copy link
Collaborator

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>> {}

@vimmerru
Copy link
Author

vimmerru commented Jun 9, 2021

@singulared Sure, the issue is more aesthetic, but there are still some practical aspects:

  1. 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 of Json by mistake and hard to find on code review based on diff. Code will compile well, but there will be significant issue.
  2. actix_web_validator just too long name for prefixed import. I stopped on use actix_web_validator as web_validator

@singulared
Copy link
Collaborator

Btw, you can create a type alias for actix_web_validator::Json in your own code:

type ValidatedJson<T> = actix_web_validator::Json<T>;

@vimmerru
Copy link
Author

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.

@singulared
Copy link
Collaborator

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>());
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants