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

Drop limitation of disallowing generics for the TypedPath derive in axum-extra #2723

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

JustusFluegel
Copy link

@JustusFluegel JustusFluegel commented Apr 25, 2024

Motivation

So I've recently tried to create a generic axum handler for any sea-orm Entity, which allows me to for example fetch it. Since a specific entity would not necessarily be indexed by the same primary key type, I wanted to be generic over that as well, which was when I noticed the TypedPath derive doesn't support generic params yet.

Example

#[allow(type_alias_bounds)]
pub type IdType<T>
where
    T: EntityTrait,
= <T::PrimaryKey as PrimaryKeyTrait>::ValueType;

#[derive(TypedPath, Deserialize)]
#[typed_path("/:id")]
pub struct GetById<T> {
    id: T,
}

pub async fn get_by_id<T: EntityTrait>(
    GetById { id }: GetById<IdType<T>>,
    State(AppState {
        database_connection,
        ..
    }): State<AppState>,
) -> impl IntoResponse {
    Json(
        T::find_by_id(id)
            .into_json()
            .one(&database_connection)
            .await
            .unwrap(),
    )
}

Solution

This removes the limitation in the proc-macro using the following 2 changes:

  • Drop the check for generics from the macro
  • Add the required generics & where bounds where needed

Signed-off-by: Justus Fluegel <justusfluegel@gmail.com>
Signed-off-by: Justus Fluegel <justusfluegel@gmail.com>
Signed-off-by: Justus Fluegel <justusfluegel@gmail.com>
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

Successfully merging this pull request may close these issues.

1 participant