playframework / playframework/playframework

Wrong ClassTag in DefaultAsyncCacheApi

Open
#8,168 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
12.6k
Forks
4k
Avg merge
2d 3h
Merged PRs (30d)
29

Description

Play Version 2.6.x
API Java to Scala
JDK Oracle java version "1.8.0_151"

DefaultAsyncCacheApi internally invokes Scala implementation of the API, which requires
passing there the ClassTag of type T
. This works fine with DefaultSyncCacheApi as the
type T usually is not nested in some other type such as X<T>. However, in DefaultAsyncCacheAPI,
the type T is nested inside the CompletionStage<T>. That causes the type T being erased at runtime, which results in Scala.<T>classTag() returning Any.

When the Scala implementation assumes a valid ClassTag and uses it, e.g., for serialization or deserialization of the value, it fails as the ClassTag is not correct.

Note: Scala implementation works because there is a correct ClassTag.

Example

play-redis implements the CacheApi against the Redis server and uses the ClassTag for deserialization of the value.

play.cache.AsyncCacheApi cache; // impl is DefaultAsyncCacheApi
cache.getOrElseUpdate(key, () -> CompletableFuture.completedFuture("Stored value"), 5)

The type T is a String here, but DefaultAsyncCacheApi passes inside the Scala implementation Any. See here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at framework/src/main/java/play/cache/DefaultAsyncCacheApi.java, especially the ClassTag invocation around line 47, and compare it with DefaultSyncCacheApi. Trace the Java-to-Scala call for getOrElseUpdate using the provided CompletableFuture example; done means the Scala implementation receives the actual value type rather than Any for serialization or deserialization.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, scala
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.