Skip to content

Commit df30535

Browse files
authored
Merge pull request #29 from swaggest/fix-merge-patch
Replace partially different array when creating merge patch
2 parents d97dcb5 + f00d651 commit df30535

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [3.7.3] - 2020-01-24
8+
9+
### Fixed
10+
- Merge patch was not replacing partially different arrays.
11+
712
## [3.7.2] - 2019-10-23
813

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

38+
[3.7.3]: https://github.com/swaggest/json-diff/compare/v3.7.2...v3.7.3
3339
[3.7.2]: https://github.com/swaggest/json-diff/compare/v3.7.1...v3.7.2
3440
[3.7.1]: https://github.com/swaggest/json-diff/compare/v3.7.0...v3.7.1
3541
[3.7.0]: https://github.com/swaggest/json-diff/compare/v3.6.0...v3.7.0

src/JsonDiff.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ private function process($original, $new)
323323
}
324324

325325
$isUriFragment = (bool)($this->options & self::JSON_URI_FRAGMENT_ID);
326+
$diffCnt = $this->addedCnt + $this->modifiedCnt + $this->removedCnt;
326327
foreach ($originalKeys as $key => $originalValue) {
327328
if ($this->options & self::STOP_ON_DIFF) {
328329
if ($this->modifiedCnt || $this->addedCnt || $this->removedCnt) {
@@ -366,6 +367,10 @@ private function process($original, $new)
366367
$this->pathItems = $pathItems;
367368
}
368369

370+
if ($merge && $isArray && $this->addedCnt + $this->modifiedCnt + $this->removedCnt > $diffCnt) {
371+
JsonPointer::add($this->merge, $this->pathItems, $new);
372+
}
373+
369374
// additions
370375
foreach ($newArray as $key => $value) {
371376
$this->addedCnt++;

tests/assets/merge.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"key1": [
3+
5,
4+
1,
5+
2,
6+
3
7+
],
8+
"key2": null,
9+
"key3": {
10+
"sub0": null,
11+
"sub1": "c",
12+
"sub2": false,
13+
"sub3": 0
14+
},
15+
"key4": [
16+
{
17+
"c": false,
18+
"a": 2
19+
},
20+
{
21+
"a": 1,
22+
"b": true
23+
},
24+
{
25+
"c": 1,
26+
"a": 3
27+
}
28+
],
29+
"key5": "wat"
30+
}

tests/src/MergePatchTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,5 +289,20 @@ protected function doTest($case)
289289

290290
}
291291

292+
public function testComplex()
293+
{
294+
$original = json_decode(file_get_contents(__DIR__ . '/../assets/original.json'));
295+
$new = json_decode(file_get_contents(__DIR__ . '/../assets/new.json'));
296+
297+
$diff = new JsonDiff($original, $new);
298+
$mergePatch = $diff->getMergePatch();
299+
$mergePatchJson = json_encode($mergePatch, JSON_UNESCAPED_SLASHES + JSON_PRETTY_PRINT);
300+
301+
$this->assertEquals(file_get_contents(__DIR__ . '/../assets/merge.json') , $mergePatchJson);
302+
303+
JsonMergePatch::apply($original, $mergePatch);
304+
$this->assertEquals($new, $original);
305+
}
306+
292307

293308
}

0 commit comments

Comments
 (0)