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

Fix JSON object order comparision with numeric keys #5941

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions src/Util/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
use const JSON_PRETTY_PRINT;
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;
use function count;
use function is_array;
use const SORT_STRING;
use function is_object;
use function is_scalar;
use function json_decode;
use function json_encode;
use function json_last_error;
Expand Down Expand Up @@ -72,24 +72,31 @@ public static function canonicalize(string $json): array
*/
private static function recursiveSort(mixed &$json): void
{
if (!is_array($json)) {
// If the object is not empty, change it to an associative array
// so we can sort the keys (and we will still re-encode it
// correctly, since PHP encodes associative arrays as JSON objects.)
// But EMPTY objects MUST remain empty objects. (Otherwise we will
// re-encode it as a JSON array rather than a JSON object.)
// See #2919.
if (is_object($json) && count((array) $json) > 0) {
$json = (array) $json;
} else {
return;
}
// Nulls, empty arrays, and scalars need no further handling.
if (!$json || is_scalar($json)) {
return;
}

ksort($json);
$isObject = is_object($json);

if ($isObject) {
// Objects need to be sorted during canonicalization to ensure
// correct comparsion since JSON objects are unordered. It must be
// kept as an object so that the value correctly stays as a JSON
// object instead of potentially being converted to an array. This
// approach ensures that numeric string JSON keys are preserved and
// don't risk being flattened due to PHP's array semantics.
// See #2919, #4584, #4674
$json = (array) $json;
ksort($json, SORT_STRING);
}

foreach ($json as &$value) {
self::recursiveSort($value);
}

if ($isObject) {
$json = (object) $json;
}
}
}
165 changes: 165 additions & 0 deletions tests/unit/Framework/Constraint/JsonMatchesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public static function provider(): array
'{"first":1, "second":"2"}',
],

'object fields with numeric keys are unordered' => [
true,
'',
'',
'{"0":null,"a":{},"b":[],"c":"1","d":1,"e":-1,"f":[1,2],"g":[2,1],"h":{"0":"0","1":"1","2":"2"}}',
'{"a":{},"d":1,"b":[],"e":-1,"0":null,"c":"1","f":[1,2],"h":{"2":"2","1":"1","0":"0"},"g":[2,1]}',
],

'child object fields are unordered' => [
true,
'',
Expand Down Expand Up @@ -198,6 +206,139 @@ public static function provider(): array
'["first", "second"]',
],

'objects with numeric keys are not arrays' => [
false,
'Failed asserting that \'[{}]\' matches JSON string "{"0":{}}".',
<<<'EOT'
Failed asserting that two json values are equal.
--- Expected
+++ Actual
@@ @@
-{
- "0": {}
-}
+[
+ {}
+]

EOT,
'{"0":{}}',
'[{}]',
],

'child array elements are ordered' => [
false,
'Failed asserting that \'{"a":{},"d":1,"b":[],"e":-1,"0":null,"c":"1","f":[2,1],"h":{"2":"2","1":"1","0":"0"},"g":[2,1]}\' matches JSON string "{"0":null,"a":{},"b":[],"c":"1","d":1,"e":-1,"f":[1,2],"g":[2,1],"h":{"0":"0","1":"1","2":"2"}}".',
<<<'EOT'
Failed asserting that two json values are equal.
--- Expected
+++ Actual
@@ @@
"d": 1,
"e": -1,
"f": [
- 1,
- 2
+ 2,
+ 1
],
"g": [
2,

EOT,
'{"0":null,"a":{},"b":[],"c":"1","d":1,"e":-1,"f":[1,2],"g":[2,1],"h":{"0":"0","1":"1","2":"2"}}',
'{"a":{},"d":1,"b":[],"e":-1,"0":null,"c":"1","f":[2,1],"h":{"2":"2","1":"1","0":"0"},"g":[2,1]}',
],

'child object with numeric fields stay as object' => [
false,
'Failed asserting that \'{"a":{},"d":1,"b":[],"e":-1,"0":null,"c":"1","f":[1,2],"h":["0","1","2"],"g":[2,1]}\' matches JSON string "{"0":null,"a":{},"b":[],"c":"1","d":1,"e":-1,"f":[1,2],"g":[2,1],"h":{"0":"0","1":"1","2":"2"}}".',
<<<'EOT'
Failed asserting that two json values are equal.
--- Expected
+++ Actual
@@ @@
2,
1
],
- "h": {
- "0": "0",
- "1": "1",
- "2": "2"
- }
+ "h": [
+ "0",
+ "1",
+ "2"
+ ]
}

EOT,
'{"0":null,"a":{},"b":[],"c":"1","d":1,"e":-1,"f":[1,2],"g":[2,1],"h":{"0":"0","1":"1","2":"2"}}',
'{"a":{},"d":1,"b":[],"e":-1,"0":null,"c":"1","f":[1,2],"h":["0","1","2"],"g":[2,1]}',
],

'nested arrays are ordered' => [
false,
'Failed asserting that \'[{"1":"1","0":"0"},{"2":"2","3":"3"}]\' matches JSON string "[[1,0],[2,3]]".',
<<<'EOT'
Failed asserting that two json values are equal.
--- Expected
+++ Actual
@@ @@
[
- [
- 1,
- 0
- ],
- [
- 2,
- 3
- ]
+ {
+ "0": "0",
+ "1": "1"
+ },
+ {
+ "2": "2",
+ "3": "3"
+ }
]

EOT,
'[[1,0],[2,3]]',
'[{"1":"1","0":"0"},{"2":"2","3":"3"}]',
],

'child objects in arrays stay in order' => [
false,
'Failed asserting that \'[{"2":"2","3":"3"},{"1":"1","0":"0"}]\' matches JSON string "[{"0":"0","1":"1"},{"2":"2","3":"3"}]".',
<<<'EOT'
Failed asserting that two json values are equal.
--- Expected
+++ Actual
@@ @@
[
{
+ "2": "2",
+ "3": "3"
+ },
+ {
"0": "0",
"1": "1"
- },
- {
- "2": "2",
- "3": "3"
}
]

EOT,

'[{"0":"0","1":"1"},{"2":"2","3":"3"}]',
'[{"2":"2","3":"3"},{"1":"1","0":"0"}]',
],

'objects are not arrays' => [
false,
'Failed asserting that \'{}\' matches JSON string "[]".',
Expand All @@ -213,6 +354,30 @@ public static function provider(): array
'[]',
'{}',
],

'arrays are not objects' => [
false,
'Failed asserting that \'{}\' matches JSON string "[]".',
<<<'EOT'
Failed asserting that two json values are equal.
--- Expected
+++ Actual
@@ @@
-[]
+{}

EOT,
'[]',
'{}',
],

'objects in arrays are unordered' => [
true,
'',
'',
'[{"0":"0","1":"1"},{"2":"2","3":"3"}]',
'[{"1":"1","0":"0"},{"2":"2","3":"3"}]',
],
];
}

Expand Down