Giter Site home page Giter Site logo

Comments (14)

marko-asplund avatar marko-asplund commented on June 10, 2024 1

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.

marko-asplund avatar marko-asplund commented on June 10, 2024 1

Here's a possible workaround marko-asplund/play-json-derived-codecs-issue@fc0c7b0

from play-json-derived-codecs.

julienrf avatar julienrf commented on June 10, 2024 1

It seems that they are aware of the problem: playframework/play-json#404

from play-json-derived-codecs.

julienrf avatar julienrf commented on June 10, 2024

Thanks for the report @marko-asplund. Would you be interested in investigating what’s going on?

from play-json-derived-codecs.

marko-asplund avatar marko-asplund commented on June 10, 2024

@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.

julienrf avatar julienrf commented on June 10, 2024

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.

marko-asplund avatar marko-asplund commented on June 10, 2024

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.

julienrf avatar julienrf commented on June 10, 2024

Hmm, I don’t know what’s wrong. Your setup looks good to me. Did you reload sbt?

from play-json-derived-codecs.

marko-asplund avatar marko-asplund commented on June 10, 2024

Yeah, I did try reloading and restarting sbt several times.

from play-json-derived-codecs.

julienrf avatar julienrf commented on June 10, 2024

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.

marko-asplund avatar marko-asplund commented on June 10, 2024

@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.

julienrf avatar julienrf commented on June 10, 2024

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.

marko-asplund avatar marko-asplund commented on June 10, 2024

🤔 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.

julienrf avatar julienrf commented on June 10, 2024

That’s a good point. Personally, I don’t understand this decision.

from play-json-derived-codecs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.