Skip to content

Petstore

An implementation of the "Petstore" sample project.

  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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package io.github.smiley4.ktoropenapi.examples

import io.github.smiley4.ktoropenapi.OpenApi
import io.github.smiley4.ktoropenapi.delete
import io.github.smiley4.ktoropenapi.get
import io.github.smiley4.ktoropenapi.post
import io.github.smiley4.ktoropenapi.openApi
import io.github.smiley4.ktorredoc.redoc
import io.github.smiley4.ktorswaggerui.swaggerUI
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.respond
import io.ktor.server.routing.route
import io.ktor.server.routing.routing

fun main() {
    embeddedServer(Netty, port = 8080, host = "localhost", module = Application::myModule).start(wait = true)
}


/**
 * Uses the OpenAPI example "petstore simple" to demonstrate ktor with swagger-ui
 * https://github.com/OAI/OpenAPI-Specification/blob/main/examples/v2.0/json/petstore-simple.json
 */
private fun Application.myModule() {

    data class Pet(
        val id: Long,
        val name: String,
        val tag: String
    )

    data class NewPet(
        val name: String,
        val tag: String
    )

    data class ErrorModel(
        val message: String
    )

    install(OpenApi) {
        info {
            title = "Swagger Petstore"
            version = "1.0.0"
            description = "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification"
            termsOfService = "http://swagger.io/terms/"
            contact {
                name = "Swagger API Team"
            }
            license {
                name = "MIT"
            }
        }
        examples {
            example("Unexpected Error") {
                value = ErrorModel("Unexpected Error")
            }
        }
    }

    routing {

        // add the routes for OpenAPI spec, Swagger UI and ReDoc
        route("swagger") {
            swaggerUI("/api.json")
        }
        route("api.json") {
            openApi()
        }
        route("redoc") {
            redoc("/api.json") {
                hideLoading = true
            }
        }

        route("/pets") {

            get({
                operationId = "findPets"
                description = "Returns all pets from the system that the user has access to"
                request {
                    queryParameter<List<String>>("tags") {
                        description = "tags to filter by"
                        required = false
                        example("dog") {
                            value = "default"
                        }
                    }
                    queryParameter<Int>("limit") {
                        description = "maximum number of results to return"
                        required = false
                        example("default") {
                            value = 100
                        }
                    }
                }
                response {
                    code(HttpStatusCode.OK) {
                        body<List<Pet>> {
                            description = "the list of available pets"
                            example("Pet List") {
                                value = listOf(
                                    Pet(
                                        id = 123,
                                        name = "Big Bird",
                                        tag = "bird"
                                    ),
                                    Pet(
                                        id = 456,
                                        name = "Charlie",
                                        tag = "dog"
                                    )
                                )
                            }
                        }
                    }
                    default {
                        body<ErrorModel> {
                            description = "unexpected error"
                            exampleRef("Unexpected Error")
                        }
                    }
                }
            }) {
                call.respond(HttpStatusCode.NotImplemented, Unit)
            }

            post({
                operationId = "addPet"
                description = "Creates a new pet in the store. Duplicates are allowed"
                request {
                    body<NewPet> {
                        description = "Pet to add to the store"
                        required = true
                        example("New Bird") {
                            value = NewPet(
                                name = "Big Bird",
                                tag = "bird"
                            )
                        }
                        example("New Dog") {
                            value = NewPet(
                                name = "Charlie",
                                tag = "dog"
                            )
                        }
                    }
                }
                response {
                    code(HttpStatusCode.OK) {
                        body<Pet> {
                            description = "the created pet"
                            example("Bird") {
                                value = Pet(
                                    id = 123,
                                    name = "Big Bird",
                                    tag = "bird"
                                )
                            }
                            example("Dog") {
                                value = Pet(
                                    id = 456,
                                    name = "Charlie",
                                    tag = "dog"
                                )
                            }
                        }
                    }
                    default {
                        body<ErrorModel> {
                            description = "unexpected error"
                            exampleRef("Unexpected Error")
                        }
                    }
                }
            }) {
                call.respond(HttpStatusCode.NotImplemented, Unit)
            }

            route("{id}") {

                get({
                    operationId = "findBetById"
                    description = "Returns a pet based on a single ID."
                    request {
                        pathParameter<Long>("id") {
                            description = "Id of pet to fetch"
                            required = true
                            example("default") {
                                value = 123L
                            }
                        }
                    }
                    response {
                        code(HttpStatusCode.OK) {
                            body<Pet>{
                                description = "the pet with the given id"
                                example("Bird") {
                                    value = Pet(
                                        id = 123,
                                        name = "Big Bird",
                                        tag = "bird"
                                    )
                                }
                                example("Dog") {
                                    value = Pet(
                                        id = 123,
                                        name = "Charlie",
                                        tag = "dog"
                                    )
                                }
                            }
                        }
                        code(HttpStatusCode.NotFound) {
                            description = "the pet with the given id was not found"
                        }
                        default {
                            body<ErrorModel> {
                                description = "unexpected error"
                                exampleRef("Unexpected Error")
                            }
                        }
                    }
                }) {
                    call.respond(HttpStatusCode.NotImplemented, Unit)
                }

                delete({
                    operationId = "deletePet"
                    description = "deletes a single pet based on the supplied ID"
                    request {
                        pathParameter<Long>("id") {
                            description = "Id of pet to delete"
                            required = true
                            example("default") {
                                value = 123L
                            }
                        }
                    }
                    response {
                        code(HttpStatusCode.NoContent) {
                            description = "the pet was successfully deleted"
                        }
                        code(HttpStatusCode.NotFound) {
                            description = "the pet with the given id was not found"
                        }
                        default {
                            body<ErrorModel> {
                                description = "unexpected error"
                                exampleRef("Unexpected Error")
                            }
                        }
                    }
                }) {
                    call.respond(HttpStatusCode.NotImplemented, Unit)
                }
            }
        }
    }
}