Skip to content

Commit

Permalink
Merge branch 'fix-file-upload' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
SMILEY4 committed Jan 15, 2025
2 parents b85a3d1 + 24aa979 commit e193ca6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.github.smiley4.schemakenerator.swagger.compileReferencingRoot
import io.github.smiley4.schemakenerator.swagger.data.TitleType
import io.github.smiley4.schemakenerator.swagger.generateSwaggerSchema
import io.github.smiley4.schemakenerator.swagger.withAutoTitle
import io.github.smiley4.schemakenerator.swagger.withTitle
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.Application
Expand Down Expand Up @@ -116,11 +117,11 @@ private fun Application.myModule() {
type
.processReflection()
.generateSwaggerSchema()
.withAutoTitle(TitleType.SIMPLE)
.withTitle(TitleType.SIMPLE)
.compileReferencingRoot()
}
overwrite<File>(Schema<Any>().also {
it.type = "string"
it.types = setOf("string")
it.format = "binary"
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.smiley4.ktorswaggerui.examples

import io.github.smiley4.ktorswaggerui.SwaggerUI
import io.github.smiley4.ktorswaggerui.data.array
import io.github.smiley4.ktorswaggerui.dsl.routing.post
import io.github.smiley4.ktorswaggerui.routing.openApiSpec
import io.github.smiley4.ktorswaggerui.routing.swaggerUI
Expand Down Expand Up @@ -28,7 +29,7 @@ private fun Application.myModule() {
schemas {
// overwrite type "File" with custom schema for binary data
overwrite<File>(Schema<Any>().also {
it.type = "string"
it.types = setOf("string")
it.format = "binary"
})
}
Expand Down Expand Up @@ -59,7 +60,7 @@ private fun Application.myModule() {
call.respond(HttpStatusCode.NotImplemented, "...")
}

// upload multiple files
// upload multiple (two) files
post("multipart", {
request {
multipartBody {
Expand All @@ -84,6 +85,24 @@ private fun Application.myModule() {
call.respond(HttpStatusCode.NotImplemented, "...")
}

// upload multiple (any amount of) files
post("list", {
request {
multipartBody {
mediaTypes(ContentType.MultiPart.FormData)
part("images", array<File>()) {
mediaTypes(
ContentType.Image.PNG,
ContentType.Image.JPEG,
ContentType.Image.SVG
)
}
}
}
}) {
call.respond(HttpStatusCode.NotImplemented, "...")
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private fun Application.myModule() {

// add a swagger schema to the component-section of the api-spec with the id "swagger-schema"
schema("swagger-schema", Schema<Any>().also {
it.type = "number"
it.types = setOf("number")
it.title = "Custom Type"
})

Expand All @@ -48,7 +48,7 @@ private fun Application.myModule() {
// overwrite 'LocalDateTime' with custom schema (root only)
overwrite<LocalDateTime>(Schema<Any>().also {
it.title = "timestamp"
it.type = "integer"
it.types = setOf("integer")
})

// customized schema generation pipeline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class ContentBuilder(
private fun buildMultipartMediaType(body: OpenApiMultipartBodyData): MediaType {
return MediaType().also { mediaType ->
mediaType.schema = Schema<Any>().also { schema ->
schema.type = "object"
schema.types = setOf("object")
schema.properties = mutableMapOf<String?, Schema<*>?>().also { props ->
body.parts.forEach { part ->
props[part.name] = schemaContext.getSchema(part.type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ class OperationBuilderTest : StringSpec({
mediaType.schema
.also { it.shouldNotBeNull() }
?.also { schema ->
schema.type shouldBe "object"
schema.types shouldContainExactlyInAnyOrder listOf("object")
schema.properties.keys shouldContainExactlyInAnyOrder listOf(
"image",
"data"
Expand Down Expand Up @@ -961,7 +961,7 @@ class OperationBuilderTest : StringSpec({
mediaType.schema
.also { it.shouldNotBeNull() }
?.also { schema ->
schema.type shouldBe "object"
schema.types shouldContainExactlyInAnyOrder listOf("object")
schema.properties.keys shouldContainExactlyInAnyOrder listOf("customData")
schema.properties["customData"]!!.`$ref` shouldBe "#/components/schemas/myCustomSchema"
}
Expand Down

0 comments on commit e193ca6

Please sign in to comment.