Skip to content

StackOverflowError when trying to generate schema for closed recursive polymorphic class from SerialDescriptor #192

@EugeneTheDev

Description

@EugeneTheDev

Expected:
Recrusive references in closed polymorphic classes are correctly resolved without causing endless loops. Generator should detect that TestClosedPolymorphism class is being processed already and just put a $ref for recursiveTypeProperty instead of starting to process TestClosedPolymorphism class again.

Actual:
Infinite loop due to generator trying to process TestClosedPolymorphism for recursiveTypeProperty from scratch every time.

Reproduction using 0.3.1:

import kotlinx.schema.generator.json.serialization.SerializationClassJsonSchemaGenerator
import kotlinx.schema.json.encodeToString
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlin.test.Test

class SchemaGeneratorTest {
    @Serializable
    @SerialName("TestClosedPolymorphism")
    sealed class TestClosedPolymorphism {
        abstract val id: String

        @Suppress("unused")
        @Serializable
        @SerialName("ClosedSubclass1")
        data class SubClass1(
            override val id: String,
            val property1: String
        ) : TestClosedPolymorphism()

        @Suppress("unused")
        @Serializable
        @SerialName("ClosedSubclass2")
        data class SubClass2(
            override val id: String,
            val property2: Int,
            // This property produces StackOverflowError when generating schema
            val recursiveTypeProperty: TestClosedPolymorphism,
        ) : TestClosedPolymorphism()
    }
    @Test
    fun test() {
        val testSchema = SerializationClassJsonSchemaGenerator.Default.generateSchema(TestClosedPolymorphism.serializer().descriptor)
        println(testSchema.encodeToString(json = Json { prettyPrint = true } ))
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    internal-usersIssues and PRs raised by internal users

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions