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

JsonMappingException ("no propery name annotation") when unmarshalling inner case class #29

Closed
lorrin opened this issue Jun 15, 2012 · 4 comments

Comments

@lorrin
Copy link

lorrin commented Jun 15, 2012

In the following code, parsing the non-inner case classes with Foo works, but parsing the inner case classes with Bar fails.

val om = new com.fasterxml.jackson.databind.ObjectMapper
om.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule)

case class FooTuple(s:String,i:Int)
class FooParse {
  def parse(json:String) = om.readValue(json,classOf[FooTuple])
}

new FooParse().parse("""{"s":"Hello","i":42}""")

class BarParse {
  case class BarTuple(s:String,i:Int)

  def parse(json:String) = om.readValue(json, classOf[BarTuple])
}

new BarParse().parse("""{"s":"Hello","i":42}""")

Foo parse goes like this:

scala> new FooParse().parse("""{"s":"Hello","i":42}""")
res16: FooTuple = FooTuple(Hello,42)

Bar parse like this:

scala> new BarParse().parse("""{"s":"Hello","i":42}""")
com.fasterxml.jackson.databind.JsonMappingException: Argument #0 of constructor [constructor for BarParse$BarTuple, annotations: [null]] has no property name annotation; must have name when multiple-paramater constructor annotated as Creator
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:252)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:227)
    at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:126)
    at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:331)
    at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:2679)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2573)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1810)
    at BarParse.parse(<console>:20)
    at .<init>(<console>:19)
    at .<clinit>(<console>)
    at .<init>(<console>:11)
    at .<clinit>(<console>)
    at $print(<console>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)
    at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920)
    at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)
    at scala.tools.nsc.io.package$$anon$2.run(package.scala:25)
    at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.IllegalArgumentException: Argument #0 of constructor [constructor for BarParse$BarTuple, annotations: [null]] has no property name annotation; must have name when multiple-paramater constructor annotated as Creator
    at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory._addDeserializerConstructors(BasicDeserializerFactory.java:450)
    at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory._constructDefaultValueInstantiator(BasicDeserializerFactory.java:352)
    at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory.findValueInstantiator(BasicDeserializerFactory.java:293)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:260)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:168)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:363)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:247)
    ... 21 more
@christophercurrie
Copy link
Member

Thanks for the bug report. I seem to recall that the reflection mechanism I'm currently using might have issues with inner classes. The reason (IIRC) is that the system uses the ScalaSig system to get run-time type information, which is only available on the top level. Another way of putting it, BarTuple is path-dependent, each instance of BarTuple is tied to a specific Bar. Since, in context, Jackson doesn't know anything about the parent Bar, it may have a hard time doing the right thing in this case, short of some stack trace inspection to see if the parse method is being called from within the parent class of BarTuple.

But I'll spend some time looking into what might be done. I also hope that the forthcoming Scala 2.10 will provide better reflection tools for these use cases.

christophercurrie added a commit that referenced this issue May 2, 2013
@christophercurrie
Copy link
Member

Have done more reading on this issue, and on the support that is within core Jackson for this use case. The relevant bug report for when support for this was added to Jackson demonstrates that this is possible in the context of deserializing an instance of the containing class.

So in the case above, BarTuple cannot be deserialized alone, but if BarParse where a bean type, that contained a BarTuple as a value, then that would be deserialzed properly. The test added in fe15b83 demonstrates that this works correctly.

@ScottPierce
Copy link

@christophercurrie

Still seeing this issue on 2.3.2 on inner case classes generated by Slick. The following case class is inside a generated trait named Tables

case class User(id: Int, email: String, password: String, firstName: String, lastName: String)

Error:
Caused by: java.lang.IllegalArgumentException: Argument #0 of constructor [constructor for db.Tables$User, annotations: [null]] has no property name annotation; must have name when multiple-paramater constructor annotated as Creator at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory.findValueInstantiator(BasicDeserializerFactory.java:287) at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:266) at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:168) at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:401) at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:350) at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:263) ... 30 more

@christophercurrie
Copy link
Member

Can you open an new issue, and provide a more complete code sample, preferably without a dependency on Slick?

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