netwo-io/apistos

Api operation on generic handler

Closed this issue · 3 comments

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

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 ?

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.

Thank you for the quick fix :)