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

[poem-openapi] Define Custom Response with generic will compile error #60

Closed
umaYnit opened this issue Oct 13, 2021 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@umaYnit
Copy link
Contributor

umaYnit commented Oct 13, 2021

Steps to Reproduce the Problem

use poem_openapi::{ApiResponse, Object, payload::Json};

#[derive(Object)]
pub struct SuccessResponses<T> {
    status: i32,
    data: T,
}

#[derive(Object)]
pub struct ErrorMessage {
    code: i32,
    reason: String,
}


#[derive(ApiResponse)]
pub enum CustomApiResponse<T> {
    #[oai(status = 200)]
    Ok(Json<SuccessResponses<T>>),
    /// Returns when the BadRequest.
    #[oai(status = 400)]
    BadRequest,
}

this is error message:

error[E0412]: cannot find type `T` in this scope
 --> src\main.rs:6:11
  |
6 |     data: T,
  |           ^ not found in this scope

error[E0412]: cannot find type `T` in this scope
  --> src\main.rs:19:30
   |
19 |     Ok(Json<SuccessResponses<T>>),
   |                              ^ not found in this scope

error[E0107]: missing generics for struct `SuccessResponses`
 --> src\main.rs:4:12
  |
4 | pub struct SuccessResponses<T> {
  |            ^^^^^^^^^^^^^^^^ expected 1 generic argument
  |
note: struct defined here, with 1 generic parameter: `T`
 --> src\main.rs:4:12
  |
4 | pub struct SuccessResponses<T> {
  |            ^^^^^^^^^^^^^^^^ -
help: add missing generic argument
  |
4 | pub struct SuccessResponses<T><T> {
  |            ~~~~~~~~~~~~~~~~~~~

error[E0107]: missing generics for enum `CustomApiResponse`
  --> src\main.rs:17:10
   |
17 | pub enum CustomApiResponse<T> {
   |          ^^^^^^^^^^^^^^^^^ expected 1 generic argument
   |
note: enum defined here, with 1 generic parameter: `T`
  --> src\main.rs:17:10
   |
17 | pub enum CustomApiResponse<T> {
   |          ^^^^^^^^^^^^^^^^^ -
help: add missing generic argument
   |
17 | pub enum CustomApiResponse<T><T> {
   |          ~~~~~~~~~~~~~~~~~~~~

Specifications

  • Version: latest
  • Platform: Windows
@umaYnit umaYnit added the bug Something isn't working label Oct 13, 2021
@sunli829
Copy link
Collaborator

There are two ways.

Use concrete type

  1. First, generic objects need to specify the name of each concrete type.
#[derive(Object)]
#[oai(concrete(name = "SuccessResponsesI32", params(i32)))]
pub struct SuccessResponses<T> {
    status: i32,
    data: T,
}
  1. But it still fails to compile here.
#[derive(ApiResponse)]
pub enum CustomApiResponse<T> {
    #[oai(status = 200)]
    Ok(Json<SuccessResponses<T>>),
    /// Returns when the BadRequest.
    #[oai(status = 400)]
    BadRequest,
}

Because Object macro only implements the poem::types::Type trait for SuccessResponses<i32>.

  1. So, you can do it like the following.
#[derive(ApiResponse)]
pub enum CustomApiResponse {
    #[oai(status = 200)]
    Ok(Json<SuccessResponses<i32>>),
    /// Returns when the BadRequest.
    #[oai(status = 400)]
    BadRequest,
}

Inline SuccessResponses

    #[derive(Object)]
    #[oai(inline)]
    pub struct SuccessResponses<T: ParseFromJSON + ToJSON> {
        status: i32,
        data: T,
    }

    #[derive(ApiResponse)]
    pub enum CustomApiResponse<T: ParseFromJSON + ToJSON> {
        #[oai(status = 200)]
        Ok(poem_openapi::payload::Json<SuccessResponses<T>>),
        /// Returns when the BadRequest.
        #[oai(status = 400)]
        BadRequest,
    }

Inline objects must avoid circular dependencies.

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

2 participants