Skip to content

Commit

Permalink
JsonSchemaGenerator update
Browse files Browse the repository at this point in the history
  • Loading branch information
morisil committed Oct 14, 2024
1 parent 5736f82 commit 08f01e6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
11 changes: 4 additions & 7 deletions src/commonMain/kotlin/schema/JsonSchemaGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@ package com.xemantic.anthropic.schema
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
import kotlin.collections.set
import kotlin.reflect.KClass

inline fun <reified T> jsonSchemaOf(): JsonSchema = generateSchema(
serializer<T>().descriptor
)

@OptIn(InternalSerializationApi::class)
fun KClass<*>.toJsonSchema(): JsonSchema = generateSchema(
serializer().descriptor
)

@OptIn(ExperimentalSerializationApi::class)
fun generateSchema(descriptor: SerialDescriptor): JsonSchema {
val properties = mutableMapOf<String, JsonSchemaProperty>()
Expand Down Expand Up @@ -58,9 +52,12 @@ private fun generateSchemaProperty(
)
StructureKind.MAP -> JsonSchemaProperty("object")
StructureKind.CLASS -> {
// dots are not allowed in JSON Schema name, if the @SerialName was not
// specified, then fully qualified class name will be used, and we need
// to translate it
val refName = descriptor.serialName.replace('.', '_').trimEnd('?')
definitions[refName] = generateSchema(descriptor)
JsonSchemaProperty("\$ref", ref = "#/definitions/$refName")
JsonSchemaProperty(ref = "#/definitions/$refName")
}
else -> JsonSchemaProperty("object") // Default case
}
Expand Down
8 changes: 4 additions & 4 deletions src/commonTest/kotlin/schema/JsonSchemaGeneratorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package com.xemantic.anthropic.schema
import com.xemantic.anthropic.anthropicJson
import io.kotest.assertions.json.shouldEqualJson
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlin.test.Test

@Serializable
@SerialName("address")
data class Address(
val street: String? = null,
val city: String? = null,
Expand All @@ -35,7 +37,6 @@ class JsonSchemaGeneratorTest {

@Test
fun generateJsonSchemaForAddress() {

// when
val schema = jsonSchemaOf<Address>()
val schemaJson = json.encodeToString(schema)
Expand Down Expand Up @@ -77,7 +78,7 @@ class JsonSchemaGeneratorTest {
{
"type": "object",
"definitions": {
"com_xemantic_anthropic_schema_Address": {
"address": {
"type": "object",
"properties": {
"street": {
Expand Down Expand Up @@ -116,8 +117,7 @@ class JsonSchemaGeneratorTest {
}
},
"address": {
"type": "${'$'}ref",
"ref": "#/definitions/com_xemantic_anthropic_schema_Address"
"${'$'}ref": "#/definitions/address"
}
},
"required": [
Expand Down

0 comments on commit 08f01e6

Please sign in to comment.