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

Case Classes with Option[Long] parameters don't deserialize properly #62

Closed
kscaldef opened this issue Feb 27, 2013 · 7 comments
Closed

Comments

@kscaldef
Copy link
Contributor

It seems that for small numbers, an Option[Long] in a case class actually gets deserialized with a runtime type of int. This can then manifest in a couple ways. Maps using these values as keys may not behave as expected, and conversions to Java Longs will throw a ClassCastException:

[info]   java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
[info]   at scala.runtime.BoxesRunTime.unboxToLong(Unknown Source)

This only seems to affect case classes. Option[Long] by itself seems to behave correctly.

kscaldef added a commit to kscaldef/jackson-module-scala that referenced this issue Feb 27, 2013
@christophercurrie
Copy link
Member

This uncovers one of the dirtier parts of Scala. The JVM doesn't support using an intrinsic like long as a type parameter, and Long is just long in the Scala type system. At one point I think that they mapped it to the boxed types in the type parameter but that ended up being a source of bugs, so they changed to Object.

For 2.2, I've added patches to databind to support using @JsonDeserialize as a workaround:

class OptionLongHolder
{
  @JsonDeserialize(contentAs=classOf[java.lang.Long])
  var optLong: Option[Long]
}

This works, as it forces Jackson to use Long for deserialization rather than the narrowest natural integer.

@magro
Copy link

magro commented May 28, 2013

We're currently tied to jackson 2.1.x. Is it somehow possible to get this backported?

@christophercurrie
Copy link
Member

I'll look into it. AFAIK, core Jackson doesn't plan on any further maintenance on 2.1.x, but I'll see what I can do about an interim release.

@magro
Copy link

magro commented May 29, 2013

Ok, thanx!

@PanzerKunst
Copy link

Hello,

Just wanted to thank you for the fix in 2.2. Got a bit stressed when I noticed that conversion bug, so I was relieved to see that it's solved using the @JsonDeserialize(contentAs=classOf[java.lang.Long]) annotation.

@eugeniyk
Copy link
Contributor

What is the workaround if your case class is generated for you? I don't have an access to apply attribute.

@devshorts
Copy link

@christophercurrie is it possible to create an optional module that uses what finatra has to work around this? It looks like they use the scala signature parsing https://github.com/twitter/finatra/tree/develop/jackson/src/main/scala/com/twitter/finatra/json/internal/caseclass/jackson

Having to annotate works fine, but its problematic when you are trying to distribute a model class (for example) which now pulls in all of jackson databind. Had the annotation been in jackson-annotations it would be less problematic, but leaking a dependency like that really sucks.

I'd be really open to having a separate module (if we don't want to include that in the main one) that provided for option[long] parsing via signature parsing in the interim, until the deprecation comes around for 2.10>

pan3793 pushed a commit to apache/kyuubi that referenced this issue Jun 20, 2024
# 🔍 Description
## Issue References 🔗

This pull request fixes #6476 : spark historyserver -> Show incomplete applications -> kyuubi query engine ui error(java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long).

The reason:
it's related to FasterXML/jackson-module-scala#62

## Describe Your Solution 🔧

add JsonDeserialize(contentAs = classOf[java.lang.Long]) annotation

## Types of changes 🔖

- [x] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Test Plan 🧪

#### Behavior Without This Pull Request ⚰️

#### Behavior With This Pull Request 🎉

#### Related Unit Tests

---

# Checklist 📝

- [x] This patch was not authored or co-authored using [Generative Tooling](https://www.apache.org/legal/generative-tooling.html)

**Be nice. Be informative.**

Closes #6479 from felixzh2020/issues/6476.

Closes #6476

034bfe5 [felixzh] [KYUUBI #6476] spark historyserver Show incomplete applications kyuubi query engine ui error
b7b0db2 [felixzh] [KYUUBI #6476] spark historyserver Show incomplete applications kyuubi query engine ui error
a66163a [felixzh] [KYUUBI #6476] spark historyserver Show incomplete applications kyuubi query engine ui error

Authored-by: felixzh <felixzh2020@126.com>
Signed-off-by: Cheng Pan <chengpan@apache.org>
pan3793 pushed a commit to apache/kyuubi that referenced this issue Jun 20, 2024
# 🔍 Description
## Issue References 🔗

This pull request fixes #6476 : spark historyserver -> Show incomplete applications -> kyuubi query engine ui error(java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long).

The reason:
it's related to FasterXML/jackson-module-scala#62

## Describe Your Solution 🔧

add JsonDeserialize(contentAs = classOf[java.lang.Long]) annotation

## Types of changes 🔖

- [x] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Test Plan 🧪

#### Behavior Without This Pull Request ⚰️

#### Behavior With This Pull Request 🎉

#### Related Unit Tests

---

# Checklist 📝

- [x] This patch was not authored or co-authored using [Generative Tooling](https://www.apache.org/legal/generative-tooling.html)

**Be nice. Be informative.**

Closes #6479 from felixzh2020/issues/6476.

Closes #6476

034bfe5 [felixzh] [KYUUBI #6476] spark historyserver Show incomplete applications kyuubi query engine ui error
b7b0db2 [felixzh] [KYUUBI #6476] spark historyserver Show incomplete applications kyuubi query engine ui error
a66163a [felixzh] [KYUUBI #6476] spark historyserver Show incomplete applications kyuubi query engine ui error

Authored-by: felixzh <felixzh2020@126.com>
Signed-off-by: Cheng Pan <chengpan@apache.org>
(cherry picked from commit c6f2ca2)
Signed-off-by: Cheng Pan <chengpan@apache.org>
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

6 participants