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.

Inconsistent handling of Collections$UnmodifiableList VS Collections$UnmodifiableRandomAccessList

See original GitHub issue

I’m sorry to bring that one up again, but I’m under the impression that the issue about unmodifiable collections (https://github.com/FasterXML/jackson-databind/issues/1880) is still not solved completely.

In fact, the way the CLASS_UNMODIFIABLE_LIST is retrieved here yields Collections$UnmodifiableRandomAccessList, and therefore only this type is currently supported by Jackson 2.9.8.

However, using Collections.unmodifiableList() on a List implementation that doesn’t implement RandomAccess will yield a Collections$UnmodifiableList instead, which is not deserialized properly and fails with:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.util.Collections$UnmodifiableList` (no Creators, like default constructor, exist): no default no-arguments constructor found

This can be reproduced by adding the following test case in TestDefaultForUtilCollections1868:

public void testUnmodifiableNonRandomAccessList() throws Exception {
   _verifyCollection(Collections.unmodifiableList(new LinkedList<>(Arrays.asList("first", "second"))));
}

Or more generally for outside the project:

public void testUnmodifiableNonRandomAccessList() throws Exception {
    Collection<?> exp = Collections.unmodifiableList(new LinkedList<>(Arrays.asList("first", "second")));
    ObjectMapper mapper = new ObjectMapper();
    mapper.enableDefaultTyping(DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
    String json = mapper.writeValueAsString(exp);
    Collection<?> act = mapper.readValue(json, Collection.class);

    assertEquals(exp, act);
    assertEquals(exp.getClass(), act.getClass());
}

Currently java.util.Collections.unmodifiableList() can only return these 2 types of unmodifiable lists, so I believe it is safe for now to just hardcode yet another special case for this class.

This can currently be solved on user side by adding a mixin, but since Collections$UnmodifiableRandomAccessList is supported, I would find it natural to also support the non-random access variant.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Mar 3, 2019

@joffrey-bion Thank you for reporting this issue – I fixed this in 2.9 for 2.9.9 (and thereby 2.10.0 / 3.0.0) when released.

0reactions
joffrey-bioncommented, Mar 3, 2019

@cowtowncoder Thanks a lot for looking into this and quickly fixing the issue!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Inconsistent handling of Collections$UnmodifiableList VS ...
This can currently be solved on user side by adding a mixin, but since Collections$UnmodifiableRandomAccessList is supported, I would find it ...
Read more >
java.util.Collections$UnmodifiableRandomAccessList to ...
The solution was to use the original list by casting to the correct class/interface: List<String>. instead of: Collections. worked.
Read more >
JDK-2146356 LTP: Java 6 breaks XML encoding ... - Bug ID
Classes in the Java DTrace API that encode/decode successfully in Java 5 no longer encode/decode successfully in Java 6 (using XMLEncoder and XMLDecoder)....
Read more >
java.util Class Collections.UnmodifiableRandomAccessList
Returns a string representation of the object. Allows instances to be deserialized in pre-1.4 JREs (which do not have UnmodifiableRandomAccessList).
Read more >
5 Creating Unmodifiable Lists, Sets, and Maps
asList("a", "b", "c"); stringList = Collections.unmodifiableList(stringList);. In JDK 9: Copy List<String> stringList = List.of( ...
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