-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve default schema generator, minor improvements
- Loading branch information
Showing
7 changed files
with
98 additions
and
4 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
80 changes: 80 additions & 0 deletions
80
...les/src/main/kotlin/io/github/smiley4/ktorswaggerui/examples/CustomizedSchemaGenerator.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,80 @@ | ||
package io.github.smiley4.ktorswaggerui.examples | ||
|
||
import io.github.smiley4.ktorswaggerui.SwaggerUI | ||
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.withAutoTitle | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.server.application.Application | ||
import io.ktor.server.application.call | ||
import io.ktor.server.application.install | ||
import io.ktor.server.engine.embeddedServer | ||
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 kotlinx.serialization.Serializable | ||
|
||
fun main() { | ||
embeddedServer(Netty, port = 8080, host = "localhost", module = Application::myModule).start(wait = true) | ||
} | ||
|
||
private fun Application.myModule() { | ||
|
||
// Install and configure the "SwaggerUI"-Plugin | ||
install(SwaggerUI) { | ||
schemas { | ||
// replace default schema-generator with customized one | ||
generator = { type -> | ||
type | ||
// process type using kotlinx-serialization instead of reflection | ||
// requires additional dependency "io.github.smiley4:schema-kenerator-kotlinx-serialization:<VERSION>" | ||
// see https://github.com/SMILEY4/schema-kenerator for more information | ||
.processKotlinxSerialization() | ||
.generateSwaggerSchema() | ||
.withAutoTitle(TitleType.SIMPLE) | ||
.compileReferencingRoot() | ||
} | ||
} | ||
} | ||
|
||
routing { | ||
|
||
// Create a route for the swagger-ui using the openapi-spec at "/api.json". | ||
// This route will not be included in the spec. | ||
route("swagger") { | ||
swaggerUI("/api.json") | ||
} | ||
// Create a route for the openapi-spec file. | ||
// This route will not be included in the spec. | ||
route("api.json") { | ||
openApiSpec() | ||
} | ||
|
||
// a documented route | ||
get("hello", { | ||
// information about the request | ||
response { | ||
// information about a "200 OK" response | ||
HttpStatusCode.OK to { | ||
// body of the response | ||
body<MyResponseBody>() | ||
} | ||
} | ||
}) { | ||
call.respond(HttpStatusCode.NotImplemented, "...") | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
@Serializable | ||
private class MyResponseBody( | ||
val name: String, | ||
) |
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