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

Is it possible to have generic OpenApi? #216

Closed
vimmerru opened this issue Feb 25, 2022 · 3 comments
Closed

Is it possible to have generic OpenApi? #216

vimmerru opened this issue Feb 25, 2022 · 3 comments
Labels
question Further information is requested

Comments

@vimmerru
Copy link

vimmerru commented Feb 25, 2022

I am trying to avoid direct dependency to business layer when expose API and struggling here.

In my business layer i have trait that defines api interface(+ some implementation). Like this

trait MyApiPort {
   type MyRepository: MyRepositoryPort;
   
   async fn some_call(&self, SomeCallRequest, repo: & MyRepository) -> Result< SomeCallResponse>;
}

pub struct MyApi<UserRepository> {
    _marker: PhantomData< MyRepository >
}

#[async_trait(?Send)]
impl<MyRepository: MyRepositoryPort> MyApiPort for MyApi< MyRepository > {
    type MyRepository = MyRepository;
    
    ...
}

Now i want to expose my api as OpenAPI with poem, but i want avoid direct dependency to MyApi struct and depend on MyApiPort instead

struct MyOpenApi<MyApi> {
    api: MyApi;
}

#[OpenApi(tag = "ApiTags::MyApi")]
impl<MyApi: MyApiPort> MyOpenApi<MyApi> {

    #[oai(path = "/some_call", method = "post")]
    async fn some_call(
        &self,
        req: Json< SomeCallRequest >,
        users: Data<& MyApi::MyRepository>,
    ) -> SomeResponse {
       ...
    }
}

Seems OpenApi can't work with generic impls as i getting error about unknown types. Is there any way to workaround this and be able to abstract and mock from business layer here?

@vimmerru vimmerru added the question Further information is requested label Feb 25, 2022
@vimmerru
Copy link
Author

One solution can be usage of trait objects, but i am trying avoid dynamic dispatch.

sunli829 added a commit that referenced this issue Mar 7, 2022
@sunli829
Copy link
Collaborator

sunli829 commented Mar 7, 2022

Sorry for the late reply, it's been too busy at this time. 😂

I added support for generic OpenAPI in v1.3.10, here is the example:

async fn generic() {

@vimmerru
Copy link
Author

@sunli829 Great, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants