-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
OpenApiDslTest.kt
204 lines (193 loc) · 8.12 KB
/
OpenApiDslTest.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
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
package cc.vileda.openapi.dsl
import io.kotlintest.matchers.instanceOf
import io.kotlintest.shouldBe
import io.kotlintest.shouldNotBe
import io.kotlintest.specs.StringSpec
import io.swagger.v3.oas.models.PathItem
import io.swagger.v3.oas.models.media.*
import io.swagger.v3.oas.models.parameters.Parameter
import io.swagger.v3.oas.models.security.SecurityScheme
import java.math.BigDecimal
import java.time.LocalDate
import java.time.LocalDateTime
import java.util.*
data class ExampleSchema(val foo: String)
data class AnotherExampleSchema(val bar: String)
data class ExampleRequestSchema(val foo: String)
data class ListExampleSchema(val baz: List<ExampleSchema>)
@Suppress("unused")
enum class ExampleEnum {
ONE,
TWO
}
class OpenApi3BuilderTest : StringSpec() {
init {
val api = openapiDsl {
openapi = "3.0.0"
tag {
name = "example_tag"
description = "an example tag"
extension("x-beta", true)
}
externalDocs {
description = "developer hub"
url = "http://hub.api"
}
server {
url = "http://localhost"
description = "localhost server"
variables {
variable("foo") {
addEnumItem("bar")
extension("x-foo", "bar")
}
}
}
extension("x-stable", true)
extension("x-internal", true)
components {
schema<ExampleSchema>()
schema<ExampleRequestSchema>()
schema<AnotherExampleSchema>()
schema<ListExampleSchema>()
schema<ExampleEnum>()
securityScheme("bearer") {
type = SecurityScheme.Type.OPENIDCONNECT
openIdConnectUrl = "http://localhost:8080/auth"
flows {
implicit {
authorizationUrl = "http://localhost:8080/auth"
refreshUrl = "http://localhost:8080/token"
tokenUrl = "http://localhost:8080/token"
extension("x-internal", true)
scopes {
scope("foo", "foo:read")
}
}
}
}
}
security {
put(SecurityScheme.Type.OPENIDCONNECT.toString(), listOf("bar"))
}
info {
title = "jjjj"
version = "1.0"
}
paths {
path("foo") {
extension("x-experimental", true)
post {
description = "test"
tags = listOf("tag1")
extension("x-version", "3.0")
parameter {
name = "id"
`in` = "path"
style = Parameter.StyleEnum.SIMPLE
schema<String>()
}
parameter {
name = "name"
`in` = "query"
style = Parameter.StyleEnum.SIMPLE
required = true
schema<String>()
}
parameter {
name = "firstname"
`in` = "query"
style = Parameter.StyleEnum.DEEPOBJECT
required = false
deprecated = true
content {
mediaTypeRef<ExampleSchema>("application/json")
}
}
requestBody {
description = "example request"
required = true
content {
mediaTypeRef<ExampleRequestSchema>("application/json") {
description = "request schema"
example(ExampleRequestSchema("bar")) {
description = "example schema value"
}
}
}
}
responses {
response("foo") {
description = "bar"
content {
mediaTypeRef<AnotherExampleSchema>("application/json") {
description = "bar"
example(AnotherExampleSchema("bar")) {
description = "example schema value"
}
}
}
}
}
}
get {
responses {
response("200") {
content {
mediaTypeArrayOfRef<ExampleSchema>("application/json") {
example(ExampleSchema("foo")) {
}
}
}
}
}
}
}
}
}
"builder should set openapi fields" {
api shouldNotBe null
api.openapi shouldBe "3.0.0"
api.info.title shouldBe "jjjj"
api.info.version shouldBe "1.0"
val securityScheme =
api.components.securitySchemes["bearer"]
securityScheme shouldNotBe null
securityScheme!!.flows!!.implicit shouldNotBe null
securityScheme.flows.implicit!!.extensions!!["x-internal"] shouldBe true
securityScheme.type shouldBe SecurityScheme.Type.OPENIDCONNECT
val postOp = api.paths["foo"]!!.readOperationsMap()!![PathItem.HttpMethod.POST]
postOp shouldNotBe null
postOp!!.extensions!!["x-version"] shouldBe "3.0"
postOp.responses["foo"]!!.description shouldBe "bar"
postOp.responses["foo"]!!.content!!["application/json"]!!.schema.javaClass shouldBe Schema<AnotherExampleSchema>().javaClass
}
"openapi should render as valid json spec" {
val asJson = validatedJson(api)
println(asJson.toString(2))
}
"findSchema should return valid schema" {
findSchema<String>() shouldBe StringSchema()
findSchema<Int>() shouldBe IntegerSchema()
findSchema<Boolean>() shouldBe BooleanSchema()
findSchema<Long>() shouldBe IntegerSchema().format("int64")
findSchema<BigDecimal>() shouldBe IntegerSchema().format("")
findSchema<Date>() shouldBe DateSchema()
findSchema<LocalDate>() shouldBe DateSchema()
findSchema<LocalDateTime>() shouldBe DateTimeSchema()
findSchema<ExampleRequestSchema>()?.javaClass shouldBe Schema<ExampleRequestSchema>().javaClass
}
"findSchema should return a valid schema for an enum" {
val schema = findSchema<ExampleEnum>()
schema shouldNotBe null
schema?.type shouldBe "string"
schema?.enum shouldBe listOf("ONE", "TWO")
}
"findSchema should return a valid schema for a list" {
val schema = api.paths["foo"]!!.readOperationsMap()!![PathItem.HttpMethod.GET]!!.responses["200"]!!.content!!["application/json"]!!.schema
schema.type shouldBe "array"
schema shouldBe instanceOf(ArraySchema()::class)
(schema as ArraySchema).items.`$ref` shouldBe "#/components/schemas/ExampleSchema"
}
}
}