Last modified: 2013-03-01 20:28:40 UTC

Wikimedia Bugzilla is closed!

Wikimedia migrated from Bugzilla to Phabricator. Bug reports are handled in Wikimedia Phabricator.
This static website is read-only and for historical purposes. It is not possible to log in and except for displaying bug reports and their history, links might be broken. See T47303, the corresponding Phabricator task for complete and up-to-date bug report information.
Bug 45303 - Ask API should return [] for ordered lists and {} for maps that are not ordered
Ask API should return [] for ordered lists and {} for maps that are not ordered
Status: REOPENED
Product: MediaWiki extensions
Classification: Unclassified
Semantic MediaWiki (Other open bugs)
REL1_20-branch
All All
: Unprioritized normal with 1 vote (vote)
: ---
Assigned To: Nobody - You can work on this!
:
: 45298 (view as bug list)
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2013-02-23 07:28 UTC by cariaso
Modified: 2013-03-01 20:28 UTC (History)
6 users (show)

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


Attachments

Description cariaso 2013-02-23 07:28:35 UTC
he api response format loses the sorting order.

This is because "results" is returned as an object instead of as a list.

Currently the response has this structure

{"query":
  {"printrequests":[
     {"label":"","typeid":"_wpg","mode":2},                  
     {"label":"Allele1","typeid":"_str","mode":1},
     {"label":"Allele2","typeid":"_str","mode":1},
     {"label":"Magnitude","typeid":"_num","mode":1},
     {"label":"On chromosome","typeid":"_str","mode":1}],

   "results": {
      "Rs121964853(A;A)":
        {"printouts":{
            "Allele1":["A"],
            "Allele2":["A"],
            "Magnitude":[4],"On chromosome":["1"]
          },
        "fulltext":"Rs121964853(A;A)",
        "fullurl":"http:\/\/bots.snpedia.com\/index.php\/Rs121964853(A;A)"
        },

      "Rs121909520(G;G)":
        {"printouts":{
            "Allele1":["G"],
            "Allele2":["G"],
            "Magnitude":[4],
            "On chromosome"["1"]
         },
        "fulltext":"Rs121909520(G;G)",
        "fullurl":"http:\/\/bots.snpedia.com\/index.php\/Rs121909520(G;G)"},


...


I think a more correct structure would be


{"query":
  {"printrequests":[
     {"label":"","typeid":"_wpg","mode":2},                  
     {"label":"Allele1","typeid":"_str","mode":1},
     {"label":"Allele2","typeid":"_str","mode":1},
     {"label":"Magnitude","typeid":"_num","mode":1},
     {"label":"On chromosome","typeid":"_str","mode":1}],

   "results": [
        {"printouts":{
            "Allele1":["A"],
            "Allele2":["A"],
            "Magnitude":[4],"On chromosome":["1"]
          },
        "fulltext":"Rs121964853(A;A)",
        "fullurl":"http:\/\/bots.snpedia.com\/index.php\/Rs121964853(A;A)"
        },

        {"printouts":{
            "Allele1":["G"],
            "Allele2":["G"],
            "Magnitude":[4],
            "On chromosome"["1"]
         },
        "fulltext":"Rs121909520(G;G)",
        "fullurl":"http:\/\/bots.snpedia.com\/index.php\/Rs121909520(G;G)"},
Comment 1 Nischay Nahata 2013-02-24 03:22:59 UTC
*** Bug 45298 has been marked as a duplicate of this bug. ***
Comment 2 Jeroen De Dauw 2013-02-24 17:07:18 UTC
> This is because "results" is returned as an object instead of as a list.

In JS, maps are order dependent while arrays are not. Don't see how returning results as map would cause a sorting problem. It seems more likely something on the backend changed that causes sort to be no longer applied. 

With these settings the API will return the executed SQL queries, so you can check if this is the case:

$wgDebugDumpSql = true;
$wgDebugToolbar = true;

Did the actual output of the API change from arrays to maps recently?
Comment 3 MWJames 2013-02-24 17:14:56 UTC
Just to chip in here, the SMW\DISerializer did not change the output recently and it was always an results dictionary object (rather then the proposed change to an array).
Comment 4 cariaso 2013-02-24 23:08:56 UTC
The json response text is correctly ordered, but because the type is a {} instead of a [] all of the libraries which turn the response into an object do not preserve the ordering.
Comment 5 Jamie Thingelstad 2013-02-25 02:55:38 UTC
Sorry, I can confirm the comment above. I forgot that my JSON formatter extension was displaying an interpretation of the raw results. Indeed, the raw result from the API is indeed sorted. But once the JSON formatter goes through it the order is lost. The order is also lost when using simplejson in Python. I guess this really isn't a SMW issue, unless the return format is changed which seems unlikely and possibly a bad idea.
Comment 6 Jeroen De Dauw 2013-02-26 17:22:30 UTC
I'm going to close this as {} is the correct thing to return for order dependent collections. [] is a list, which is order independent in many languages such as JS.
Comment 7 cariaso 2013-02-28 04:07:51 UTC
I recognize the issue has been closed. But will continue to record some relevant notes on this topic.

http://search.cpan.org/~mlehmann/JSON-XS-2.33/XS.pm#JSON_->_PERL
Comment 8 cariaso 2013-02-28 04:53:00 UTC
http://www.ietf.org/rfc/rfc4627.txt

 An object is an unordered collection of zero or more name/value
   pairs, where a name is a string and a value is a string, number,
   boolean, null, object, or array.

 An array is an ordered sequence of zero or more values.
Comment 9 Jeroen De Dauw 2013-03-01 16:43:49 UTC
If that is the JSON spec, then we better adhere to it. The ordered vs unordered discussion came up in the Wikidata team a while back and there we figured that JavaScript treats lists as unordered, and that we thus should use maps if we want order to be retained. The JSON spec was not used in this discussion though. Guess it requires revisiting...
Comment 10 jeblad 2013-03-01 20:23:38 UTC
I said in that discussion that Javascript object preserve its ordering, but it must have been old information as the current doc for ECMAscript states that it does not preserve ordering. In our API we use key-value pairs as in the object notation ({}), where the value might be arrays ([]) and in some cases arrays of objects. Arrays are used if we must preserve order, and we make no assumption about order in objects other than what we might deduce from sorting the keys.

That said I think some browsers still preserve order in objects, but as long as it isn't consistent and according to the JSON-spec we can't use it.

See for example the answer about V8 in http://stackoverflow.com/questions/7214293/is-the-order-of-elements-in-a-json-list-maintained
Comment 11 cariaso 2013-03-01 20:28:40 UTC
> That said I think some browsers still preserve order in objects, but as long
> as
> it isn't consistent and according to the JSON-spec we can't use it.

Some browsers explicitly re-order, and consider this a feature
https://code.google.com/p/chromium/issues/detail?id=883#c31

https://code.google.com/p/v8/issues/detail?id=164

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


Navigation
Links