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

Api operation on generic handler #115

Closed
AzHicham opened this issue Jul 11, 2024 · 3 comments · Fixed by #117
Closed

Api operation on generic handler #115

AzHicham opened this issue Jul 11, 2024 · 3 comments · Fixed by #117
Labels
bug Something isn't working enhancement New feature or request

Comments

@AzHicham
Copy link

Hello

I'm facing an issue with the macro api_operation.
here is my code :

pub(crate) fn config<T: AnalysisInterface + 'static>() -> Scope {
    scope("/analyze")
        .service(_config::<T, Product1<SubProductA>>())
        .service(_config::<T, Product1<SubProductB>>())
        .service(_config::<T, Product2<SubProductA>>())
        .service(_config::<T, Product2<SubProductB>>())
}

fn _config<T: AnalysisInterface + 'static, R: Product + Send + 'static>() -> Resource {
    let route =
        "/{product_id}/{algo_version}".replace("{product_id}", &T::product_name::<R>());
    resource(&route).route(post().to(start_analysis::<T, R>))
}

#[api_operation(summary = "Start an analysis")]
#[instrument(skip(data))]
async fn start_analysis<T: AnalysisInterface, R: Product + Send + 'static>(
    data: Data<T>,
    _: Path<String>,
    Json(req): Json<AnalyzeRequest>,
) -> Result<AnalyzeResponse, AppError> {
    let res = data.start_analyze::<R>(req).await?;
    Ok(res)
}

I know this is kind of tricky to have a generic handler and register it multiple times.
But do you think it should works or it's better to create multiple handler, one for each case (4 here in this example).

The error message:

error[E0107]: struct takes 1 generic argument but 2 generic arguments were supplied
   --> src/server/v0/analysis.rs:40:1
    |
40  | #[api_operation(summary = "Get status of the service")]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 1 generic argument
41  | #[instrument(skip(data))]
42  | async fn start_analysis<T: AnalysisInterface, R: Product + Send + 'static>(
    |                                               - help: remove this generic argument

Thank you for your help

@AzHicham
Copy link
Author

AzHicham commented Jul 11, 2024

Looking at the code here
It seems that the macro can handle generic handler with at most 1 parameter type.
and PhantomData should not accept more that 1 type parameter
A solution could be to add as many PhantomData as needed ?

@rlebran rlebran linked a pull request Jul 12, 2024 that will close this issue
@rlebran
Copy link
Contributor

rlebran commented Jul 12, 2024

Hi !
This part of the code is the problem you are right, I just opened a PR which aims at handling any number of generic params.
Just keep in mind this should not have any impact on the generated documentation for now.

@rlebran rlebran added bug Something isn't working enhancement New feature or request labels Jul 12, 2024
@AzHicham
Copy link
Author

Thank you for the quick fix :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants