Skip to content

Mention type unsoundness of serializer() in the documentation #2998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 25, 2025

Conversation

dkhalanskyjb
Copy link
Contributor

Fixes #2948

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates the documentation for various serializer() overloads to mention that type unsoundness may result in incorrect results or [ClassCastException] if improperly used.

  • Added a clear Pitfall section warning against potential type mismatches.
  • Revised examples and wording in multiple serializer() overloads to improve clarity.

@@ -58,14 +58,14 @@ public inline fun <reified T> SerializersModule.serializer(): KSerializer<T> {
* Creates a serializer for the given [type].
* [type] argument is usually obtained with [typeOf] method.
*
* This overload works with full type information, including type arguments and nullability,
* and is a recommended way to retrieve a serializer.
* For example, `serializer<typeOf<List<String?>>>()` returns [KSerializer] that is able
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what this dependent-type-style syntax means and was assuming that this was a copy-paste artifact of some sort.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I mistyped the function call: it should be serializer(typeOf<List<String?>>()). Can you please bring this paragraph back in the correct form?

@@ -60,12 +60,17 @@ public inline fun <reified T> SerializersModule.serializer(): KSerializer<T> {
*
* This overload works with full type information, including type arguments and nullability,
* and is a recommended way to retrieve a serializer.
* For example, `serializer<typeOf<List<String?>>>()` returns [KSerializer] that is able
* to serialize and deserialize list of nullable strings — i.e. `ListSerializer(String.serializer().nullable)`.
* For example, `serializerOrNull(typeOf<List<String?>>)` returns [KSerializer] that is able
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* For example, `serializerOrNull(typeOf<List<String?>>)` returns [KSerializer] that is able
* For example, `serializer(typeOf<List<String?>>())` returns [KSerializer] that is able

This is documentation for regular serializer() function, and typeOf needs invocation

* To create array serializer, use overload with [KType] or [ArraySerializer] directly.
*
* Caching on JVM platform is disabled for this function, so it may work slower than an overload with [KType].
*
* **Pitfall**: the returned serializer may return incorrect results or throw a [ClassCastException] if it receives
* a value that's not a valid instance of the [KType], even though the type allows passing such a value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* a value that's not a valid instance of the [KType], even though the type allows passing such a value.
* a value that's not a valid instance of the [KClass], even though the type allows passing such a value.

@@ -103,11 +113,14 @@ public fun serializer(
* This overload works with full type information, including type arguments and nullability,
* and is a recommended way to retrieve a serializer.
* For example, `serializerOrNull<typeOf<List<String?>>>()` returns [KSerializer] that is able
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line also needs change to serializerOrNull(typeOf<List<String?>>()) while we're here

@@ -120,12 +133,18 @@ public fun serializerOrNull(type: KType): KSerializer<Any?>? = EmptySerializersM
*
* This overload works with full type information, including type arguments and nullability,
* and is a recommended way to retrieve a serializer.
* For example, `serializer<typeOf<List<String?>>>()` returns [KSerializer] that is able
* to serialize and deserialize list of nullable strings — i.e. `ListSerializer(String.serializer().nullable)`.
* For example, `serializer(typeOf<List<String?>>)` returns [KSerializer] that is able
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* For example, `serializer(typeOf<List<String?>>)` returns [KSerializer] that is able
* For example, `serializer(typeOf<List<String?>>())` returns [KSerializer] that is able

@@ -169,11 +194,14 @@ public fun SerializersModule.serializer(
* This overload works with full type information, including type arguments and nullability,
* and is a recommended way to retrieve a serializer.
* For example, `serializerOrNull<typeOf<List<String?>>>()` returns [KSerializer] that is able
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here, if you do not mind

@dkhalanskyjb dkhalanskyjb requested a review from sandwwraith June 25, 2025 13:43
@sandwwraith sandwwraith merged commit 596bac8 into dev Jun 25, 2025
3 of 4 checks passed
@dkhalanskyjb dkhalanskyjb deleted the dkhalanskyjb/fix-2948 branch June 26, 2025 07:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants