Skip to content

Commit 270b5e5

Browse files
committed
Improve message about missing polymorphic serializer
Fixes #2174
1 parent 2cb7f7d commit 270b5e5

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

core/commonMain/src/kotlinx/serialization/internal/AbstractPolymorphicSerializer.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ internal fun throwSubtypeNotRegistered(subClassName: String?, baseClass: KClass<
104104
"Class discriminator was missing and no default polymorphic serializers were registered $scope"
105105
else
106106
"Class '$subClassName' is not registered for polymorphic serialization $scope.\n" +
107-
"Mark the base class as 'sealed' or register the serializer explicitly."
107+
"To be registered automatically, class '$subClassName' has to be '@Serializable', and the base class '${baseClass.simpleName}' has to be sealed and '@Serializable'.\n" +
108+
"Alternatively, register the serializer for '$subClassName' explicitly in a corresponding SerializersModule."
108109
)
109110
}
110111

docs/polymorphism.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ This is close to the best design for a serializable hierarchy of classes, but ru
124124

125125
```text
126126
Exception in thread "main" kotlinx.serialization.SerializationException: Class 'OwnedProject' is not registered for polymorphic serialization in the scope of 'Project'.
127-
Mark the base class as 'sealed' or register the serializer explicitly.
127+
To be registered automatically, class 'OwnedProject' has to be '@Serializable', and the base class 'Project' has to be sealed and '@Serializable'.
128+
Alternatively, register the serializer for 'OwnedProject' explicitly in a corresponding SerializersModule.
128129
```
129130

130131
<!--- TEST LINES_START -->

guide/test/PolymorphismTest.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class PolymorphismTest {
2424
fun testExamplePoly03() {
2525
captureOutput("ExamplePoly03") { example.examplePoly03.main() }.verifyOutputLinesStart(
2626
"Exception in thread \"main\" kotlinx.serialization.SerializationException: Class 'OwnedProject' is not registered for polymorphic serialization in the scope of 'Project'.",
27-
"Mark the base class as 'sealed' or register the serializer explicitly."
27+
"To be registered automatically, class 'OwnedProject' has to be '@Serializable', and the base class 'Project' has to be sealed and '@Serializable'.",
28+
"Alternatively, register the serializer for 'OwnedProject' explicitly in a corresponding SerializersModule."
2829
)
2930
}
3031

0 commit comments

Comments
 (0)