murar8 / axum_typed_multipart

Type safe multipart/form-data handling for axum.
77 stars 13 forks source link

Example: Usage with Utoipa #82

Open mattdavis90 opened 1 week ago

mattdavis90 commented 1 week ago

Hi,

I just wanted to say thanks for creating the library - it made working with multipart in axum a whole load easier!!

One thing I did note is that when using the library with utoipa the file upload isn't recognised in rapidoc as a file upload, and Vec<u8> doesn't implement TryFromField or have the ability to carry any metadata. The highlighted lines below was key to tying the libraries together.

I wanted to post so this might be found by others. Feel free to close it, or I'm happy to provide a more complete example if you'd like to add one to the examples directory.

Again, thanks for the library!!

use axum_typed_multipart::{FieldData, TryFromMultipart};
use tempfile::NamedTempFile;
use utoipa::ToSchema;

#[derive(TryFromMultipart, ToSchema)]
pub struct FileUpload {
    /// File or files to upload
    #[form_data(limit = "2MiB")]
    #[schema(value_type = Vec<u8>)]  // <- This is the key line
    myfile: FieldData<NamedTempFile>,
}
murar8 commented 22 hours ago

Hi @mattdavis90, when creating the library I made Vec<_> a special case that will store every occurrence of the field in the derive implementation of TryFromMultipart, so TryFromField can't really be implemented because in order to build the array we have to see the whole request, not only a specific field. Will leave the issue open anyway for a while in case anyone finds a workaround.

mattdavis90 commented 22 hours ago

Hi, no problem at all. The decision you made makes sense to me and the above solution works great. I wasn't asking for any changes to be made; just wanted to highlight that I got both libraries working together with one line addition and hope others may find it useful ☺️

murar8 commented 21 hours ago

Sorry not sure why I read it as a question. If you feel like you struggled, I would say an example integration with utoipa could be a nice addition to the examples. If you would like to open a PR I would be happy to have it in the repo!