Skip to content

Commit

Permalink
Minor schema fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownJoe796 committed Nov 1, 2024
1 parent 1cec44b commit 0828007
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object ExternalServerFileSerializer : KSerializer<ServerFile> {
@OptIn(ExperimentalSerializationApi::class)
override val descriptor: SerialDescriptor = object : SerialDescriptor {
override val kind: SerialKind = PrimitiveKind.STRING
override val serialName: String = "com.lightningkite.lightningserver.files.ServerFile/external"
override val serialName: String = "com.lightningkite.lightningserver.files.ServerFile"
override val elementsCount: Int get() = 0
override fun getElementName(index: Int): String = error()
override fun getElementIndex(name: String): Int = error()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class JsonSchemaTest {
val schema = Serialization.jsonWithoutDefaults.schema(HasServerFiles.serializer())
.also(::println)
.also { println(Json(Serialization.jsonWithoutDefaults) { prettyPrint = true }.encodeToString(it)) }
assertEquals("#/definitions/com.lightningkite.lightningserver.files.ServerFile_external", schema.definitions["com.lightningkite.lightningdb.test.HasServerFiles"]!!.properties!!["file"]!!.ref)
assertEquals("#/definitions/com.lightningkite.lightningserver.files.ServerFile", schema.definitions["com.lightningkite.lightningdb.test.HasServerFiles"]!!.properties!!["file"]!!.ref)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,18 @@ class SerializationRegistry(val module: SerializersModule) {
register(LocalDateIso8601Serializer)
register(LocalTimeIso8601Serializer)
register(LocalDateTimeIso8601Serializer)
register(kotlinx.datetime.serializers.InstantIso8601Serializer)
register(kotlinx.datetime.serializers.LocalDateIso8601Serializer)
register(kotlinx.datetime.serializers.LocalTimeIso8601Serializer)
register(kotlinx.datetime.serializers.LocalDateTimeIso8601Serializer)
register(UUIDSerializer)
register(OffsetDateTimeIso8601Serializer)
register(ZonedDateTimeIso8601Serializer)
register(ServerFileSerializer)
register(GeoCoordinateArraySerializer)
register(TrimOnSerialize)
register(TrimLowercaseOnSerialize)
register(LowercaseOnSerialize)
register(TrimmedStringSerializer)
register(TrimmedCaselessStringSerializer)
register(CaselessStringSerializer)
Expand Down Expand Up @@ -179,6 +186,7 @@ class SerializationRegistry(val module: SerializersModule) {
}

fun registerVirtualDeep(type: KSerializer<*>) {
type.nullElement()?.let { return registerVirtualDeep(it) }
if(registerVirtual(type) != null) {
type.tryChildSerializers()?.forEach { registerVirtualDeep(it) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,10 @@ data class VirtualTypeReference(

@Suppress("UNCHECKED_CAST")
fun serializer(registry: SerializationRegistry, context: Map<String, KSerializer<*>>): KSerializer<Any?> {
try {
return (context[serialName] as? KSerializer<Any?>
?: registry[serialName, arguments.map { it.serializer(registry, context) }.toTypedArray()])
?.let { if (isNullable) it.nullable2 else it }
?: throw Exception("$serialName is not registered in either the registeredTypes or registeredGenericTypes")
} catch (e: Exception) {
throw Exception("Could not find serializer for '${serialName}'", e)
}
return (context[serialName] as? KSerializer<Any?>
?: registry[serialName, arguments.map { it.serializer(registry, context) }.toTypedArray()])
?.let { if (isNullable) it.nullable2 else it }
?: throw Exception("$serialName is not registered in either the registeredTypes or registeredGenericTypes. virtualTypes are ${registry.virtualTypes.keys.joinToString()}")
}
}

Expand Down

0 comments on commit 0828007

Please sign in to comment.