Comments (14)
This issue seems to have been caused by variance changes in play.api.libs.json.Writes
between play-json v2.7 and v2.8.
Writes
was made invariant on A
whereas before it was contravariant.
play-json v2.7.4
package play.api.libs.json
trait Writes[-A] { self =>
...
play-json v2.8.1
package play.api.libs.json
trait Writes[A] { self =>
...
This changed in commit playframework/play-json@ba86577#diff-43f2e42c6ce469bcd932a93a187413fe
Because of this change Json.toJson
doesn't allow serializing SubType
using OWrites[SuperType]
anymore.
from play-json-derived-codecs.
Here's a possible workaround marko-asplund/play-json-derived-codecs-issue@fc0c7b0
from play-json-derived-codecs.
It seems that they are aware of the problem: playframework/play-json#404
from play-json-derived-codecs.
Thanks for the report @marko-asplund. Would you be interested in investigating what’s going on?
from play-json-derived-codecs.
@julienrf This issue is currently blocking Play v2.8 upgrade, so I'll need to figure out some kind of workaround. Any pointers for investigating the issue?
from play-json-derived-codecs.
I think you did a great job of sharing the test cases that fail. Thank you for this! The next step would be to debug which implicit definitions are used in both cases (with v6 and v7) to see where they diverge. The compiler plugin tek/splain could help a lot in this area.
from play-json-derived-codecs.
Thanks for the tip! 👍 I'll look into debugging using the plugin.
🤔 How do I get the plugin to output implicit definitions? I've tried setting this up in build.sbt
, but e.g. compile
doesn't seem to generate any additional output.
from play-json-derived-codecs.
Hmm, I don’t know what’s wrong. Your setup looks good to me. Did you reload sbt?
from play-json-derived-codecs.
Yeah, I did try reloading and restarting sbt several times.
from play-json-derived-codecs.
Thanks for the investigation @marko-asplund!
I guess for the second problem you’ll have to manually upcast your values:
- val b2 = Json.toJson(Bar2("hello", 55))
+ val b2 = Json.toJson(Bar2("hello", 55): Foo2)
The issue 1 is more problematic. I’m not sure how to fix it.
from play-json-derived-codecs.
@julienrf Thanks for the feedback!
issue 1: Writes
was changed to be invariant on A
. Before this change both Writes[SubType]
and Writes[SuperType]
were eligible for implicit resolution, but after the change only Writes[SubType]
is, effectively forcing Writes[SubType]
to get picked up.
issue 2: only Writes[SuperType]
is defined. Since Writes
is now invariant it can't be used with SubType
.
IMO, though it's not an ideal solution, passing the desired type parameter to Json.toJson
(Json.toJson[SuperType]
) or upcasting to SuperType
can be used as a workaround and force use of Writes[SuperType]
in both cases.
🤔 Trying to figure out what play-json-derived-codecs could do to address this issue.
Not sure if there's much that can be done in terms of addressing this issue in terms of play-json-derived-codecs features.
As I guess this issue has the potential to hit many play-json-derived-codecs users who are using it with sum types, one thing could be to add a caveat in the README about upgrading from v6 to v7.
from play-json-derived-codecs.
As I guess this issue has the potential to hit many play-json-derived-codecs users who are using it with sum types, one thing could be to add a caveat in the README about upgrading from v6 to v7.
Yes, that’s a good idea, thanks for proposing! We should provide the workarounds in the README.
from play-json-derived-codecs.
🤔 Also wondering what was the exact rationale for play-json to go this way. It would've been very helpful to publish a heads up about this e.g. in the form of breaking changes in v2.8 release notes. I couldn't find anything on this, at least not with a quick search and couldn't find release notes for v2.8 either.
from play-json-derived-codecs.
That’s a good point. Personally, I don’t understand this decision.
from play-json-derived-codecs.
Related Issues (20)
- Can't figure out how to write logic to select sub-type HOT 1
- Release for Scala 2.13.x
- make type renaming shorter
- Class default values supporting HOT 3
- Output null for None values HOT 2
- Release for Scala 2.13.0 HOT 1
- Map Property in child class causes issues HOT 2
- Support Play 2.8 HOT 3
- Compiler Blows Up After Adding Big Sum Type and Big Product Type HOT 8
- How to use DerivedReads with 2 params? HOT 1
- Mismatched Reads/Writes when using custom formats HOT 3
- Excluding type tag from json HOT 4
- jar for 10.0.0 doesn't contain any classes HOT 2
- Stack overflow after migration to 10.0.1 from 9.0.0 HOT 3
- Adding type tags to individual case classes HOT 4
- Casing mismatch HOT 2
- stackOverflowError
- Support for play-json 3.x HOT 2
- Scala 3 support HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from play-json-derived-codecs.