Skip to content

Unexpected diff when comparing similar objects #4

@andreasfreier

Description

@andreasfreier

For the following two similar objects

a = [
  {key: 'a', value: '0'},
  {key: 'BUG', value: ''},
  {key: 'c', value: '1'},
  {key: 'd', value: '2'},
  {key: 'e', value: '3'},
  {key: 'f', value: '4'},
  {key: 'g', value: '5'},
  {key: 'BUG', value: ''}
]


b = [
  {key: 'a', value: '0'},
  {key: 'BUG', value: ''},
  {key: 'c', value: '1'},
  {key: 'd', value: '2'},
  {key: 'e', value: '3'},
  {key: 'f', value: '4'},
  {key: 'g', value: '5'},
  {key: 'BUG', value: ''}
]

JsonDiff.diff(a, b) returns the following diff

[{"op"=>"move", "from"=>"/7", "path"=>"/1"}, {"op"=>"move", "from"=>"/2", "path"=>"/7"}]

which is replacing the similar lines with each other.

Strangely, if we remove any of the a/c/d/e/f/g lines in both objects, the bug isn't there anymore, e.g.

a = [
  {key: 'BUG', value: ''},
  {key: 'c', value: '1'},
  {key: 'd', value: '2'},
  {key: 'e', value: '3'},
  {key: 'f', value: '4'},
  {key: 'g', value: '5'},
  {key: 'BUG', value: ''}
]


b = [
  {key: 'BUG', value: ''},
  {key: 'c', value: '1'},
  {key: 'd', value: '2'},
  {key: 'e', value: '3'},
  {key: 'f', value: '4'},
  {key: 'g', value: '5'},
  {key: 'BUG', value: ''}
]

JsonDiff.diff(a, b)
=> []

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions