-
-
Notifications
You must be signed in to change notification settings - Fork 134
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
JSON field ordering bug with Scala 3 #1038
Comments
My semi-systematic experimentation suggests that the observed field order will be:
By declaration order, I mean the same order as So if either all fields are Here's a blueprint for a workaround:
This can be simplified—you can use apply instead of get and then remove the collect—if all fields are always present in the output, e.g. if you use It is of note that the bug seems to still occur if all fields are present in the output—so it must depend on the optionality in the declaration, not in the output. In version 3.0.3 I did some jump-to-source maneuvers in my application:
That sounds suspicious, but it's no smoking gun. I dug around some more, and found this:
This looks like the cause of exactly the pattern I have observed, i.e. a smoking gun. Across my work I think I have only seen the bug in those projects that use Scala 3. This makes sense, the apparent smoking gun is in a scala3-specific folder, so one would expect the bug to occur if and only if using Scala 3. This matches what @levinson wrote in the issue-creating post: "TODO: Test fails when using Scala 3". I hope this helps. |
Happens here as well.
In scala 2.13, generated json was according to field order, but after upgrading to Scala 3, this is the order of an example:
It breaks a lot of tests we have :( |
Play JSON Version (2.5.x / etc)
Affected versions:
API (Scala / Java / Neither / Both)
Scala
Operating System (Ubuntu 15.10 / MacOS 10.10 / Windows 10)
MacOS 14.3.1
JDK (Oracle 1.8.0_72, OpenJDK 1.8.x, Azul Zing)
java version "17.0.10" 2024-01-16 LTS
Java(TM) SE Runtime Environment (build 17.0.10+11-LTS-240)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.10+11-LTS-240, mixed mode, sharing)
Expected Behavior
Expected JSON ordering to be preserved when serializing model class.
Actual Behavior
JSON field ordering is not preserved when compiling using Scala 3
Reproducible Test Case
I created the following project to demonstrate the issue: https://github.com/levinson/play-json-scala3-ordering-bug
Posting the source code here as well for reference:
build.sbt:
JsonSerializersTest.scala
The text was updated successfully, but these errors were encountered: