in prompt/prompt-structure/src/commonTest/kotlin/ai/koog/prompt/structure/json/generator/JsonSchemaGeneratorTest.kt [634:723]
fun testStandardSchemaClosedPolymorphic() {
val descriptions = mapOf(
"ClosedSubclass1.id" to "ID for subclass 1",
"ClosedSubclass1.property1" to "Property 1 for subclass 1",
"ClosedSubclass2.id" to "ID for subclass 2",
"ClosedSubclass2.property2" to "Property 2 for subclass 2",
)
val result = standardGenerator.generate(
json,
"TestClosedPolymorphism",
serializer<TestClosedPolymorphism>(),
descriptions
)
val schema = json.encodeToString(result.schema)
val expectedSchema = """
{
"${"$"}id": "TestClosedPolymorphism",
"${"$"}defs": {
"ClosedSubclass1": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "ID for subclass 1"
},
"property1": {
"type": "string",
"description": "Property 1 for subclass 1"
},
"kind": {
"const": "ClosedSubclass1"
}
},
"required": [
"id",
"property1",
"kind"
],
"additionalProperties": false
},
"ClosedSubclass2": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "ID for subclass 2"
},
"property2": {
"type": "integer",
"description": "Property 2 for subclass 2"
},
"recursiveTypeProperty": {
"oneOf": [
{
"${"$"}ref": "#/${"$"}defs/ClosedSubclass1"
},
{
"${"$"}ref": "#/${"$"}defs/ClosedSubclass2"
}
]
},
"kind": {
"const": "ClosedSubclass2"
}
},
"required": [
"id",
"property2",
"recursiveTypeProperty",
"kind"
],
"additionalProperties": false
}
},
"oneOf": [
{
"${"$"}ref": "#/${"$"}defs/ClosedSubclass1"
},
{
"${"$"}ref": "#/${"$"}defs/ClosedSubclass2"
}
]
}
""".trimIndent()
assertEquals(expectedSchema, schema)
}