Skip to content

Commit

Permalink
Replace partially different array when creating merge patch
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Jan 24, 2020
1 parent d97dcb5 commit f00d651
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.7.3] - 2020-01-24

### Fixed
- Merge patch was not replacing partially different arrays.

## [3.7.2] - 2019-10-23

### Added
Expand All @@ -30,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Compatibility option to `TOLERATE_ASSOCIATIVE_ARRAYS` that mimic JSON objects.

[3.7.3]: https://github.com/swaggest/json-diff/compare/v3.7.2...v3.7.3
[3.7.2]: https://github.com/swaggest/json-diff/compare/v3.7.1...v3.7.2
[3.7.1]: https://github.com/swaggest/json-diff/compare/v3.7.0...v3.7.1
[3.7.0]: https://github.com/swaggest/json-diff/compare/v3.6.0...v3.7.0
Expand Down
5 changes: 5 additions & 0 deletions src/JsonDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ private function process($original, $new)
}

$isUriFragment = (bool)($this->options & self::JSON_URI_FRAGMENT_ID);
$diffCnt = $this->addedCnt + $this->modifiedCnt + $this->removedCnt;
foreach ($originalKeys as $key => $originalValue) {
if ($this->options & self::STOP_ON_DIFF) {
if ($this->modifiedCnt || $this->addedCnt || $this->removedCnt) {
Expand Down Expand Up @@ -366,6 +367,10 @@ private function process($original, $new)
$this->pathItems = $pathItems;
}

if ($merge && $isArray && $this->addedCnt + $this->modifiedCnt + $this->removedCnt > $diffCnt) {
JsonPointer::add($this->merge, $this->pathItems, $new);
}

// additions
foreach ($newArray as $key => $value) {
$this->addedCnt++;
Expand Down
30 changes: 30 additions & 0 deletions tests/assets/merge.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"key1": [
5,
1,
2,
3
],
"key2": null,
"key3": {
"sub0": null,
"sub1": "c",
"sub2": false,
"sub3": 0
},
"key4": [
{
"c": false,
"a": 2
},
{
"a": 1,
"b": true
},
{
"c": 1,
"a": 3
}
],
"key5": "wat"
}
15 changes: 15 additions & 0 deletions tests/src/MergePatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,20 @@ protected function doTest($case)

}

public function testComplex()
{
$original = json_decode(file_get_contents(__DIR__ . '/../assets/original.json'));
$new = json_decode(file_get_contents(__DIR__ . '/../assets/new.json'));

$diff = new JsonDiff($original, $new);
$mergePatch = $diff->getMergePatch();
$mergePatchJson = json_encode($mergePatch, JSON_UNESCAPED_SLASHES + JSON_PRETTY_PRINT);

$this->assertEquals(file_get_contents(__DIR__ . '/../assets/merge.json') , $mergePatchJson);

JsonMergePatch::apply($original, $mergePatch);
$this->assertEquals($new, $original);
}


}

0 comments on commit f00d651

Please sign in to comment.