abergmeier
abergmeier

Reputation: 14071

Utoipa complaining about mismatching signature

In my code I use utoipa in combination with axum. For some reason when registering the routes utoipa reports a signature mismatch.

The code is:


#[utoipa::path(
  post,
  path = "/machines/{id}/state",
  summary = "Set state of machine",
  params(
      ("id" = String, description = "Machine ID"),
  ),
  request_body = MachineState,
  responses(
      (status = StatusCode::OK),
      (status = StatusCode::BAD_REQUEST),
      (status = StatusCode::UNAUTHORIZED, response = crate::rest::authentication::responses::Unauthorized),
      (status = StatusCode::FORBIDDEN),
      (status = StatusCode::NOT_FOUND),
  ),
)]
async fn set_state(
  State(ctx): State<Arc<MachineContext>>,
  Path(_id): Path<String>,
  state: MachineState,
) -> impl IntoResponse {
  let resource = ctx.resources.get_by_id(_id.as_str());
  if resource.is_none() {
    return (StatusCode::NOT_FOUND).into_response();
  }
  let resource = resource.unwrap();
  let parent = info_span!("list_machines");
  let uid = "123";
  let session = ctx.sessionmanager.try_open(&parent, uid);
  if session.is_none() {
    return (StatusCode::NOT_FOUND).into_response();
  }

  let session = session.unwrap();
  if !resource.visible(&session) {
    return (StatusCode::FORBIDDEN).into_response()
  }

  let state = match state {
    MachineState::Blocked => Status::Blocked(session.get_user_ref()),
    MachineState::Disabled => Status::Disabled,
    MachineState::Free => Status::Free,
    MachineState::InUse => Status::InUse(session.get_user_ref()),
    MachineState::Reserved => Status::Reserved(session.get_user_ref()),
    MachineState::ToCheck => Status::ToCheck(session.get_user_ref()),
  };
  resource
    .try_update(session.clone(), state)
    .await;
  (StatusCode::OK).into_response()
}

pub fn router(sessionmanager: &SessionManager, ressources: &ResourcesHandle) -> OpenApiRouter {
    let ctx = Arc::new(MachineContext {
        resources: ressources.clone(),
        sessionmanager: sessionmanager.clone(),
    });
    OpenApiRouter::new()
        .routes(routes!(
            set_state,
        ))
        .with_state(ctx)
}

which complains:

error[E0277]: the trait bound `fn(axum::extract::State<Arc<MachineContext>>, axum::extract::Path<std::string::String>, resource::MachineState) -> impl futures_util::Future<Output = impl IntoResponse> {set_state}: Handler<_, Arc<MachineContext>>` is not satisfied
   --> bffhd/rest/resource.rs:448:17
    |
448 |           .routes(routes!(
    |  _________________^
449 | |             register_machine,
450 | |             unregister_machine,
451 | |             get_machine,
...   |
454 | |             get_reservations,
455 | |         ))
    | |         ^
    | |         |
    | |_________the trait `Handler<_, Arc<MachineContext>>` is not implemented for fn item `fn(State<Arc<MachineContext>>, Path<String>, MachineState) -> impl Future<Output = ...> {set_state}`
    |           required by a bound introduced by this call
    |
    = note: the full name for the type has been written to '/user/bffh/target/debug/deps/difluoroborane-7eaf12a346b5233d.long-type-6479587143606823754.txt'
    = note: consider using `--verbose` to print the full type name to the console
    = note: Consider using `#[axum::debug_handler]` to improve the error message
    = help: the following other types implement trait `Handler<T, S>`:
              <MethodRouter<S> as Handler<(), S>>
              <axum::handler::Layered<L, H, T, S> as Handler<T, S>>
note: required by a bound in `MethodRouter::<S>::on`
   --> /user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/axum-0.8.1/src/routing/method_routing.rs:632:12
    |
630 |     pub fn on<H, T>(self, filter: MethodFilter, handler: H) -> Self
    |            -- required by a bound in this associated function
631 |     where
632 |         H: Handler<T, S>,
    |            ^^^^^^^^^^^^^ required by this bound in `MethodRouter::<S>::on`
    = note: this error originates in the macro `$crate::routes` which comes from the expansion of the macro `routes` (in Nightly builds, run with -Z macro-backtrace for more info)

For me it seems like the types are correct - but maybe I am reading the error wrong.

Upvotes: 0

Views: 31

Answers (1)

abergmeier
abergmeier

Reputation: 14071

Had to wrap MachineState in Json to get it working. Is described here: https://docs.rs/axum/0.8.1/axum/extract/index.html

#[axum::debug_handler] did not extend the error message but show a totally different one with the above link - which helped. So thanks to @ChayimFriedman for nudging me into that direction.

Upvotes: 0

Related Questions