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
Serialization of Set as Collection does not work #1421
Comments
Serializer is determined by the static type without looking at the actual instance. For now, we do not have special collection serializer, so we use ArrayListSerializer instead (
I wonder why you need exactly |
I'm confused, I already am using
Which would be fine with me. But actually in this case, the class only ever gets serialized, but never deserialized. |
Oh sorry, I think I've got distracted and mistyped. I meant
Use-case ' a Set and sometimes a List, and want to avoid an additional toList() conversion' looks reasonable |
Also, to me that's exactly an argument for the |
Any idea how to start addressing this @sandwwraith? Would the right approach be to try generalizing |
I'd say we need to add new |
Thanks for the hints! I tried to come up with something here, but unfortunately I'm running into a compile error:
Anymore hints? |
@sschuberth Try internal abstract class CollectionSerializer<E, C: Collection<E>, B>(element: KSerializer<E>) : ListLikeSerializer<E, C, B>(element) {
override fun C.collectionSize(): Int = size
override fun C.collectionIterator(): Iterator<E> = iterator()
}
internal class ArrayListSerializer<E>(element: KSerializer<E>) : CollectionSerializer<E, List<E>, ArrayList<E>>(element) |
Thanks @sandwwraith, that worked. Do you have one more hint for me in which test class(es) to add a test for this? |
I think there should be various tests for serializing sets as lists and vice versa. At least, add your use-case (with |
So far there was an implicit assumption hard-coded that collections are always lists. However, sets are also collections, and can be serialized to JSON arrays just like lists. This change allows to serialize generic collections independently of the concrete implementation. Fixes Kotlin#1421.
I filed a draft PR to move forward with this, in the hope that tests would be executed as part of PR checks, but unfortunately no tests seem to be run as part of the PR 😢 |
So far there was an implicit assumption hard-coded that collections are always lists. However, sets are also collections, and can be serialized to JSON arrays just like lists. This change allows to serialize generic collections independently of the concrete implementation. Fixes Kotlin#1421.
So far there was an implicit assumption hard-coded that collections are always lists. However, sets are also collections, and can be serialized to JSON arrays just like lists. This change allows to serialize generic collections independently of the concrete implementation. Fixes Kotlin#1421.
So far there was an implicit assumption hard-coded that collections are always lists. However, sets are also collections, and can be serialized to JSON arrays just like lists. This change allows to serialize generic collections independently of the concrete implementation. Fixes Kotlin#1421.
So far there was an implicit assumption hard-coded that collections are always lists. However, sets are also collections, and can be serialized to JSON arrays just like lists. This change allows serializing generic collections independently of the concrete implementation. Fixes #1421. * CollectionSerializers: Remove redundant visibility modifiers * SealedGenericClassesTest: Remove unused variables to avoid warnings * Rename `ListLikeSerializer` to `CollectionLikeSerializer` Now with `CollectionSerializer` inheriting from `ListLikeSerializer`, it makes sense to rename `ListLikeSerializer` to the more generic `CollectionLikeSerializer`.
So far there was an implicit assumption hard-coded that collections are always lists. However, sets are also collections, and can be serialized to JSON arrays just like lists. This change allows serializing generic collections independently of the concrete implementation. Fixes #1421. * CollectionSerializers: Remove redundant visibility modifiers * SealedGenericClassesTest: Remove unused variables to avoid warnings * Rename `ListLikeSerializer` to `CollectionLikeSerializer` Now with `CollectionSerializer` inheriting from `ListLikeSerializer`, it makes sense to rename `ListLikeSerializer` to the more generic `CollectionLikeSerializer`. (cherry picked from commit d254e6d)
Describe the bug
I'm using Retrofit with kotlinx.serialization and the following code:
When calling
service.getPackageVulnerabilities(PackagesWrapper(packageMap.keys))
wherekeys
is aSet
, I getTo Reproduce
Checkout https://github.com/oss-review-toolkit/ort/tree/vc2kxs-bug and run
./gradlew :advisor:test --tests org.ossreviewtoolkit.advisor.advisors.VulnerableCodeTest
.Expected behavior
As a
Set
is aCollection
that can be serialized to a JSON array, I'd expected this to just work. However,ArrayListSerializer
seems to hard-code inListLikeSerializer<E, List<E>, ArrayList<E>>(element)
that the "list like" collection needs to be aList
.Environment
The text was updated successfully, but these errors were encountered: