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

Add test for field ordering #1042

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

levinson
Copy link

Pull Request Checklist

  • Have you read through the contributor guidelines?
  • Have you squashed your commits?
  • Have you added copyright headers to new files? N/A
  • Have you updated the documentation? N/A
  • Have you added tests for any changed functionality? N/A

Purpose

This PR demonstrates #1038. The new test fails due to serialized field order when compiled with Scala 3:

[info] - should preserve field ordering *** FAILED *** (33 milliseconds)
[info]   "{"[a":42,"m1":"foo","b":"baz","c":"bar","z":1.2]}" did not equal "{"[m1":"foo","c":"bar","z":1.2,"a":42,"b":"baz"]}" (JsonSharedSpec.scala:454)

@levinson levinson force-pushed the field-ordering-scala3-bug branch from cff4e97 to 588157c Compare May 11, 2024 20:38
"preserve field ordering" in {
val model = Model(Some("foo"), "bar", Some(1.2), 42, Some("baz"))
val serialized = Json.stringify(Json.toJson(model))
serialized.mustEqual("""{"m1":"foo","c":"bar","z":1.2,"a":42,"b":"baz"}""")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
serialized.mustEqual("""{"m1":"foo","c":"bar","z":1.2,"a":42,"b":"baz"}""")
serialized.mustEqual("""{"m1":"foo","c":"bar","z":1.2,"a":42,"b":"baz"}""")

@jonaskoelker-jypo
Copy link

Would it make sense to test the field ordering at the JsObject level in addition to the String level?

I note the comment on the fields method on JsObject: it gives the fields in order. Based on this, I would—naively?—expect the Seq returned by fields to be in declaration order when using the Json.writes macro and friends. It would be a real nicety to have a test for that as well.

case class JsObject(
private[json] val underlying: Map[String, JsValue]
) extends JsValue {
/**
* The fields of this JsObject in the order passed to the constructor
*/
def fields: collection.Seq[(String, JsValue)] = underlying.toSeq

[On the other hand: I can argue that a JsObject doesn't contain any information about the field declaration order except the ordering of the ImmutableLinkedHashMap in underlying. Ergo: if it's wrong at the JsObject level there's no way Json.stringify can correct it, and thus this test case implicitly also verifies that underlying is ordered correctly. That feels a bit indirect for my tastes—I lean towards an independent test of that—but I figured it deserved to be mentioned.]

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

Successfully merging this pull request may close these issues.

3 participants