-
Notifications
You must be signed in to change notification settings - Fork 648
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
Conversation
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* 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 |
There was a problem hiding this comment.
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
Fixes #2948