Skip to content

SerializersModule overwriteWith throws when used twice #2820

@gpunto

Description

@gpunto

Describe the bug

When overwriteWith is used twice, the second call throws an exception complaining that there are duplicated serializers. However, the duplicates should've been removed by the first overwriteWith call.

For the snippet below, the error message is:

Multiple polymorphic serializers for base class 'interface example.Parent (Kotlin reflection is not available)' have the same serial name 'CHILD': 'class example.ChildA (Kotlin reflection is not available)' and 'class example.ChildB (Kotlin reflection is not available)=example.ChildB$$serializer@4d95d2a2'

To Reproduce

interface Parent

@Serializable
@SerialName("CHILD")
data class ChildA(val a: String) : Parent

@Serializable
@SerialName("CHILD")
data class ChildB(val b: String) : Parent

fun main() {
    val moduleA = SerializersModule {
        polymorphic(Parent::class) {
            subclass(ChildA::class)
        }
    }
    val moduleB = SerializersModule {
        polymorphic(Parent::class) {
            subclass(ChildB::class)
        }
    }
    val firstMerge = moduleA overwriteWith moduleB
    firstMerge overwriteWith SerializersModule { } // <- throws
}

Also note that while this throws:

firstMerge overwriteWith anotherModule

this doesn't:

anotherModule overwriteWith firstMerge

However, we can't replace the first with the second, because they are not equivalent.

Expected behavior

I expect overwriteWith not to throw because of duplicated serializers. We should be able to do:

module1 overwriteWith module2 overwriteWith module3 …

And get a combined "union" module.

Environment

  • Kotlin version: 2.0.20
  • Library version: 1.7.2
  • Kotlin platforms: JVM
  • Gradle version: 8.10.1

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions