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

Will utopia support generic types?seem's not work #1049

Closed
ipconfiger opened this issue Sep 13, 2024 · 5 comments
Closed

Will utopia support generic types?seem's not work #1049

ipconfiger opened this issue Sep 13, 2024 · 5 comments

Comments

@ipconfiger
Copy link

I have a struct

#[derive(ToSchema, Serialize)]
pub struct Response<T: Serialize> {
    pub code: i32,
    pub msg: String,
    pub data: Option<T>,
}

impl<T> Response<T>
where
    T: Serialize,
{
    pub fn new(code: i32, msg: String, data: Option<T>) -> Self {
        Self { code, msg, data }
    }
    pub fn ok(data: T) -> Self {
        Self::new(200, "OK".to_string(), Some(data))
    }
    pub fn err(code: i32, msg: String) -> Self {
        Self::new(code, msg, None)
    }
}

when i use another struct such as :

#[derive(Debug, ToSchema, Deserialize, Serialize)]
pub struct Token {
    pub token: String,
    pub expire: i64,
}

so i can assembly a response as Response and it will generate a json response like below:

{
  code: 200,
  msg: "OK",
  data: {
    token: "xxxxxxx",
    expire: 1780000000
  }
}

i want use Response in utopia.path macro:

#[utoipa::path(
    post,
    path = "/api/login",
    request_body = LoginForm,
    responses(
            (status = 200, description = "To Login", body = Response<Token>),
            (status = 500, description = "Error info", body = Response)
    )
)]

but in docs, it show example like that:

{
  "code": 0,
  "data": "/api/docs/openapi.json#/components/schemas/T",
  "msg": "string"
}

and the schema:

{
code*: integer
data: {recursive: T}
msg*: string
}

so, Will utopia support generic types? what i miss?

@juhaku
Copy link
Owner

juhaku commented Sep 13, 2024

@ipconfiger The short answer is yes via aliases. See the docs: https://docs.rs/utoipa/latest/utoipa/derive.ToSchema.html#generic-schemas-with-aliases

However there is coming new support regarding generics in coming 5.0.0 release: #1034 #1048 https://github.com/juhaku/utoipa/tree/master/utoipa-config

@799189288
Copy link

@ipconfiger The short answer is yes via aliases. See the docs: https://docs.rs/utoipa/latest/utoipa/derive.ToSchema.html#generic-schemas-with-aliases

However there is coming new support regarding generics in coming 5.0.0 release: #1034 #1048 https://github.com/juhaku/utoipa/tree/master/utoipa-config

seems not that convinient, why not support generic type directly. just like the example above.

@juhaku
Copy link
Owner

juhaku commented Sep 13, 2024

There is plenty of discussion around the generics in the old issues. But to answer the why it was not the simpliest thing to implement. Yet the 5.0.0 will bring about support for generic params directly.

@juhaku
Copy link
Owner

juhaku commented Sep 15, 2024

@ipconfiger Can this be considered solved? I so this could be closed as I need to get the dragging issues count down.

@ipconfiger
Copy link
Author

I tried, it work.
tks
pls close it

@juhaku juhaku closed this as completed Sep 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants