Skip to content
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

"Conflicting getter definitions" with @BeanProperty when using ScalaClassIntrospectorModule #73

Closed
cimbul opened this issue Apr 25, 2013 · 6 comments
Milestone

Comments

@cimbul
Copy link

cimbul commented Apr 25, 2013

We have a mixed Java/Scala project where many of our domain objects are written in Scala. Because we use Hibernate for persistence, we have to annotate our properties with @BeanProperty. Unfortunately, when we use ScalaClassIntrospectorModule, Jackson picks up both the Scala-style getter and the Java Beans-style one.

Say we have a domain class like this:

package com.example
import scala.beans.BeanProperty

class Foo {
  @BeanProperty var id: Int = _
  @BeanProperty var bar: String = _
}

If we try to serialize a list of Foos with the ScalaClassIntrospectorModule added to our ObjectMapper, we get this:

com.fasterxml.jackson.databind.JsonMappingException: Conflicting getter definitions for property "id": com.example.Foo#getId(0 params) vs com.example.Foo#id(0 params) (through reference chain: java.util.ArrayList[0])
    at com.fasterxml.jackson.databind.SerializerProvider._createAndCacheUntypedSerializer(SerializerProvider.java:838)
    at com.fasterxml.jackson.databind.SerializerProvider.findValueSerializer(SerializerProvider.java:387)
    at com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap.findAndAddSerializer(PropertySerializerMap.java:38)
    at com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase._findAndAddDynamic(AsArraySerializerBase.java:270)
    at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:96)
    at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:21)
    at com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:186)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:118)
    at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:1819)

Fortunately, because of your mixin design, we can work around this by just excluding the ScalaClassIntrospectorModule and rely on @BeanProperty as we are forced to elsewhere.

But is there an easy way to make this configurable? Could the Scala introspector detect the presence of bean-style accessors and defer to them? (Having the Scala accessors take precedence over the bean-style ones would be fine, too---I just imagine that the other way would be easier.) Is there something I can configure in Jackson that I am missing?

Thanks!

@christophercurrie
Copy link
Member

No, not at time; looks like this is a bit of an oversight in my testing of the new introspection, so thanks for highlighting this use case, and I'm glad you have a workaround.

In the short term, deferring to the @BeanProperty annotations sounds like the right call, and if there's interest I can look into adding some configuration that will allow you to configure which methods get preferred.

@jendap
Copy link

jendap commented Apr 29, 2013

This is quite a problem. A lot of scala code has java style getters and setters for java interop. Upgrade to jackson 2.2 breaks working code. And the workaround has it's limitations.

In our case we use scala code - no java getters and setters. So we can't remove ScalaClassIntrospectorModule. We also use documentation in Swagger. It has (in order to work with other frameworks) java style getters as well scala fields. So we would need to remove ScalaClassIntrospectorModule. That's not easy!

For example apiVersion() (from var in constructor) and getApiVersion() manually written in:
https://github.com/wordnik/swagger-core/blob/master/modules/swagger-core/src/main/scala/com/wordnik/swagger/core/Documentation.scala#L50

@christophercurrie
Copy link
Member

I completely understand, this is my highest priority to fix for 2.2.1.

christophercurrie added a commit that referenced this issue Apr 30, 2013
christophercurrie added a commit that referenced this issue Apr 30, 2013
@christophercurrie
Copy link
Member

I've just pushed a fix to master that should address this issue, and it will go out with Jackson 2.2.1. If there's an urgent need for a patched "2.2.0-1" for this, open another issue and I'll see what I can do.

@jendap
Copy link

jendap commented Apr 30, 2013

Thanks!

@cimbul
Copy link
Author

cimbul commented May 1, 2013

Great! Thanks for the quick turnaround. No need for an urgent patch from my perspective.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants