Skip to content

Commit 6488fef

Browse files
authored
Clarify that it is preferable to have unique class' serial name (#3098)
Fixes #3003
1 parent 2144281 commit 6488fef

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

core/commonMain/src/kotlinx/serialization/Annotations.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ public annotation class Serializer(
148148
* println(Json.encodeToString(CustomName(42)))
149149
* ```
150150
*
151-
* If a name of class or property is overridden with this annotation, original source code name is not available for the library.
151+
* When picking a serial name for a class, avoid assigning the same name to different classes.
152+
* Check out Equality rules for [SerialDescriptor] to make sure that class descriptor will stay unique.
153+
*
154+
* If a name of a class or property is overridden with this annotation, the original source code name is not available for the library.
152155
* Tools like `JsonNamingStrategy` and `ProtoBufSchemaGenerator` would see and transform [value] from [SerialName] annotation.
153156
*/
154157
@MustBeDocumented

core/commonMain/src/kotlinx/serialization/descriptors/SerialDescriptors.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public fun PrimitiveSerialDescriptor(serialName: String, kind: PrimitiveKind): S
9393
/**
9494
* Factory to create a new descriptor that is identical to [original] except that the name is equal to [serialName].
9595
* Usually used when you want to serialize a type as another type, delegating implementation of `serialize` and `deserialize`.
96+
* Do not use [serialName] that is equal to the name of [original] or other serializable classes.
9697
*
9798
* Example:
9899
* ```

docs/polymorphism.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ This way we can have a stable _serial name_ that is not affected by the class's
234234
> In addition to that, JSON can be configured to use a different key name for the class discriminator.
235235
> You can find an example in the [Class discriminator for polymorphism](json.md#class-discriminator-for-polymorphism) section.
236236
237+
> [!IMPORTANT]
238+
> When picking a serial name for a class, avoid assigning the same name to different classes.
239+
Check out equality rules in documentation for [SerialDescriptor] to make sure that the class descriptor will stay unique.
240+
237241
### Concrete properties in a base class
238242

239243
A base class in a sealed hierarchy can have properties with backing fields.
@@ -1017,6 +1021,10 @@ The next chapter covers [JSON features](json.md).
10171021
[DeserializationStrategy]: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-deserialization-strategy/index.html
10181022
[SerializationStrategy]: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-serialization-strategy/index.html
10191023

1024+
<!--- INDEX kotlinx-serialization-core/kotlinx.serialization.descriptors -->
1025+
1026+
[SerialDescriptor]: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization.descriptors/-serial-descriptor/index.html
1027+
10201028
<!--- INDEX kotlinx-serialization-core/kotlinx.serialization.modules -->
10211029

10221030
[SerializersModule]: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization.modules/-serializers-module/index.html
@@ -1037,4 +1045,3 @@ The next chapter covers [JSON features](json.md).
10371045
[Json]: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json/index.html
10381046

10391047
<!--- END -->
1040-

0 commit comments

Comments
 (0)