diff --git a/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/Basics.kt b/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/Basics.kt index 95222a4..40290b3 100644 --- a/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/Basics.kt +++ b/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/Basics.kt @@ -70,7 +70,7 @@ private fun Application.myModule() { // information about possible responses response { // information about a "200 OK" response - HttpStatusCode.OK to { + code(HttpStatusCode.OK) { // a description of the response description = "successful request - always returns 'Hello World!'" } diff --git a/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/CompleteConfig.kt b/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/CompleteConfig.kt index 9f22eed..78d776e 100644 --- a/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/CompleteConfig.kt +++ b/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/CompleteConfig.kt @@ -179,7 +179,7 @@ private fun Application.myModule() { body() } response { - HttpStatusCode.OK to { + code(HttpStatusCode.OK) { description = "successful request - always returns 'Hello World!'" header("x-random") { description = "A header with some random number" diff --git a/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/CustomizedSchemaGenerator.kt b/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/CustomizedSchemaGenerator.kt index 5940244..d6f5458 100644 --- a/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/CustomizedSchemaGenerator.kt +++ b/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/CustomizedSchemaGenerator.kt @@ -61,7 +61,7 @@ private fun Application.myModule() { // information about the request response { // information about a "200 OK" response - HttpStatusCode.OK to { + code(HttpStatusCode.OK) { // body of the response body() } diff --git a/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/Petstore.kt b/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/Petstore.kt index 6cb299f..a284b1e 100644 --- a/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/Petstore.kt +++ b/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/Petstore.kt @@ -15,6 +15,7 @@ import io.ktor.server.netty.Netty import io.ktor.server.response.respond import io.ktor.server.routing.route import io.ktor.server.routing.routing +import sun.jvm.hotspot.oops.CellTypeState.value fun main() { embeddedServer(Netty, port = 8080, host = "localhost", module = Application::myModule).start(wait = true) @@ -78,7 +79,7 @@ private fun Application.myModule() { } } response { - HttpStatusCode.OK to { + code(HttpStatusCode.OK) { body> { description = "the list of available pets" example("Pet List") { @@ -130,7 +131,7 @@ private fun Application.myModule() { } } response { - HttpStatusCode.OK to { + code(HttpStatusCode.OK) { body { description = "the created pet" example("Bird") { @@ -175,7 +176,7 @@ private fun Application.myModule() { } } response { - HttpStatusCode.OK to { + code(HttpStatusCode.OK) { body{ description = "the pet with the given id" example("Bird") { @@ -194,7 +195,7 @@ private fun Application.myModule() { } } } - HttpStatusCode.NotFound to { + code(HttpStatusCode.NotFound) { description = "the pet with the given id was not found" } default { @@ -221,10 +222,10 @@ private fun Application.myModule() { } } response { - HttpStatusCode.NoContent to { + code(HttpStatusCode.NoContent) { description = "the pet was successfully deleted" } - HttpStatusCode.NotFound to { + code(HttpStatusCode.NotFound) { description = "the pet with the given id was not found" } default { diff --git a/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/RequestResponse.kt b/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/RequestResponse.kt index 3dc9229..50b02fe 100644 --- a/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/RequestResponse.kt +++ b/ktor-swagger-ui-examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/RequestResponse.kt @@ -62,7 +62,7 @@ private fun Application.myModule() { // information the possible responses response { // document the "200 OK"-response - HttpStatusCode.OK to { + code(HttpStatusCode.OK) { description = "Calculation was performed successfully." // specify the schema of the response-body and some additional information body { @@ -70,7 +70,7 @@ private fun Application.myModule() { } } // document the "422 UnprocessableEntity"-response - HttpStatusCode.UnprocessableEntity to { + code(HttpStatusCode.UnprocessableEntity) { description = "The requested calculation could not be performed, e.g. due to division by zero." } } diff --git a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/routes/OpenApiResponses.kt b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/routes/OpenApiResponses.kt index 0a41cf2..4a3d38c 100644 --- a/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/routes/OpenApiResponses.kt +++ b/ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/dsl/routes/OpenApiResponses.kt @@ -25,6 +25,18 @@ class OpenApiResponses { */ infix fun HttpStatusCode.to(block: OpenApiResponse.() -> Unit) = this.value.toString() to block + /** + * Information of response for a given http status code + */ + fun code(statusCode: String, block: OpenApiResponse.() -> Unit) { + responses[statusCode] = OpenApiResponse(statusCode).apply(block) + } + + /** + * Information of response for a given http status code + */ + fun code(statusCode: HttpStatusCode, block: OpenApiResponse.() -> Unit) = code(statusCode.toString(), block) + /** * Information of the default response