Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map or Array for RESTful design of finite, unordered collection?

Tags:

json

rest

A coworker and I are in a heated debate regarding the design of a REST service. For most of our API, GET calls to collections return something like this:

GET /resource
[
    { "id": 1, ... },
    { "id": 2, ... },
    { "id": 3, ... },
    ...
]

We now must implement a call to a collection of properties whose identifying attribute is "name" (not "id" as in the example above). Furthermore, there is a finite set of properties and the order in which they are sent will never matter. The spec I came up with looks like this:

GET /properties
[
    { "name": "{PROPERTY_NAME}", "value": "{PROPERTY_VALUE}", "description": "{PROPERTY_DESCRIPTION}" },
    { "name": "{PROPERTY_NAME}", "value": "{PROPERTY_VALUE}", "description": "{PROPERTY_DESCRIPTION}" },
    { "name": "{PROPERTY_NAME}", "value": "{PROPERTY_VALUE}", "description": "{PROPERTY_DESCRIPTION}" },
    ...
]

My coworker thinks it should be a map:

GET /properties
{
    "{PROPERTY_NAME}": { "value": "{PROPERTY_VALUE}", "description": "{PROPERTY_DESCRIPTION}" },
    "{PROPERTY_NAME}": { "value": "{PROPERTY_VALUE}", "description": "{PROPERTY_DESCRIPTION}" },
    "{PROPERTY_NAME}": { "value": "{PROPERTY_VALUE}", "description": "{PROPERTY_DESCRIPTION}" },
    ...
}

I cite consistency with the rest of the API as the reason to format the response collection my way, while he cites that this particular collection is finite and the order does not matter. My question is, which design best adheres to RESTful design and why?

like image 420
AndyPerlitch Avatar asked Feb 06 '14 01:02

AndyPerlitch


People also ask

How are the values defined in unordered_map containers?

In the unordered_map containers, the values are not defined in any particular fashion internally. The data types of both the key values and the mapped values can either be predefined or executed at the time, and values are inserted into the container.

What is the proper representation of a collection with no data?

Therefore, when there is no data there (but the collection is valid), the proper representation is an empty array. Show activity on this post. Either Return a 200 (OK) status code, and an empty array in the body.

What is an array map in Java?

An Array is a collection of elements of the same data type. The map is a hashed structure of key and value pairs. The indices of the list are integers starting from 0. The keys of the Map can be of any data type. The elements are accessed via indices.

Why unordered_map is implemented using hash tables?

This is attributed to the fact that the unordered map is implemented using hash tables. C++ unordered_map is a built-in container that is used to store elements in the form of key-value pairs. In the unordered_map containers, the values are not defined in any particular fashion internally.


2 Answers

IIRC how you return the properties of a resource does not matter in a RESTful approach.

http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm

From an API client point of view I would prefer your solution, considering it is explicitly stating that the name of a property is XYZ.

Whereas your coworkers solution would imply it is the name, but how would I know for sure (without reading the API documenation). Try not to assume anything regarding your consuming clients, just because you know what it means (and probably is easy enough to assume to what it means) it might not be so obvious for your clients.

And on top of that, it could break consuming clients if you are ever to decide to revert that value from being a name back to ID. Which in this case you have done already in the past. Now all the clients need to change their code, whereas they would not have to in your solution, unless they need the newly added id (or some other property).

like image 121
Kay Tsar Avatar answered Nov 14 '22 23:11

Kay Tsar


To me the approach would depend on how you need to use the data. Are the property names known before hand by the consuming system, such that having a map lookup could be used to directly access the record you want without needing to iterate over each item? Would there be a method such as...

GET /properties/{PROPERTY_NAME}

If you need to look up properties by name and that sort of method is NOT available, then I would agree with the map approach, otherwise, I would go with the array approach to provide consistent results when querying the resource for a full collection.

like image 40
Mike Brant Avatar answered Nov 14 '22 22:11

Mike Brant