Fastapi: [QUESTION] is it possible to implement a JSONAPI spec compliant API with FastAPI?

Created on 22 May 2019  路  6Comments  路  Source: tiangolo/fastapi

Description

Is it possible to implement a JSONAPI spec compliant API with FastAPI?

Additional context
See https://jsonapi.org/

This mostly comes down to the schema used for response data, http status code usage and the way filters, pagination and side-loading are implemented.

I'm considering to try and invest the effort to support this, but I'd like to hear a maintainer's thoughts on it before I dive in and commit.

question

All 6 comments

Yes, of course, you can.

JSON:API defines mainly the payload contents sent back and forth. And imposes strict rules on what you can receive and send in your API. You can perfectly follow those rules and receive and send data in the formats required by that spec.

It also imposes rules on including data related to other endpoints, etc. So, it's more or less tightly coupled with relational entities.

FastAPI can generate automatically OpenAPI specs from your code, more or less, effortlessly.

It cannot generate automatically JSON:API endpoints for you, in part because that depends heavily on the kind of database that you use and how you handle data relationships.

But you can use FastAPI to implement a JSON:API compliant API (that also has OpenAPI for free).

I guess that answers your question, so I'll close this issue now. But feel free to add more comments or create new issues.

I've looked at the docs and I can see how most features would be implemented. One specific question is query parameters with dict type, for example fields in the following maps string keys to lists of strings:

GET /articles?include=author&fields[articles]=title,body,author&fields[people]=name HTTP/1.1

Is that supported as a query string type?

Hmm, I see, there's currently no integrated logic for parsing that style of parameters. You would have to read the parameters from the request as in https://fastapi.tiangolo.com/tutorial/using-request-directly/ and parse them in your code.

The current query parsing is done by Starlette, which in turn uses Python's urllib.parse.parse_qsl: https://docs.python.org/3.7/library/urllib.parse.html#urllib.parse.parse_qsl

This might be a bit late, but I really recommend taking a look at pyramid_jsonapi approach. It would be possible to create something like that.

https://github.com/colinhiggs/pyramid-jsonapi/blob/bd57d8dcca5ccc69efbfd3669a45a0f6158d2aa1/pyramid_jsonapi/__init__.py#L267

It is an awesome implementation, you only pass the models and that's it, you have a full JSONAPI automatically generated with filtering, sorting, limiting, paging, etc.

I am checking the code and it doesn't seem to be so tightly coupled to pyramid, but it is coupled to sqlalchemy.

I would be happy to help if other people thinks it makes sense.

EDIT:
Just found an implementation of JSON:api using pydantic for validation
https://github.com/DeanWay/pydantic-jsonapi

has anybody done any work on a JSONAPI implementation using FastAPI?

Thanks!

I'm also a bit late to the party, but I stumbled upon this python library that handles serialization/deserialization between a "plain" JSON object and a JSON:API document. I'm bad at explaining, so you can just look at the examples on the repo :')
https://github.com/noplay/json-api-doc

It looks promising though, the source code is pretty short and sweet. I found this repo listed on the JSON:API website:
https://jsonapi.org/implementations/#client-libraries-python

Just thought I'd share some stuff I found in my research :)

Was this page helpful?
0 / 5 - 0 ratings