mrevutskyi / flask-restless-ng

A Flask extension for creating simple ReSTful JSON APIs from SQLAlchemy models.
https://flask-restless-ng.readthedocs.io
Other
64 stars 11 forks source link

Help me to migrate some projects: issue with APIManager.create_api() #9

Closed cedricbonhomme closed 3 years ago

cedricbonhomme commented 3 years ago

I am migrating a Flask app I am working on since some years.

I defined a blueprint as explained in the documentation: https://github.com/CASES-LU/MOSP/blob/flask-restless-ng/mosp/web/views/api/v1/jsonobject.py#L41 (three in reality)

then I register the blueprints: https://github.com/CASES-LU/MOSP/blob/flask-restless-ng/runserver.py#L36-L38

When I query an endpoint I always get:

  File "/home/cedric/.cache/pypoetry/virtualenvs/mosp-ToRNPEts-py3.9/lib/python3.9/site-packages/flask_restless/helpers.py", line 441, in __call__
    raise ValueError(message)
ValueError: Model <class 'mosp.models.user.User'> is not known to any APIManager objects; maybe you have not called APIManager.create_api() for this model.

And if I do this: https://github.com/CASES-LU/MOSP/blob/flask-restless-ng/mosp/web/views/api/v1/jsonobject.py#L59

I will get the same error message but for a kind of object not exposed (directly) by the API. It's on a relationship of the object supposed to be exposed by the API.

It would be nice if you have an idea. Let me know if you need more information.

I have some few projects to migrate, like https://open-source-security-software.net/ which is relying a lot on Flask-Restless.

By the way, with the latest versions of Flask and Flask-SQLAlchemy (https://github.com/CASES-LU/MOSP/blob/master/poetry.lock) I still have no issues with Flask-Restless.

cedricbonhomme commented 3 years ago

So actually the migration consist of this.

I removed _max_results_perpage, _results_perpage and _excludecolumns. :-( I will find replacement for these features later.

mrevutskyi commented 3 years ago

Hi,

when you register a model in the API manager it creates a serializer for it, which is later used for both serializing full objects and relationships. So you do need to register all models that you want to expose directly or via relationships.

Also, from JSON API standpoint it is expected to be able to fetch a resource listed in relationships.

If you do not want to expose some resources at all, you should add that relationship field into exclude parameter when registering the parent model. That way it will not appear in relationships

cedricbonhomme commented 3 years ago

thanks, I'll test this !