-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
206 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...examples/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/KotlinxSerialization.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package io.github.smiley4.ktorswaggerui.examples | ||
|
||
import io.github.smiley4.ktorswaggerui.SwaggerUI | ||
import io.github.smiley4.ktorswaggerui.data.kotlinxExampleEncoder | ||
import io.github.smiley4.ktorswaggerui.dsl.routing.get | ||
import io.github.smiley4.ktorswaggerui.routing.openApiSpec | ||
import io.github.smiley4.ktorswaggerui.routing.swaggerUI | ||
import io.github.smiley4.schemakenerator.serialization.processKotlinxSerialization | ||
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.withTitle | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.server.application.Application | ||
import io.ktor.server.application.install | ||
import io.ktor.server.engine.embeddedServer | ||
import io.ktor.server.netty.Netty | ||
import io.ktor.server.response.respondText | ||
import io.ktor.server.routing.route | ||
import io.ktor.server.routing.routing | ||
import kotlinx.serialization.Serializable | ||
|
||
fun main() { | ||
embeddedServer(Netty, port = 8080, host = "localhost", module = Application::myModule).start(wait = true) | ||
} | ||
|
||
private fun Application.myModule() { | ||
|
||
install(SwaggerUI) { | ||
schemas { | ||
// configure the schema generator to use kotlinx-serializer | ||
// (see https://github.com/SMILEY4/schema-kenerator/wiki for more information) | ||
generator = { type -> | ||
type | ||
.processKotlinxSerialization() | ||
.generateSwaggerSchema() | ||
.withTitle(TitleType.SIMPLE) | ||
.compileReferencingRoot() | ||
} | ||
} | ||
examples { | ||
// configure the example encoder to encode kotlin objects using kotlinx-serializer | ||
exampleEncoder = kotlinxExampleEncoder | ||
} | ||
} | ||
|
||
routing { | ||
|
||
// add the routes for swagger-ui and api-spec | ||
route("swagger") { | ||
swaggerUI("/api.json") | ||
} | ||
route("api.json") { | ||
openApiSpec() | ||
} | ||
|
||
// a documented route | ||
get("hello", { | ||
description = "A Hello-World route" | ||
request { | ||
queryParameter<String>("name") { | ||
description = "the name to greet" | ||
example("Name Parameter") { | ||
value = "Mr. Example" | ||
} | ||
} | ||
} | ||
response { | ||
code(HttpStatusCode.OK) { | ||
description = "successful request - always returns 'Hello World!'" | ||
body<TestResponse> { | ||
example("Success Response") { | ||
value = TestResponse( | ||
name = "Mr. Example", | ||
length = 11 | ||
) | ||
} | ||
} | ||
} | ||
} | ||
}) { | ||
call.respondText("Hello ${call.request.queryParameters["name"]}") | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
|
||
@Serializable | ||
data class TestResponse( | ||
val name: String, | ||
val length: Int, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
ktor-swagger-ui/src/main/kotlin/io/github/smiley4/ktorswaggerui/data/ExampleConfigData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.