question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serialization of Set as Collection does not work

See original GitHub issue

Describe the bug

I’m using Retrofit with kotlinx.serialization and the following code:

@Serializable
data class PackagesWrapper(
    val purls: Collection<String>
)

@POST("api/packages/bulk_search")
suspend fun getPackageVulnerabilities(@Body packageUrls: PackagesWrapper): List<PackageVulnerabilities>

When calling service.getPackageVulnerabilities(PackagesWrapper(packageMap.keys)) where keys is a Set, I get

java.lang.ClassCastException: class java.util.LinkedHashMap$LinkedKeySet cannot be cast to class java.util.List (java.util.LinkedHashMap$LinkedKeySet and java.util.List are in module java.base of loader 'bootstrap')

To 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 a Collection that can be serialized to a JSON array, I’d expected this to just work. However, ArrayListSerializer seems to hard-code in ListLikeSerializer<E, List<E>, ArrayList<E>>(element) that the “list like” collection needs to be a List.

Environment

  • Kotlin version: 1.4.32
  • Library version: 1.1.0
  • Kotlin platforms: JVM
  • Gradle version: 7.0
  • IDE version (if bug is related to the IDE): IntellijIDEA 2021.1
  • Other relevant context: Windows 10 64-bit

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
sandwwraithcommented, Apr 19, 2021

Oh sorry, I think I’ve got distracted and mistyped. I meant

I wonder why you need exactly Collection static type and why you can’t replace it with val purls: Set<String>?

Use-case ’ a Set and sometimes a List, and want to avoid an additional toList() conversion’ looks reasonable

0reactions
sschuberthcommented, Jan 9, 2022

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 😢

Read more comments on GitHub >

github_iconTop Results From Across the Web

Collection serialization issues - java - Stack Overflow
Inside of user class i have multiple variables (ints, Strings, etc.) and few Collections. Problem is in overwriting the file. 2 out of...
Read more >
Tuning - Spark 3.3.1 Documentation - Apache Spark
This setting configures the serializer used for not only shuffling data between worker nodes but also when serializing RDDs to disk. The only...
Read more >
How to serialize properties of derived classes with System ...
In this article, you will learn how to serialize properties of derived classes with the System.Text.Json namespace.
Read more >
JSON Serialization Usage
This causes problems when you want a connected object model in your server, but you can't send that object model to the client...
Read more >
Serialization Guide - Json.NET
NET collections are not serialized. In situations where a type implements IEnumerable but a JSON array is not wanted, then the JsonObjectAttribute can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found