keploy / keploy

Test generation for Developers. Generate tests and stubs for your application that actually work!
https://keploy.io
Apache License 2.0
3.4k stars 375 forks source link

[bug]: JSON array diff not specific enough #1496

Open slayerjain opened 3 months ago

slayerjain commented 3 months ago

Is there an existing issue for this?

Current behavior

Currently is there is a diff in a JSON array, keploy would highlight the entire string after the faulty string. This makes it hard to see the diff. Eg:

Screenshot 2024-02-05 at 1 38 46 PM

It should only highlight the difference and leave the rest. The potential reason could be that the JSON array diff compare is not implemented for JSON Array type or there is incorrect matching of type.

Steps to reproduce

  1. Create an application which returns a JSON Array.
  2. Record test cases.
  3. Change a field from any of the object from the array.
  4. Non diff items will also be highlighted (as above)

Environment

None

Version

Cloud

Repository

keploy

Akash-Singh04 commented 3 months ago

Hey there! I would like to try fixing this.

slayerjain commented 3 months ago

@Akash-Singh04 Please feel free :)

slayerjain commented 3 months ago

@shivamsouravjha please review this once there is a PR.

Akash-Singh04 commented 3 months ago

@slayerjain Could you provide the exact application in which the jsonarr diff are not visible. It would make it easier for me to develop a solution if you could provide it. Thanks

shivamsouravjha commented 3 months ago

Hey @Akash-Singh04 ! You can try with Pet Clinic sample app to replicate the error. This happens when any element in an array changes and everything after that value would be shown in different color.

Akash-Singh04 commented 3 months ago

According to my investigation, the error lies in here:

func calculateJSONDiffs(json1 []byte, json2 []byte) (string, error) {...} and func separateAndColorize(diffStr string, noise map[string][]string) (string, string) {...}

The calculateJSONDiffs is not catered to handle json arrays, similarly the separateaAndColourize function also doesn't work for json arrays, only for Jsons.

Please let me know how we can refactor this to solve the issue

shivamsouravjha commented 3 months ago

Hey @Akash-Singh04 in calculateJSONDiffs the Format function does have formatArray and formatObject.

Can you please explain the error in detail.

Akash-Singh04 commented 3 months ago

I am referring to these two functions

image

image

The separateAndColourize function is documented to only work with Json

image

The function sprintJSONDiff which calls calculateJSONDiffs and separateaAndColourize is also documented to only work with JSONS

Hence, when an array of Jsons is passed, sprintDiff is called instead image

This according to me is the cause of the problem.

shivamsouravjha commented 3 months ago

Agreed this seems to be the problem with the calculateJSONDiffs as it throws error on diff.Compare.

The dependency seems to be inactive for a while and thus we're looking for alternatives as well.

image
Akash-Singh04 commented 3 months ago

Alright, let me know what is the alternative approach to this. Had a lot of fun learning and debugging this!

Akash-Singh04 commented 4 weeks ago

Since we switched to GJSON from gojsondiff, this issue was resolved and can be closed @slayerjain