Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3637

Rest API best practice when dealing with duplicate resources

$
0
0

Let's consider an API for some kind of tchat application and that can provide 2 kinds of resources : Room and User.

A Room has 2 properties : users and admins. Both contain some instances of User.A same User can be stored in both properties, users and admins.

A basic get request of a Room will so provide something like that :

{  id: 1,  users: [{    id:1,    name:bob}],  admins:[{    id:1,    name:bob}]}

It seems odd to me to have twice the same data (User Bob). The client will so have two objects for the same data (which can generate some issues). For instance, if he checks if a User is listed in the property admins to define if he is an admin, he might not have the result expected if another instance, based on the same User, is listed instead.

So in my opinion, I have a few solutions :

    1. Do not return sub-resources when a Room is queried. In that case I will not have duplicates of User while I do not send any User. I then provide access to 2 sub-queries to let the client get the content of the properties users and admins. At this point, it's their responsibility to avoid duplicates.
    1. I return the data as presented above (with duplicates). Once again, I let the user handling duplicates.
    1. I send the User once and each other copies are replaced by an alias (probably the id). So the result will be:

    {id: 1,users: [{id:1,name:bob}],admins:[1]}

That solution works well if I know that all Users in the property admins are also listed in the property users. In a case where some admins are not actually users, the property admins will contain a mix of both : some user-id and some user-instance. The client will so have an extra step of work to handle that property.

    1. Replace all Users by its id and let the client require all those Users from ids with other requests.

I'm a beginner in Rest api, so I'm sorry if it's a dumb question...


Viewing all articles
Browse latest Browse all 3637

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>