Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public fun SerialDescriptor.buildJsonSchema(
}
val annotations = includeAnnotations + annotations

// For inline descriptors, use the delegate descriptor
if (this.isInline) {
return this.getElementDescriptor(0)
.buildJsonSchema(visiting = visiting)
}

return when (kind) {
StructureKind.CLASS, StructureKind.OBJECT -> {
visiting += nonNullSerialName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ public class ReflectionJsonSchemaInference(
Boolean::class -> jsonSchemaFromAnnotations(annotations, ::schemaRefForClass, type = JsonType.BOOLEAN)

Byte::class, Short::class, Int::class, Long::class,
UByte::class, UShort::class, UInt::class, ULong::class,
java.lang.Byte::class, java.lang.Short::class, Integer::class, java.lang.Long::class ->
jsonSchemaFromAnnotations(annotations, ::schemaRefForClass, type = JsonType.INTEGER)

Expand Down Expand Up @@ -298,7 +299,12 @@ public class ReflectionJsonSchemaInference(
annotations,
::schemaRefForClass,
type = JsonType.STRING.orNullable(nullable),
format = "date-time"
)

kotlin.time.Duration::class -> jsonSchemaFromAnnotations(
annotations,
::schemaRefForClass,
type = JsonType.STRING.orNullable(nullable),
)

LocalDate::class -> jsonSchemaFromAnnotations(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type: object
title: io.ktor.openapi.reflect.TimeTypes
required:
- instant
- duration
properties:
instant:
type: string
duration:
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type: object
title: io.ktor.openapi.reflect.UnsignedTypes
required:
- unsignedInt
- unsignedLong
properties:
unsignedInt:
type: integer
unsignedLong:
type: integer
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import kotlinx.serialization.encodeToString
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.time.Duration
import kotlin.time.ExperimentalTime
import kotlin.time.Instant

abstract class AbstractSchemaInferenceTest(
val inference: JsonSchemaInference
Expand Down Expand Up @@ -105,6 +108,14 @@ abstract class AbstractSchemaInferenceTest(
)
}

@Test
open fun `unsigned types`() =
assertSchemaMatches<UnsignedTypes>()

@Test
fun `time types`() =
assertSchemaMatches<TimeTypes>()

private inline fun <reified T : Any> assertSchemaMatches() {
val schema = inference.jsonSchema<T>()
val expected = readSchemaYaml<T>()
Expand Down Expand Up @@ -156,6 +167,19 @@ enum class Color {
BLUE
}

@Serializable
data class UnsignedTypes(
val unsignedInt: UInt,
val unsignedLong: ULong
)

@OptIn(ExperimentalTime::class)
@Serializable
data class TimeTypes(
val instant: Instant,
val duration: Duration
)

@Serializable
sealed class Shape {
@Serializable
Expand Down Expand Up @@ -197,4 +221,7 @@ data class LogicalOperatorsData(
)

@Serializable
data class TreeNode(val name: String, val parent: TreeNode?)
data class TreeNode(
val name: String,
val parent: TreeNode?
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

package io.ktor.openapi.reflect

import io.ktor.openapi.KotlinxJsonSchemaInference
import io.ktor.openapi.*

class KotlinxJsonSchemaInferenceTest : AbstractSchemaInferenceTest(KotlinxJsonSchemaInference)