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

How to use ktor-swagger-ui 2.2.3 to generate openapi-spec that support multi-file upload. #165

Closed
nartgg35665 opened this issue Dec 4, 2024 · 3 comments

Comments

@nartgg35665
Copy link

Hello, I am using ktor-swagger-ui 2.2.3 . Is there a way to generate openai-spec.json to support multi-file upload.

I did sth like this but it does not work.

  post(
        "/debugTech/sendEmail",
        {
            request {
                multipartBody {
                    mediaType(ContentType.MultiPart.FormData)
                    part<List<File>>("images") { mediaTypes = setOf(ContentType.Image.JPEG) }
                }
            }
        }
    ){...}
 install(SwaggerUI) {
        customSchemas {
            openApi("images") {
                Schema<Any>().also {
                    it.type = "string"
                    it.format = "binary"
                }
            }
        }
    }
@RowlenZen
Copy link

Hi @nartgg35665, try this sample (I copied this one from examples in repo):

request {
                multipartBody {
                    mediaTypes(ContentType.MultiPart.FormData)
                    part<File>("first-image",) {
                        mediaTypes(
                            ContentType.Image.PNG,
                            ContentType.Image.JPEG,
                            ContentType.Image.SVG
                        )
                    }
                    part<File>("second-image") {
                        mediaTypes(
                            ContentType.Image.PNG,
                            ContentType.Image.JPEG,
                            ContentType.Image.SVG
                        )
                    }
                }
            }

@nartgg35665
Copy link
Author

Hello @RowlenZen ,

It was shown like this
image

I want the swaggerUI to enable user to select multiple file when clicking choose file button. Right now, I can choose only one.

@SMILEY4
Copy link
Owner

SMILEY4 commented Jan 11, 2025

Hi @nartgg35665, sorry for the late response.

I'm a bit rusty with this version, but I think a possible solution for versions 2.x could look like this:

// define a custom json schema for an array of files
TypeOverwrites.entries[getSchemaType<Array<File>>()] = """
    {
        "type": "array",
        "items": {
            "type": "string",
            "format": "binary"
        }
    }
""".trimIndent()

install(SwaggerUI)

//...

post("list_of_files", {
    request {
        multipartBody {
            mediaType(ContentType.MultiPart.FormData)
            part<Array<File>>("images") { // use array of files as schema -> uses own defined json schema
                mediaTypes = setOf(
                    ContentType.Image.PNG,
                    ContentType.Image.JPEG,
                    ContentType.Image.GIF
                )
            }
        }
    }
}) {
    call.respond(HttpStatusCode.NotImplemented, "...")
}

@SMILEY4 SMILEY4 closed this as completed Jan 29, 2025
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