Fixed 2.1 REST API returns an object rather than an array

Jake B.

Well-known member
Affected version
2.1.0 Betas
The JSON response in all of the endpoints (at least the ones I've noticed so far) are returning objects for a list of multiple items, this results in any sort of order/sorting being broken when parsed by some languages (such as JavaScript) as, by definition:

An object is an unordered set of name/value pairs.

Compared to:

An array is an ordered collection of values.


So if you parse the JSON string into an object using JSON.parse it will result in the items being ordered by the key rather than the intended order the API returned them in. Changing this behavior could potentially be a breaking change if anyone is consuming the API currently and assuming that the key of the item is equal to its ID, but not changing it will result in sorting being broken on many languages that do not keep the order of objects which is likely going to be a much larger issue in the long term.
 
I have roughly been aware of this and been considering a change, but short of the issue DBT mentioned, I've never actually seen an implementation not maintain an order of a map (object), though I guess they're within their rights to change it. Easy to take PHP's array data type for granted, though there are definitely situations where people have complained about it.
 
Thank you for reporting this issue. The issue is now resolved and we are aiming to include that in a future XF release (2.1.0 Beta 5).

Change log:
Adjust many parts of the API to explicitly return JSON arrays rather than ID-keyed objects to ensure consistent ordering regardless of language used to access the API. Adjust other places to consistently use an object, even when we would return an empty array internally. For predictability, entities will now list their parameters alphabetically.
Any changes made as a result of this issue being resolved may not be rolled out here until later.
 
Back
Top Bottom