forked from LukasForst/ktor-openapi-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimalExample.kt
89 lines (83 loc) · 2.98 KB
/
MinimalExample.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import com.fasterxml.jackson.annotation.JsonFormat
import com.fasterxml.jackson.annotation.JsonProperty
import com.papsign.ktor.openapigen.BehovType
import com.papsign.ktor.openapigen.OpenAPIGen
import com.papsign.ktor.openapigen.annotations.parameters.PathParam
import com.papsign.ktor.openapigen.content.type.binary.BinaryResponse
import com.papsign.ktor.openapigen.route.apiRouting
import com.papsign.ktor.openapigen.route.path.normal.get
import com.papsign.ktor.openapigen.route.path.normal.post
import com.papsign.ktor.openapigen.route.response.respond
import com.papsign.ktor.openapigen.route.response.respondWithStatus
import com.papsign.ktor.openapigen.route.route
import io.ktor.http.*
import io.ktor.serialization.jackson.jackson
import io.ktor.server.application.Application
import io.ktor.server.application.install
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import java.io.InputStream
/**
* Minimal example of OpenAPI plugin for Ktor.
*/
fun Application.minimalExample() {
// install OpenAPI plugin
install(OpenAPIGen) {
// this serve OpenAPI definition on /openapi.json
serveOpenApiJson = true
// this servers Swagger UI on /swagger-ui/index.html
serveSwaggerUi = true
info {
title = "Minimal Example API"
}
}
// install JSON support
install(ContentNegotiation) {
jackson()
}
// and now example routing
apiRouting {
route("/example/{name}") {
// SomeParams are parameters (query or path), SomeResponse is what the backend returns and SomeRequest
// is what was passed in the body of the request
post<SomeParams, SomeResponse, SomeRequest> { params, someRequest ->
respond(SomeResponse(bar = "Hello ${params.name}! From body: ${someRequest.foo}."))
}
get<Unit, SomeSimpleEnum> { _ ->
respond(SomeSimpleEnum.C)
}
}
route("/not-an-example/") {
get<Unit, SomeComplexEnum> {
respond(SomeComplexEnum.C)
}
}
route("/not-an-example-anymore/") {
get<Unit, BehovType> {
respond(BehovType.MANUELT_PÅKREVD)
}
post<Unit, BehovType, BehovType> { _, dto ->
respond(dto)
}
}
route("/forbidden/{name}") {
get<SomeParams, DokumentResponsDTO> { params ->
params.name
respondWithStatus(HttpStatusCode.Forbidden)
}
}
}
}
@BinaryResponse(contentTypes = ["application/pdf"])
data class DokumentResponsDTO(val stream: InputStream)
data class SomeParams(@PathParam("who to say hello") val name: String)
data class SomeRequest(val foo: String)
data class SomeResponse(val bar: String)
enum class SomeSimpleEnum {
A, B, C
}
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
enum class SomeComplexEnum(
@JsonProperty("proppen") val variable: String,
) {
A("a"), B("b"), C("c")
}