question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with `id` after upgrading from 0.30.1 to 0.31.0

See original GitHub issue

I have this issue after upgrading from 0.30.1 to 0.31.0 or 0.31.2 The reads work, but my update is failing.

curl -X PATCH -d '{ "data":{ "type": "email", "id": "100","attributes":{"status": "S"} } }' -H 'Content-Type: application/vnd.api+json' 'localhost:5000/v1/emails/100' | jq .

Is anyone else having this issue? Or maybe there is a change with the new dependency libraries?

{
  "errors": [
    {
      "detail": "Unknown field.",
      "source": {
        "pointer": "/data/id"
      },
      "status": "422",
      "title": "Validation error"
    }
  ],
  "jsonapi": {
    "version": "1.0"
  }
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:7

github_iconTop GitHub Comments

3reactions
benjaminhallouincommented, Dec 4, 2020

@akira-dev I also face the same issue. It can be reproduced by using your example (in /examples/api.py) and performing the patch call:

PATCH /computers/1 HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": {
    "type": "computer",
    "id": "1",
    "attributes": {
      "serial": "Amstrad 2"
    }
  }
}

It seems that the only way to avoid this is to remove the dump_only=True on the id field from the computer schema, but in that case, we can update the id with a patch call… which we obviously don’t want. 😉

2reactions
Gitigicommented, Mar 24, 2021

I was able to work around this in a much simpler way via the following:

class FooSchema(Schema):
    class Meta:
        type_ = 'foo'
        self_view = 'foo_detail'
        self_view_kwargs = {'id': '<id>'}
        self_view_many = 'foo_list'

    # id = fields.UUID(as_string=True, dump_only=True)
    id = fields.UUID(as_string=True)
    name = fields.Str(required=True)


class FooDetail(ResourceDetail):
    schema = FooSchema
    data_layer = {
        'session': db.session,
        'model': Foo,
    }
    methods = ['GET', 'PATCH']

    def before_patch(self, *args, **kwargs):
        if 'id' in kwargs['data']:
            del kwargs['data']['id']

Similarly in my FooList resource with a before_post method. It isn’t strictly required, because my model simply won’t accept an id during creation, but rather than have an unhandled exception from the data layer, I’m handling it like this:

class FooList(ResourceList):
    schema = FooSchema
    data_layer = {
        'session': db.session,
        'model': Foo,
    }
    methods = ['GET', 'POST']

    def before_post(self, *args, **kwargs):
        if 'id' in kwargs['data']:
            raise BadRequest('')

This is much cleaner/less-hacky than my proposal above. Thankfully flask-rest-jsonapi includes these nice hooks for manipulating the data.

Better fix would be to remove the id using pre_load, which is called before deserializing

from marshmallow import pre_load

class FooSchema(Schema):
    class Meta:
        type_ = 'foo'
        self_view = 'foo_detail'
        self_view_kwargs = {'id': '<id>'}
        self_view_many = 'foo_list'

    @pre_load
    def remove_id_before_deserializing(self, data, **kwargs):
        if 'id' in data:
            del data['id']
        return data

    id = fields.UUID(as_string=True, dump_only=True)
    name = fields.Str(required=True)


class FooDetail(ResourceDetail):
    schema = FooSchema
    data_layer = {
        'session': db.session,
        'model': Foo,
    }
    methods = ['GET', 'PATCH']

class FooList(ResourceList):
    schema = FooSchema
    data_layer = {
        'session': db.session,
        'model': Foo,
    }
    methods = ['GET', 'POST']
Read more comments on GitHub >

github_iconTop Results From Across the Web

[SOLVED] sbopkg: b0rked by bash upgrade in -current?
Good evening all - I recently upgraded my -current box, and now when I try to run sbopkg I get the ... Upgrading...
Read more >
1706064 – Packages left after upgrading to Fedora 30
It's not a bug to have leftover packages after an upgrade. We add things to fedora-obsolete-packages only when the extra packages cause dependency...
Read more >
Release Notes — Numba 0.50.1 documentation - PyData |
If a module is imported from a moved location the shim will issue a deprecation warning and suggest how to update the import...
Read more >
11. Change log — zhmcclient 1.6.0.dev1 documentation
Fixed issues in the zhmcclient_mock support for the “Update LPAR ... Blanked out value of 'x-api-session' field (Session ID) when logging error responses....
Read more >
Release Notes — Documentação Qiskit 0.39.2
Qiskit Metapackage Version qiskit‑terra qiskit‑aer qiskit‑ibmq‑provider Release Date 0.39.2 0.22.2 0.11.1 0.19.2 2022‑11‑03 0.39.1 0.22.1 0.11.1 0.19.2 2022‑11‑02 0.39.0 0.22.0 0.11.0 0.19.2 2022‑10‑13
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found