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 JsonMatches for [] and {} #2938

Merged
merged 1 commit into from Feb 1, 2018

Conversation

mkasberg
Copy link
Contributor

This commit fixes #2919 (assertJsonStringEqualsJsonString empty object
matches empty array).

We had a problem where JsonMatches would report that the JSON string
'{}' matches the JSON string '[]'. This bug was due to the fact that
Json::canonicalize() was converting JSON strings to PHP arrays. In a PHP
array, there is no difference between an empty associative array and an
empty indexed array, so Json::canonicalize() did not distinguish between
them either. The solution we apply here is to deserialize JSON into
objects (rather than associative arrays) so that PHP can distinguish
between an empty object and an empty array. If the object is not empty,
we still ultimately convert it to an associative array so we can sort
its keys.

This commit fixes sebastianbergmann#2919 (assertJsonStringEqualsJsonString empty object
matches empty array).

We had a problem where JsonMatches would report that the JSON string
'{}' matches the JSON string '[]'. This bug was due to the fact that
Json::canonicalize() was converting JSON strings to PHP arrays. In a PHP
array, there is no difference between an empty associative array and an
empty indexed array, so Json::canonicalize() did not distinguish between
them either. The solution we apply here is to deserialize JSON into
objects (rather than associative arrays) so that PHP _can_ distinguish
between an empty object and an empty array. If the object is not empty,
we still ultimately convert it to an associative array so we can sort
its keys.
@codecov-io
Copy link

codecov-io commented Dec 23, 2017

Codecov Report

Merging #2938 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #2938      +/-   ##
============================================
+ Coverage     79.86%   79.86%   +<.01%     
- Complexity     2822     2824       +2     
============================================
  Files           107      107              
  Lines          7438     7440       +2     
============================================
+ Hits           5940     5942       +2     
  Misses         1498     1498
Impacted Files Coverage Δ Complexity Δ
src/Util/Json.php 100% <100%> (ø) 9 <0> (+2) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 92a2617...9664d70. Read the comment docs.

@sebastianbergmann sebastianbergmann merged commit 8e06f3f into sebastianbergmann:master Feb 1, 2018
@mkasberg mkasberg deleted the issue-2919 branch February 1, 2018 05:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

assertJsonStringEqualsJsonString empty object matches empty array
3 participants