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.

Failed to specialize `Map` type during serialization where type key type incompatibility overidden via "raw" types

See original GitHub issue

I’m using a class with a Map attribute, whose value is a Collection. I’m using OrientDB, so when I obtain this object from database, it’s returning a OTrackedMap object.

When I’m trying to serialize it, it works perfect until Jackson v2.9.2. If I upgrade it to v2.9.3 or v2.9.4, it crashes with the following exception:

INFO: Jackson version: 2.9.4

com.fasterxml.jackson.databind.JsonMappingException: Failed to specialize base type java.util.Map<java.lang.String,java.util.Collection<java.lang.String>> as com.orientechnologies.orient.core.db.record.OTrackedMap, problem: Type parameter #1/2 differs; can not specialize java.lang.String with java.lang.Object (through reference chain: com.example.jackson.AccessModel["repositoryPrivileges"])

	at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:391)
	at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:351)
	at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:316)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:727)
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
	at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3893)
	at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3207)
	at com.example.jackson.JacksonTest.testSerialization(JacksonTest.java:37)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	[...]

You can take a look at this gist to see an example.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
antonio-tomaccommented, Mar 12, 2018

I didn’t want to create new issue, but I’m also having same exception when serializing or deserializing classes with nested generic types. Here is a simple project with reduced example model and test to verify presence of problem. Difference is that it affects versions 2.8.11 up to current latest 2.9.4, but works OK for versions <=2.8.10

2reactions
cowtowncodercommented, Apr 19, 2018

Quick note: realized that use of “static typing” can be used to work around the problem, because that will force use of declared type over attempting to combine declared (static) type with runtime instance type. Although you may or may not want to force it globally (MapperFeature.USE_STATIC_TYPING), it is possible to force it on specific property:

// works on getter or field:
@JsonSerialize(typing = JsonSerialize.Typing.STATIC)
public Map<String, Collection<String>> getRepositoryPrivileges() { ... }

and this will prevent the problem here. So it may be worth considering this as short-term fix; if this is actual type for real use case it should be safe for serialization.

I will still pursue potential change I outlined above, but thought work-around would be useful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

json - Jackson serialization failing when upgrading from 2.10 ...
I'm trying to use this as a general purpose serializer, so I don't want to have to manually add custom serializers for every...
Read more >
ObjectMapper (jackson-databind 2.9.0 API) - FasterXML
Mapping that defines how to apply mix-in annotations: key is the type to received additional annotations, and value is the type that has...
Read more >
Hibernate ORM 6.1.6.Final User Guide - Red Hat on GitHub
This might be useful for mapping JDBC User Data Types (UDTs) or other specialized database-specific types (PostgreSQL's UUID type, e.g.). In addition to...
Read more >
6 Understanding Mappings
6.1.4 Transformation Mapping ... In some special circumstances, existing mapping types and their default Java to data source type handling may be insufficient....
Read more >
Safari Technology Preview Release Notes - Apple Developer
Preview the new type of phishing-resistant credential that makes signing in to websites safer and easier. Available through Safari's WebAuthn platform ...
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