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

OpenApi with multipart & TempFile #120

Open
AzHicham opened this issue Jul 15, 2024 · 4 comments
Open

OpenApi with multipart & TempFile #120

AzHicham opened this issue Jul 15, 2024 · 4 comments
Labels
blocked Currently blocked

Comments

@AzHicham
Copy link

AzHicham commented Jul 15, 2024

Hello,

I would like to generate an openapi def for this struct:

#[derive(Debug, MultipartForm, ApiType)]
pub struct UploadForm {
    pub main: TempFile,
    pub dependencies: Vec<TempFile>,
}

impl TypedSchema for UploadForm {
    fn schema_type() -> InstanceType {
        InstanceType::Object
    }

    fn format() -> Option<String> {
        None
    }
}

Unfortunately I'm stuck on how to do that, obviously I cannot use JsonSchema since it doesn't work with TempFile.
Any help would be really appreciated.

Thank you

@AzHicham
Copy link
Author

Btw the manual implementation works fine in swagger :

impl ApiComponent for UploadForm {
    fn content_type() -> String {
        "multipart/form-data".to_string()
    }

    fn child_schemas() -> Vec<(String, ReferenceOr<Schema>)> {
        vec![]
    }

    fn schema() -> Option<(String, ReferenceOr<Schema>)> {
        Some((
            "UploadForm".to_string(),
            apistos::reference_or::ReferenceOr::Object(Schema::Object(SchemaObject {
                instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::Object))),
                object: Some(Box::new(ObjectValidation {
                    properties: {
                        let mut map = Map::new();
                        map.insert(
                            "main".to_string(),
                            Schema::Object(SchemaObject {
                                instance_type: Some(SingleOrVec::Single(Box::new(
                                    InstanceType::String,
                                ))),
                                format: Some("binary".to_string()),
                                ..Default::default()
                            }),
                        );
                        map.insert(
                            "dependencies".to_string(),
                            Schema::Object(SchemaObject {
                                instance_type: Some(SingleOrVec::Single(Box::new(
                                    InstanceType::Array,
                                ))),
                                array: Some(Box::new(ArrayValidation {
                                    items: Some(SingleOrVec::Single(Box::new(Schema::Object(
                                        SchemaObject {
                                            instance_type: Some(SingleOrVec::Single(Box::new(
                                                InstanceType::String,
                                            ))),
                                            format: Some("binary".to_string()),
                                            ..Default::default()
                                        },
                                    )))),
                                    min_items: Some(0),
                                    ..Default::default()
                                })),
                                ..Default::default()
                            }),
                        );
                        map
                    },
                    required: ["main".to_string()].into_iter().collect(),
                    ..Default::default()
                })),
                ..Default::default()
            })),
        ))
    }
}

@axelfaure
Copy link
Contributor

Hi !

Unfortunately you may need to wait a few weeks until @rlebran is back to get a definitive answer. But by any chance did you also try to do it using apistos 1.0.0-pre-release.2?

I am under the impression it will work in this upcoming version (but I did not have time to do a complete test).

@AzHicham
Copy link
Author

Hi :)

No worries since I have a backup solution with the manual impl it's fine.
I don't see in the 1.0.0 pre-release any change that will make it work with TempFile but I'll try ;)

@rlebran
Copy link
Contributor

rlebran commented Aug 4, 2024

Hi !

Sadly schemars doesn't provide a JsonSchema implementation for TempFile.
For now the manual implementation seems to be the only way of documenting your struct.

If I think of a solution I will keep you updated. In the meantime I will keep the issue open for reference.

@rlebran rlebran added the blocked Currently blocked label Aug 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked Currently blocked
Projects
None yet
Development

No branches or pull requests

3 participants