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

Trait Objects in handler #955

Open
open-schnick opened this issue Jan 29, 2025 · 0 comments
Open

Trait Objects in handler #955

open-schnick opened this issue Jan 29, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@open-schnick
Copy link

Hi,
i noticed that the exact same bug / question was already asked in #408 but the code doesn't seem to work (anymore?)

Expected Behavior

One can define a trait object as state and inject a concrete implementation during runtime

Actual Behavior

Calling the endpoint returns:

data of type alloc::boxed::Box<dyn crate::Trait> was not found.

Using the example below the error would be

data of type alloc::boxed::Box<dyn poem-test::api::Database> was not found.

Steps to Reproduce the Problem

  1. define a trait as follows:
#[async_trait]
pub trait Database: Send + Sync + dyn_clone::DynClone {
  async fn save(&self, name: String);
}
  1. define a endpoint like this:
#[oai(path = "/", method = "get")]
async fn dummy(&self,  Data(implementation): Data<&Box<dyn Database>>) {
  implementation.save("test".to_string()).await;
}
  1. implementation of Database trait:
#[derive(Debug, Clone)]
pub struct DummyDatabase;

#[async_trait]
impl Database for DummyDatabase {
  async fn save(&self, name: String) -> {
    todo!()
  }
}
  1. define the state in the route
let api = OpenApiService(Api, "App", "0.0.1").data(Box::new(DummyDatabase));
let app = Route::new().nest("/", api);
// Start it ...

Specifications

  • Version: poem: 3.1.6
  • Platform: Linux
  • Subsystem:
@open-schnick open-schnick added the bug Something isn't working label Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant