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

Diff on arrays with single values is reducing them to the value #1384

Open
mcnerneyd opened this issue Oct 23, 2023 · 0 comments
Open

Diff on arrays with single values is reducing them to the value #1384

mcnerneyd opened this issue Oct 23, 2023 · 0 comments

Comments

@mcnerneyd
Copy link

json4s version

4.1.0-M3
4.0.6

scala version

2.13.8

jdk version

Open JDK 11

When I do a diff on two objects both containing the same key pointing to an array, and the new value contains only one value then the resulting change is that single value as opposed to an array containing that value.

The following test demonstrates the problem:

    val v0 = JsonMethods.parse("""{ "a": [8,9] }""")
    val v1 = JsonMethods.parse("""{ "a": [10,11] }""")

    val Diff(c1,a1,d1) = v0 diff v1
    println("     c:" + c1)
    println("     a:" + a1)
    println("     d:" + d1)
    c1 \ "a" shouldBe JArray(List(10, 11))

    val v2 = JsonMethods.parse("""{ "a": [10] }""")

    val Diff(c2,a2,d2) = v0 diff v2
    println("     c:" + c2)
    println("     a:" + a2)
    println("     d:" + d2)
    c2 \ "a"  shouldBe JArray(List(10))  // Fails
  }

The final value, rather than being [10] is just 10.

The problem appears to be on line 102 of Diff.scala (4.1.0-M3):

      case (xs, Nil) => Diff(JNothing, JNothing, if (xs.isEmpty) JNothing else JArray(xs))
      case (Nil, ys) => Diff(JNothing, if (ys.isEmpty) JNothing else JArray(ys), JNothing)
      case (x :: xs, y :: ys) =>
        val Diff(c1, a1, d1) = diff(x, y)
        val Diff(c2, a2, d2) = diffRec(xs, ys)
        Diff(c1 ++ c2, a1 ++ a2, d1 ++ d2)           // Line 102
    }

Where c1 is any value and c2 is nothing, the result is c1 when it should be a JArray of c1.

Possibly, a combiner method is needed that combines JValue with a JArray as normal, but a JValue with a JNothing as a new JArray.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant