Skip to content

Commit 8ef4ca9

Browse files
committed
replace java class with kclass
1 parent 88fd736 commit 8ef4ca9

File tree

6 files changed

+62
-59
lines changed

6 files changed

+62
-59
lines changed

src/main/kotlin/io/github/smiley4/ktorswaggerui/apispec/OApiSchemaGenerator.kt

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.github.smiley4.ktorswaggerui.apispec
33
import io.github.smiley4.ktorswaggerui.routing.SchemaRef
44
import io.swagger.v3.oas.models.media.Schema
55
import java.math.BigDecimal
6+
import kotlin.reflect.KClass
67

78
/**
89
* Generator for an OpenAPI Schema Object
@@ -12,177 +13,177 @@ class OApiSchemaGenerator {
1213
/**
1314
* Generate the Content Object from the given config
1415
*/
15-
fun generate(schema: Class<*>): Schema<Any> {
16+
fun generate(schema: KClass<*>): Schema<Any> {
1617
return Schema<Any>().apply {
1718
when (schema) {
18-
Byte::class.java -> {
19+
Byte::class -> {
1920
type = "integer"
2021
minimum = BigDecimal.valueOf(Byte.MIN_VALUE.toLong())
2122
maximum = BigDecimal.valueOf(Byte.MAX_VALUE.toLong())
2223
}
23-
Short::class.java -> {
24+
Short::class -> {
2425
type = "integer"
2526
minimum = BigDecimal.valueOf(Short.MIN_VALUE.toLong())
2627
maximum = BigDecimal.valueOf(Short.MAX_VALUE.toLong())
2728
}
28-
Int::class.java -> {
29+
Int::class -> {
2930
type = "integer"
3031
format = "int32"
3132
}
32-
Long::class.java -> {
33+
Long::class -> {
3334
type = "integer"
3435
format = "int64"
3536
}
36-
UByte::class.java -> {
37+
UByte::class -> {
3738
type = "integer"
3839
minimum = BigDecimal.valueOf(UByte.MIN_VALUE.toLong())
3940
maximum = BigDecimal.valueOf(UByte.MAX_VALUE.toLong())
4041
}
41-
UShort::class.java -> {
42+
UShort::class -> {
4243
type = "integer"
4344
minimum = BigDecimal.valueOf(UShort.MIN_VALUE.toLong())
4445
maximum = BigDecimal.valueOf(UShort.MAX_VALUE.toLong())
4546
}
46-
UInt::class.java -> {
47+
UInt::class -> {
4748
type = "integer"
4849
minimum = BigDecimal.valueOf(UInt.MIN_VALUE.toLong())
4950
maximum = BigDecimal.valueOf(UInt.MAX_VALUE.toLong())
5051
}
51-
ULong::class.java -> {
52+
ULong::class -> {
5253
type = "integer"
5354
minimum = BigDecimal.valueOf(ULong.MIN_VALUE.toLong())
5455
maximum = BigDecimal.valueOf(ULong.MAX_VALUE.toLong())
5556
}
56-
Float::class.java -> {
57+
Float::class -> {
5758
type = "number"
5859
format = "float"
5960
}
60-
Double::class.java -> {
61+
Double::class -> {
6162
type = "number"
6263
format = "double"
6364
}
64-
Boolean::class.java -> {
65+
Boolean::class -> {
6566
type = "boolean"
6667
}
67-
Char::class.java -> {
68+
Char::class -> {
6869
type = "string"
6970
minLength = 1
7071
maxLength = 1
7172
}
72-
String::class.java -> {
73+
String::class -> {
7374
type = "string"
7475
}
7576

76-
Array<Byte>::class.java, ByteArray::class.java -> {
77+
Array<Byte>::class, ByteArray::class -> {
7778
type = "array"
7879
items = Schema<String>().apply {
7980
type = "integer"
8081
minimum = BigDecimal.valueOf(Byte.MIN_VALUE.toLong())
8182
maximum = BigDecimal.valueOf(Byte.MAX_VALUE.toLong())
8283
}
8384
}
84-
Array<Short>::class.java, ShortArray::class.java -> {
85+
Array<Short>::class, ShortArray::class -> {
8586
type = "array"
8687
items = Schema<String>().apply {
8788
type = "integer"
8889
minimum = BigDecimal.valueOf(Short.MIN_VALUE.toLong())
8990
maximum = BigDecimal.valueOf(Short.MAX_VALUE.toLong())
9091
}
9192
}
92-
Array<Int>::class.java, IntArray::class.java -> {
93+
Array<Int>::class, IntArray::class -> {
9394
type = "array"
9495
items = Schema<String>().apply {
9596
type = "integer"
9697
format = "int32"
9798
}
9899
}
99-
Array<Long>::class.java, LongArray::class.java -> {
100+
Array<Long>::class, LongArray::class -> {
100101
type = "array"
101102
items = Schema<String>().apply {
102103
type = "integer"
103104
format = "int64"
104105
}
105106
}
106-
Array<UByte>::class.java -> {
107+
Array<UByte>::class -> {
107108
type = "array"
108109
items = Schema<String>().apply {
109110
type = "integer"
110111
minimum = BigDecimal.valueOf(UByte.MIN_VALUE.toLong())
111112
maximum = BigDecimal.valueOf(UByte.MAX_VALUE.toLong())
112113
}
113114
}
114-
Array<UShort>::class.java -> {
115+
Array<UShort>::class -> {
115116
type = "array"
116117
items = Schema<String>().apply {
117118
type = "integer"
118119
minimum = BigDecimal.valueOf(UShort.MIN_VALUE.toLong())
119120
maximum = BigDecimal.valueOf(UShort.MAX_VALUE.toLong())
120121
}
121122
}
122-
Array<UInt>::class.java -> {
123+
Array<UInt>::class -> {
123124
type = "array"
124125
items = Schema<String>().apply {
125126
type = "integer"
126127
minimum = BigDecimal.valueOf(UInt.MIN_VALUE.toLong())
127128
maximum = BigDecimal.valueOf(UInt.MAX_VALUE.toLong())
128129
}
129130
}
130-
Array<ULong>::class.java -> {
131+
Array<ULong>::class -> {
131132
type = "array"
132133
items = Schema<String>().apply {
133134
type = "integer"
134135
minimum = BigDecimal.valueOf(ULong.MIN_VALUE.toLong())
135136
maximum = BigDecimal.valueOf(ULong.MAX_VALUE.toLong())
136137
}
137138
}
138-
Array<Float>::class.java, FloatArray::class.java -> {
139+
Array<Float>::class, FloatArray::class -> {
139140
type = "array"
140141
items = Schema<String>().apply {
141142
type = "number"
142143
format = "float"
143144
}
144145
}
145-
Array<Double>::class.java, DoubleArray::class.java -> {
146+
Array<Double>::class, DoubleArray::class -> {
146147
type = "array"
147148
items = Schema<String>().apply {
148149
type = "number"
149150
format = "double"
150151
}
151152
}
152153

153-
Array<Boolean>::class.java, BooleanArray::class.java -> {
154+
Array<Boolean>::class, BooleanArray::class -> {
154155
type = "array"
155156
items = Schema<String>().apply {
156157
type = "boolean"
157158
}
158159
}
159-
Array<Char>::class.java -> {
160+
Array<Char>::class -> {
160161
type = "array"
161162
items = Schema<String>().apply {
162163
type = "string"
163164
minLength = 1
164165
maxLength = 1
165166
}
166167
}
167-
Array<String>::class.java -> {
168+
Array<String>::class -> {
168169
type = "array"
169170
items = Schema<String>().apply {
170171
type = "string"
171172
}
172173
}
173174
else -> {
174-
if (schema.isArray) {
175+
if (schema.java.isArray) {
175176
type = "array"
176177
items = Schema<String>().apply {
177178
type = "object"
178-
`$ref` = SchemaRef.refOfClass(schema.componentType)
179+
`$ref` = SchemaRef.refOfClass(schema.java.componentType)
179180
}
180-
} else if (schema.isEnum) {
181+
} else if (schema.java.isEnum) {
181182
type = "string"
182-
enum = schema.enumConstants.map { it.toString() }
183+
enum = schema.java.enumConstants.map { it.toString() }
183184
} else {
184185
type = "object"
185-
`$ref` = SchemaRef.refOfClass(schema)
186+
`$ref` = SchemaRef.refOfClass(schema.java)
186187
}
187188
}
188189
}

src/main/kotlin/io/github/smiley4/ktorswaggerui/documentation/RouteDocumentation.kt

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.smiley4.ktorswaggerui.documentation
22

33
import io.ktor.http.HttpStatusCode
4+
import kotlin.reflect.KClass
45

56
class RouteDocumentation {
67

@@ -62,24 +63,24 @@ class RequestDocumentation {
6263
*/
6364
private val parameters = mutableListOf<RequestParameterDocumentation>()
6465

65-
fun pathParameter(name: String, schema: Class<*>, block: RequestParameterDocumentation.() -> Unit) {
66+
fun pathParameter(name: String, schema: KClass<*>, block: RequestParameterDocumentation.() -> Unit) {
6667
parameters.add(RequestParameterDocumentation(name, schema, RequestParameterDocumentation.Location.PATH).apply(block))
6768
}
6869

69-
fun pathParameter(name: String, schema: Class<*>) = pathParameter(name, schema) {}
70+
fun pathParameter(name: String, schema: KClass<*>) = pathParameter(name, schema) {}
7071

71-
fun queryParameter(name: String, schema: Class<*>, block: RequestParameterDocumentation.() -> Unit) {
72+
fun queryParameter(name: String, schema: KClass<*>, block: RequestParameterDocumentation.() -> Unit) {
7273
parameters.add(RequestParameterDocumentation(name, schema, RequestParameterDocumentation.Location.QUERY).apply(block))
7374
}
7475

75-
fun queryParameter(name: String, schema: Class<*>) = queryParameter(name, schema) {}
76+
fun queryParameter(name: String, schema: KClass<*>) = queryParameter(name, schema) {}
7677

7778

78-
fun headerParameter(name: String, schema: Class<*>, block: RequestParameterDocumentation.() -> Unit) {
79+
fun headerParameter(name: String, schema: KClass<*>, block: RequestParameterDocumentation.() -> Unit) {
7980
parameters.add(RequestParameterDocumentation(name, schema, RequestParameterDocumentation.Location.HEADER).apply(block))
8081
}
8182

82-
fun headerParameter(name: String, schema: Class<*>) = headerParameter(name, schema) {}
83+
fun headerParameter(name: String, schema: KClass<*>) = headerParameter(name, schema) {}
8384

8485

8586
fun getParameters(): List<RequestParameterDocumentation> = parameters
@@ -90,11 +91,11 @@ class RequestDocumentation {
9091
*/
9192
private var body: BodyDocumentation? = null
9293

93-
fun body(schema: Class<*>, block: BodyDocumentation.() -> Unit) {
94+
fun body(schema: KClass<*>, block: BodyDocumentation.() -> Unit) {
9495
body = BodyDocumentation(schema).apply(block)
9596
}
9697

97-
fun body(schema: Class<*>) = body(schema) {}
98+
fun body(schema: KClass<*>) = body(schema) {}
9899

99100
fun getBody() = body
100101

@@ -109,13 +110,13 @@ class RequestParameterDocumentation(
109110
/**
110111
* The schema defining the type used for the parameter.
111112
* Examples:
112-
* - Int::class.java
113-
* - UByte::class.java
114-
* - BooleanArray::class.java
115-
* - Array<String>::class.java
116-
* - Array<MyClass>::class.java
113+
* - Int::class
114+
* - UByte::class
115+
* - BooleanArray::class
116+
* - Array<String>::class
117+
* - Array<MyClass>::class
117118
*/
118-
val schema: Class<*>,
119+
val schema: KClass<*>,
119120
/**
120121
* Location of the parameter
121122
*/
@@ -198,11 +199,11 @@ class SingleResponseDocumentation(val statusCode: HttpStatusCode) {
198199
*/
199200
private var body: BodyDocumentation? = null
200201

201-
fun body(schema: Class<*>, block: BodyDocumentation.() -> Unit) {
202+
fun body(schema: KClass<*>, block: BodyDocumentation.() -> Unit) {
202203
body = BodyDocumentation(schema).apply(block)
203204
}
204205

205-
fun body(schema: Class<*>) = body(schema) {}
206+
fun body(schema: KClass<*>) = body(schema) {}
206207

207208
fun getBody() = body
208209

@@ -222,7 +223,7 @@ class BodyDocumentation(
222223
* - Array<String>::class.java
223224
* - Array<MyClass>::class.java
224225
*/
225-
val schema: Class<*>,
226+
val schema: KClass<*>,
226227
) {
227228

228229
/**

src/main/kotlin/io/github/smiley4/ktorswaggerui/routing/SwaggerRouting.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import io.ktor.server.routing.get
2020
import io.ktor.server.routing.routing
2121
import mu.KotlinLogging
2222
import java.net.URL
23+
import kotlin.reflect.KClass
2324

2425
/**
2526
* Registers and handles routes required for the swagger-ui

src/test/kotlin/io/github/smiley4/ktorswaggerui/examples/AuthExample.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fun main() {
5757
response {
5858
HttpStatusCode.OK to {
5959
description = "Successful Request"
60-
body(String::class.java) { description = "the response" }
60+
body(String::class) { description = "the response" }
6161
}
6262
}
6363
}) {

src/test/kotlin/io/github/smiley4/ktorswaggerui/examples/BasicExample.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fun main() {
3434
response {
3535
HttpStatusCode.OK to {
3636
description = "Successful Request"
37-
body(String::class.java) { description = "the response" }
37+
body(String::class) { description = "the response" }
3838
}
3939
HttpStatusCode.InternalServerError to {
4040
description = "Something unexpected happened"

src/test/kotlin/io/github/smiley4/ktorswaggerui/examples/CompleteExample.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fun main() {
5353
response {
5454
HttpStatusCode.OK to {
5555
description = "Successful Request"
56-
body(String::class.java) { description = "the response" }
56+
body(String::class) { description = "the response" }
5757
}
5858
HttpStatusCode.InternalServerError to {
5959
description = "Something unexpected happened"
@@ -66,15 +66,15 @@ fun main() {
6666
tags = listOf("test")
6767
description = "Performs the given operation on the given values and returns the result"
6868
request {
69-
pathParameter("operation", String::class.java) {
69+
pathParameter("operation", String::class) {
7070
description = "the math operation to perform. Either 'add' or 'sub'"
7171
}
72-
body(MathRequest::class.java)
72+
body(MathRequest::class)
7373
}
7474
response {
7575
HttpStatusCode.OK to {
7676
description = "The operation was successful"
77-
body(MathResult::class.java) {
77+
body(MathResult::class) {
7878
description = "The result of the operation"
7979
}
8080
}
@@ -95,7 +95,7 @@ fun main() {
9595
post("random/results", {
9696
response {
9797
HttpStatusCode.OK to {
98-
body(Array<MathResult>::class.java)
98+
body(Array<MathResult>::class)
9999
}
100100
}
101101
}) {
@@ -104,19 +104,19 @@ fun main() {
104104
post("random/numbers", {
105105
response {
106106
HttpStatusCode.OK to {
107-
body(IntArray::class.java)
107+
body(IntArray::class)
108108
}
109109
}
110110
}) {
111111
call.respond(HttpStatusCode.OK, (0..5).map { Random().nextInt() })
112112
}
113113
post("echo/{color}", {
114114
request {
115-
pathParameter("color", Color::class.java)
115+
pathParameter("color", Color::class)
116116
}
117117
response {
118118
HttpStatusCode.OK to {
119-
body(String::class.java)
119+
body(String::class)
120120
}
121121
}
122122
}) {

0 commit comments

Comments
 (0)