-
-
Notifications
You must be signed in to change notification settings - Fork 295
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
How to return bytes but generate spec for specific type + encoding #314
Comments
struct Api {
db_handle: DbHandle
}
#[OpenApi]
impl Api {
#[oai(path = "/get_stuff", method = "get")]
async fn get_stuff(&self) -> Base64<Vec<u8>> { // << just change this to Base64<Vec<u8>>
self.db_handle.get_stuff()
}
} |
You don't want to use If so, you might be able to reference this to create a new type? https://github.com/poem-web/poem/blob/master/poem-openapi/src/types/base64_type.rs |
Sorry I don't think I was very clear with what I'm asking here. I didn't mean to include both Binary and Base64 above, just Base64. Imagine I have code like this:
What I'm trying to declare here is when we generate the OpenAPI spec, it should say that this function returns
But within the code, I don't want to construct So in the above code you see I add this A more complete code example, missing the |
I see, you want to return the binary directly from the database, thus avoiding unnecessary serialization, I'll add an |
Awesome you're a legend. |
Is this what you want? poem/poem-openapi/tests/api.rs Line 675 in cd27094
|
Yes that's exactly it, this would be very helpful for my use case. |
Actually on closer inspection, this doesn't quite do what I want. Apologies, it works for the example I gave, but I actually have a more complicated case:
I imagine this is more complicated to add. |
Upon further experimentation, I can add the following types to make it work!
Which I then use like this:
|
#[derive(ResponseContent)]
pub enum MyResponseContent<T: ToJSON + Send + Sync> {
Json(Json<T>),
#[oai(actual_type = "Binary<T>")] // <<<< I'll add this
Binary(Binary<Vec<u8>>),
} |
Awesome that would help a lot, because the solution above works but there is no type checking that the inner type is the same. |
Once you add it I'll make an example demonstrating it 💯 |
Done! 🙂 |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days. |
This issue was closed because it has been stalled for 5 days with no activity. |
First, some code demonstrating what I want to do:
In short, I want to be able to read bytes from a DB and return them, but because I know they really represent a type (Stuff) encoded in a particular way (Base64), generate the OpenAPI spec in a way that says it returns Base64 encoded Stuff, not just a bunch of bytes.
Is there a way to do this today in Poem?
I want this to save myself having to deserialise Stuff just to reserialize it again.
Thanks!!
The text was updated successfully, but these errors were encountered: