Last modified: 2014-07-25 23:34:56 UTC

Wikimedia Bugzilla is closed!

Wikimedia has migrated from Bugzilla to Phabricator. Bug reports should be created and updated in Wikimedia Phabricator instead. Please create an account in Phabricator and add your Bugzilla email address to it.
Wikimedia Bugzilla is read-only. If you try to edit or create any bug report in Bugzilla you will be shown an intentional error message.
In order to access the Phabricator task corresponding to a Bugzilla report, just remove "static-" from its URL.
You could still run searches in Bugzilla or access your list of votes but bug reports will obviously not be up-to-date in Bugzilla.
Bug 10887 - API JSON formatter returns [] as an empty return value - inconsistent with {} for non-empty values
API JSON formatter returns [] as an empty return value - inconsistent with {}...
Status: REOPENED
Product: MediaWiki
Classification: Unclassified
API (Other open bugs)
1.21.x
All All
: Lowest trivial with 1 vote (vote)
: ---
Assigned To: Nobody - You can work on this!
http://en.wikipedia.org/w/api.php?act...
:
: 49201 (view as bug list)
Depends on:
Blocks: api-2.0 67054
  Show dependency treegraph
 
Reported: 2007-08-11 21:29 UTC by Bryan Tong Minh
Modified: 2014-07-25 23:34 UTC (History)
9 users (show)

See Also:
Web browser: ---
Mobile Platform: ---
Assignee Huggle Beta Tester: ---


Attachments

Description Bryan Tong Minh 2007-08-11 21:29:05 UTC
Compare a query with results and without:
http://en.wikipedia.org/w/api.php?action=query&generator=embeddedin&geititle=Template:Stub&geilimit=5&prop=info&inprop=protection&format=jsonfm
http://en.wikipedia.org/w/api.php?action=query&generator=embeddedin&geititle=Template:Stubaaaa&geilimit=5&prop=info&inprop=protection&format=jsonfm

The query with results returns an JSON object ({...}), while the query without results returns an empty JSON list ([]). For consistency, it should return an empty object {}.
Comment 1 Brion Vibber 2007-08-13 14:58:00 UTC
Note that lists and dictionaries/associative arrays are the same type in PHP, making it somewhat difficult to do this consistently without some manual poking about -- you can't tell an empty "list" from an empty "dictionary". You'd have to know what type it 'should' be, or wrap it in some kind of special wrapper object.
Comment 2 Bryan Tong Minh 2007-08-15 14:18:44 UTC
Or maybe this is more preferably: {'query':{'pages':{}}}. That still leaves the associative array problem of course.
Comment 3 Yuri Astrakhan 2007-08-15 20:14:47 UTC
We could return null instead of a list/array - removes the problem all together...
Comment 4 Roan Kattouw 2007-09-03 20:22:58 UTC
Is [] illegal in JSON then? If [] is legal, there shouldn't be much of a problem, should there?
Comment 5 Bryan Tong Minh 2007-09-03 20:28:15 UTC
It's not that much a problem, it's only inconsistent. It causes some problems in Python for scripts that assume that the API returns a dict {}. It is easily worked around though, because both [] and {} are equal in a boolean sense.
Comment 6 Roan Kattouw 2007-09-03 20:51:49 UTC
Resolving as LATER. [] is legal syntax, but inconsistent. If Python can't handle [], that's a problem in their JSON interpreter. We could fix this in theory, but it's so much hassle that it's only practical to do if the formatting code as a whole is revised.
Comment 7 Andre Klapper 2012-12-21 14:17:29 UTC
Still valid in 1.21wmf6.
[Removing RESOLVED LATER as discussed in http://lists.wikimedia.org/pipermail/wikitech-l/2012-November/064240.html . Reopening and setting priority to "Lowest". For future reference, please use either RESOLVED WONTFIX (for issues that will not be fixed), or simply set lowest priority. Thanks a lot!]
Comment 8 db [inactive,noenotif] 2013-06-08 06:45:51 UTC
*** Bug 49201 has been marked as a duplicate of this bug. ***
Comment 9 Brion Vibber 2013-09-26 22:07:11 UTC
(In reply to comment #6)
> Resolving as LATER. [] is legal syntax, but inconsistent. If Python can't
> handle [], that's a problem in their JSON interpreter. We could fix this in
> theory, but it's so much hassle that it's only practical to do if the
> formatting code as a whole is revised.

Just to clarify on this old bug -- the Python JSON interpreter can handle [] just fine, but it creates a *list* (equivalent to JS *array*) instead of a *dict* (equivalent to JS *object*).

This can cause problems if you do some kind of operation on what you expected would be a dict/object/map/hash/associative array.

This is not Python-specific, but could also cause problems in Java, Objective-C, C++, and ... basically anything but PHP .... especially for those with strong typing, you might even throw an error when simply assigning an extracted object to a variable of what turns out to be the wrong type.


A few possible ways to resolve this, should we care to:

a) use objects instead of associative arrays in the PHP code in the first place

b) use a special array key value that the JSON formatter knows to drop from the output, that forces an otherwise empty array to be treated as an associative array

c) create a special object type that wraps associative arrays in the API output, that the JSON formatter knows how to deal with

Under PHP 5.4, option c) can be combined with the JsonSerializable interface to ensure that a wrapped associative array outputs the way we want it even if people use json_encode() directly, so this could be used outside the API as well.

Combine that with the array-like object interface, and the array wrapper also becomes transparent to array-like uses in PHP. I think this is my favorite method...

  $data['pages'] = $someArray;
  // ...
  echo json_encode( $data ); // {"pages":[]} OOPS!

fix to:

  // wrap an array that might be empty
  $data['pages'] = new JsonAssociativeArray( $someArray );
  // ...
  echo json_encode( $data ); // {"pages":{}}

or

  // start with an empty array
  $foo = new JsonAssociativeArray();
  if ( $condition ) {
    $foo["bar"] = "baz";
  }
  $data['pages'] = $foo;
  // ...
  echo json_encode( $data ); // {"pages":{}}
Comment 10 Bartosz Dziewoński 2014-02-09 18:53:46 UTC
Couldn't we check for the '_element' key (normally used by the XML formatter) and treat otherwise empty PHP arrays as arrays if it's present, and as objects if it's not? (Note that I have no idea what I'm talking about.)
Comment 11 Brad Jorsch 2014-02-10 00:33:28 UTC
Probably. Although I'd really like to get rid of the need for that "_element" key entirely.
Comment 12 Matthew Flaschen 2014-05-20 03:10:16 UTC
> a) use objects instead of associative arrays in the PHP code in the first place

Or perhaps just cast empty arrays to objects.
Comment 13 Brad Jorsch 2014-06-17 14:20:23 UTC
*** Bug 66720 has been marked as a duplicate of this bug. ***

Note You need to log in before you can comment on or make changes to this bug.


Navigation
Links