Is it bad practice to return a list of objects in a microservice REST api call?

Viewed 1357

I am following swagger-driven api development. While skimming codebases, i observe that services return a parent object whenever there is an expectation to return a list of objects.

The reason being given is 'it is bad practice to return list of objects in api'. Most likely this popular tutorial is the source of inspiration :- https://www.youtube.com/watch?v=Dmvhe2cw-_k It says 'the api could break if the service returns an object instead of a list'

However, i am not convinced with this one-size-fits-all dictum, since every api contract is documented in swagger and all developers have to strictly follow the document to develop their services and service calls.

I still prefer returning a list of objects over returning a wrapper object. What do the experts suggest ?

Links to any documentation, developer guides for best practices are most appreciated.

1 Answers

If it is a 'collection' resource such as:

/employees

Then as a consumer, I would expect a list of items + any pagination details (It would also be really helpful to receive some aggregate information on the total number of employees matching the criteria (if you provide some query params) etc)

This guide should help you understand more about the different types of resources https://restfulapi.net/resource-naming/

Related