@@ -28,209 +28,6 @@ compile "cc.vileda:kotlin-openapi3-dsl:1.2.0"
28
28
29
29
for a complete example [ look at the test] ( src/test/kotlin/cc/vileda/openapi/dsl/OpenApiDslTest.kt )
30
30
31
-
32
- ### complete vertx.io example
33
-
34
- ``` kotlin
35
-
36
- import cc.vileda.openapi.dsl.*
37
- import io.swagger.oas.models.parameters.Parameter
38
- import io.swagger.oas.models.security.SecurityScheme
39
- import io.vertx.core.Handler
40
- import io.vertx.core.json.JsonObject.mapFrom
41
- import io.vertx.kotlin.core.http.HttpServerOptions
42
- import io.vertx.reactivex.core.Vertx
43
- import io.vertx.reactivex.ext.web.Router
44
- import io.vertx.reactivex.ext.web.api.contract.openapi3.OpenAPI3RouterFactory
45
- import io.vertx.reactivex.ext.web.handler.CorsHandler
46
-
47
- private val vertx = Vertx .vertx()
48
-
49
- data class HelloResponse (
50
- val message : String
51
- )
52
-
53
- data class HelloRequest (
54
- val message : String
55
- )
56
-
57
- private val api3 = openapiDsl {
58
- info {
59
- title = " test api"
60
- version = " 0.0.1"
61
- }
62
-
63
- components {
64
- schema<HelloResponse >()
65
- schema<HelloRequest >()
66
- securityScheme {
67
- name = " apiKey"
68
- type = SecurityScheme .Type .APIKEY
69
- `in ` = SecurityScheme .In .HEADER
70
- }
71
- }
72
-
73
- security {
74
- put(" apiKey" , emptyList())
75
- }
76
-
77
- paths {
78
- path(" /hello" ) {
79
- get {
80
- tags = listOf (" without params" )
81
- operationId = " hello"
82
- description = " hello get"
83
- parameter {
84
- name = " id"
85
- `in ` = " query"
86
- required = true
87
- style = Parameter .StyleEnum .SIMPLE
88
- schema<String >()
89
- }
90
- responses {
91
- response(" 200" ) {
92
- description = " a 200 response"
93
- content {
94
- mediaTypeRef<HelloResponse >(" application/json" ) {
95
- description = " Hello response"
96
- example = HelloResponse (" World" )
97
- }
98
- }
99
- }
100
- }
101
- }
102
- post {
103
- tags = listOf (" without params" )
104
- operationId = " postHello"
105
- description = " hello post"
106
- extension(" x-stable" , true )
107
- responses {
108
- response(" 201" ) {
109
- description = " created response"
110
- requestBody {
111
- content {
112
- mediaTypeRef<HelloRequest >(" application/json" ) {
113
- description = " Hello request"
114
- example(HelloRequest (" World" )) {
115
- description = " hello request"
116
- }
117
- }
118
- }
119
- }
120
- content {
121
- mediaType<HelloResponse >(" application/json" ) {
122
- description = " Hello response"
123
- example = HelloResponse (" World" )
124
- }
125
- }
126
- }
127
- }
128
- }
129
- }
130
- path(" /greetings" ) {
131
- get {
132
- tags = listOf (" without params" )
133
- operationId = " greetings"
134
- description = " greetings get"
135
- responses {
136
- response(" 200" ){
137
- description = " a 200 response"
138
- content {
139
- mediaTypeArrayOfRef<HelloResponse >(" application/json" ) {
140
- description = " An array of HelloResponses"
141
- example = HelloResponse (" Greetings" )
142
- }
143
- }
144
- }
145
- }
146
- }
147
- }
148
- }
149
- }
150
-
151
- fun main (args : Array <String >) {
152
- val apiFile = api3.asFile()
153
- println (api3.asJson().toString(2 ))
154
- OpenAPI3RouterFactory .rxCreateRouterFactoryFromFile(vertx, apiFile.absolutePath)
155
- .doOnError { it.printStackTrace() }
156
- .doOnSuccess(::createOperationHandlers)
157
- .doOnSubscribe { println (" Server started" ) }
158
- .subscribe(::startServer)
159
- }
160
-
161
- fun startServer (routerFactory : OpenAPI3RouterFactory ) {
162
- val mainRouter = Router .router(vertx)
163
- bindAdditionalHandlers(mainRouter)
164
- mainRouter.mountSubRouter(" /" , routerFactory.router)
165
- val server = vertx.createHttpServer(HttpServerOptions (
166
- port = 8080 ,
167
- host = " localhost" ))
168
- server.requestHandler({ mainRouter.accept(it) }).listen(8080 )
169
- }
170
-
171
- fun bindAdditionalHandlers (router : Router ) {
172
- val create = CorsHandler .create(" ^.*$" )
173
- .allowedHeaders(setOf (
174
- " Content-Type" ,
175
- " apiKey"
176
- ))
177
- router.route().handler(create)
178
-
179
- router.get(" /spec.json" ).handler { routingContext ->
180
- routingContext.response()
181
- .putHeader(" Content-Type" , " application/json" )
182
- .end(api3.asJson().toString(2 ))
183
- }
184
- }
185
-
186
- private fun createOperationHandlers (routerFactory : OpenAPI3RouterFactory ) {
187
- routerFactory.addSecurityHandler(" apiKey" , Handler {
188
- if (it.request().getHeader(" apiKey" ) == " foo" ) it.next()
189
- else it.fail(401 )
190
- })
191
-
192
- routerFactory.addHandlerByOperationId(" postHello" , { routingContext ->
193
- routingContext.response()
194
- .putHeader(" Content-Type" , " application/json" )
195
- .end(mapFrom(HelloResponse (" Hello World!" )).encode())
196
- })
197
-
198
- routerFactory.addHandlerByOperationId(" hello" , { routingContext ->
199
- routingContext.response()
200
- .putHeader(" Content-Type" , " application/json" )
201
- .end(mapFrom(HelloResponse (" Hello World!" )).encode())
202
- })
203
-
204
- routerFactory.addFailureHandlerByOperationId(" hello" , { routingContext ->
205
- println (" FAIL" )
206
- routingContext.fail(500 )
207
- })
208
- }
209
- ```
210
-
211
- ## todo
212
-
213
- - [x] Make compatible to vertx [ OpenAPI3RouterFactory] ( src/main/kotlin/OpenApi3Vertx.kt )
214
- - [ ] Implement all OpenApi3 fields
215
- - [ ] paths
216
- - [x] all HTTP methods
217
- - [x] minimal features
218
- - [x] security features
219
- - [ ] complete features
220
- - [ ] requestBody
221
- - [x] minimal features
222
- - [ ] examples
223
- - [ ] complete features
224
- - [ ] parameters
225
- - [x] minimal features
226
- - [ ] complete features
227
- - [x] components
228
- - [x] $ref to components
229
- - [x] Publish on bintray
230
- - [x] Publish on jcenter
231
- - [x] Publish on maven central
232
-
233
-
234
31
## license
235
32
```
236
33
Copyright 2017 Tristan Leo
0 commit comments