Skip to content

Commit

Permalink
add support for array types
Browse files Browse the repository at this point in the history
  • Loading branch information
angryziber committed Aug 22, 2023
1 parent 1d03094 commit 10d8298
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
19 changes: 11 additions & 8 deletions openapi/src/OpenAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,16 @@ private fun toParameterIn(paramAnnotation: Annotation?) = when(paramAnnotation)

private fun KType.toJsonSchema(response: Boolean = false): Map<String, Any>? {
val cls = classifier as? KClass<*> ?: return null
val jsonType = when (cls) {
Nothing::class -> "null"
Boolean::class -> "boolean"
Number::class -> "integer"
BigDecimal::class, Decimal::class, Float::class, Double::class -> "number"
else -> if (cls == String::class || Converter.supports(cls)) "string" else "object"
val jsonType = when {
cls == Nothing::class -> "null"
cls == Boolean::class -> "boolean"
cls == BigDecimal::class || cls == Decimal::class || cls == Float::class || cls == Double::class -> "number"
cls.isSubclassOf(Number::class) -> "integer"
cls.isSubclassOf(Array::class) || cls.isSubclassOf(Iterable::class) -> "array"
cls.isSubclassOf(CharSequence::class) || Converter.supports(cls) -> "string"
else -> "object"
}
val jsonFormat = when (cls) {
val jsonStringFormat = when (cls) {
LocalDate::class, Date::class -> "date"
LocalTime::class -> "time"
Instant::class, LocalDateTime::class -> "date-time"
Expand All @@ -107,7 +109,8 @@ private fun KType.toJsonSchema(response: Boolean = false): Map<String, Any>? {
}
return mapOfNotNull(
"type" to jsonType,
"format" to jsonFormat,
"format" to jsonStringFormat,
"items" to if (jsonType == "array") arguments.firstOrNull()?.type?.toJsonSchema() else null,
"enum" to if (cls.isSubclassOf(Enum::class)) cls.java.enumConstants.toList() else null,
"properties" to if (jsonType == "object") cls.publicProperties.associate { it.name to it.returnType.toJsonSchema(response) }.takeIf { it.isNotEmpty() } else null,
"required" to if (jsonType == "object") cls.publicProperties.filter { p ->
Expand Down
4 changes: 2 additions & 2 deletions openapi/test/OpenAPITest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ class OpenAPITest {

@Test fun parameters() {
class MyRoutes {
fun withParams(@PathParam date: LocalDate, @QueryParam simpleString: String?, @CookieParam dow: DayOfWeek) = null
fun withParams(@PathParam date: LocalDate, @QueryParam simpleString: String?, @CookieParam dow: List<DayOfWeek>) = null
}
expect(FunHandler(MyRoutes(), MyRoutes::withParams).params.filter { it.source != null }.map { toParameter(it) }).toContainExactly(
mapOf("name" to "date", "required" to true, "in" to PATH, "schema" to mapOf("type" to "string", "format" to "date")),
mapOf("name" to "simpleString", "required" to false, "in" to QUERY, "schema" to mapOf("type" to "string")),
mapOf("name" to "dow", "required" to true, "in" to COOKIE, "schema" to mapOf("type" to "string", "enum" to DayOfWeek.values().toList()))
mapOf("name" to "dow", "required" to true, "in" to COOKIE, "schema" to mapOf("type" to "array", "items" to mapOf("type" to "string", "enum" to DayOfWeek.values().toList())))
)
}

Expand Down

0 comments on commit 10d8298

Please sign in to comment.