-
Notifications
You must be signed in to change notification settings - Fork 68
deserialization of responses from flask-rest-jsonapi not working #294
Comments
From memory, flask-rest-jsonapi adds some metadata, like the jsonapi version, which will cause you to have trouble if you load them via marshmallow-jsonapi. My hack to solve this for my API is simply deleting the metadata and jsonapi keys: https://github.com/ewels/MegaQC/blob/130cafdb1f74b042385d0645a7acf11e8129f8d7/tests/api/test_api.py#L202-L204 |
The error is actually a validation error of my "attributes". Note, I have replaced the actual names of my fields with field_name_1, etc below. JSON response after deleting ret['meta'] and ret['jsonapi'] marshmallow.exceptions.ValidationError: {0: {'field_name_1': ['Unknown field.'], 'field_name_2': ['Unknown field.'], 'field_name_3': ['Unknown field.'], 'field_name_4': ['Unknown field.'], 'field_name_5': ['Unknown field.'], 'field_name_6': ['Unknown field.']}} Client:
Schema:
field_name_3 = fields.String(dump_only=True) |
I think I see the issue. I have fields set as dump_only so that they are really read-only fields. However, I want to share the same schema between my client and server code.
The client only wants read-only access to those fields. |
Replacing dump_only=True with the following 3 parameters gets close to what I want. I believe now the real solution is define two schemas, one for server, one for client, that are identical, except for the field parameters. |
I currently use flask-rest-jsonapi (https://flask-rest-jsonapi.readthedocs.io/en/latest/) to implement an API. I have defined my schemas and relationshipsusing marshmallow_jsonapi.flask, and the schemas live in another project.
I attempted to create a client which would import the schemas, and utilize the requests library to make http requests to my backend.
MySchema().loads(response.json()) gives back "unknown fields" for things that are not listed in the "attributes" of the jsonapi response. The json response from flask-rest-jsonapi looks correct, and response.json() turns that json into a dict (which is passed to loads)
Am I do something something wrong here?
The text was updated successfully, but these errors were encountered: