igorbenav / FastAPI-boilerplate

An extendable async API using FastAPI, Pydantic V2, SQLAlchemy 2.0, PostgreSQL and Redis.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Don't return arrays as top level responses

wlwwt opened this issue · comments

The top level response from an endpoint should always be an object, never an array.
The problem is that it's very hard to make backwards compatible changes when you return arrays. Objects let you make additive changes.

# GOOD
GET /api/v1/users returns:
{ "data": [{ ...user1...}, { ...user2...}] }

# BAD
GET /api/v1/users returns:
[{ ...user1...}, { ...user2...}]

The obvious common evolution in this specific example will be to add pagination. You can always add totalCount or hasMore fields and old clients will continue to work. If your endpoint returns a top-level array, you will need a whole new endpoint.

Hey, @wlwwt, good catch! Thanks for the issue.

@wlwwt closed, I also added pagination to the endpoints. Thanks!