Will utopia support generic types?seem's not work
Closed this issue · 5 comments
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?
@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
@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.
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.
@ipconfiger Can this be considered solved? I so this could be closed as I need to get the dragging issues count down.
I tried, it work.
tks
pls close it