Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch request validates id match before loading schema #57

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions flask_combo_jsonapi/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,15 @@ def patch(self, *args, **kwargs):
except PluginMethodNotImplementedError:
pass

if "data" not in json_data:
raise BadRequest('Missing "data" node', source={"pointer": "/data"})
if "id" not in json_data["data"]:
raise BadRequest('Missing id in "data" node', source={"pointer": "/data/id"})
if str(json_data["data"]["id"]) != str(kwargs[getattr(self._data_layer, "url_field", "id")]):
raise BadRequest(
"Value of id does not match the resource identifier in url", source={"pointer": "/data/id"}
)

try:
data = schema.load(json_data)
except IncorrectTypeError as e:
Expand All @@ -296,13 +305,6 @@ def patch(self, *args, **kwargs):
message["title"] = "Validation error"
return errors, 422

if "id" not in json_data["data"]:
raise BadRequest('Missing id in "data" node', source={"pointer": "/data/id"})
if str(json_data["data"]["id"]) != str(kwargs[getattr(self._data_layer, "url_field", "id")]):
raise BadRequest(
"Value of id does not match the resource identifier in url", source={"pointer": "/data/id"}
)

self.before_patch(args, kwargs, data=data)

obj = self.update_object(data, qs, kwargs)
Expand Down
7 changes: 4 additions & 3 deletions tests/test_sqlalchemy_data_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,8 @@ def schema_load_mock(*args):
with pytest.raises(Exception):
r.dispatch_request()
rl.post()
rd.patch()
with pytest.raises(Exception):
rd.patch()


def test_compute_schema(person_schema):
Expand Down Expand Up @@ -1620,7 +1621,7 @@ def test_patch_detail_missing_id(client, register_routes, computer, person):
def test_patch_detail_wrong_id(client, register_routes, computer, person):
payload = {
"data": {
"id": "error",
"id": str(person.person_id + 1),
"type": "person",
"attributes": {"name": "test2"},
"relationships": {"computers": {"data": [{"type": "computer", "id": str(computer.id)}]}},
Expand All @@ -1631,7 +1632,7 @@ def test_patch_detail_wrong_id(client, register_routes, computer, person):
response = client.patch(
"/persons/" + str(person.person_id), data=json.dumps(payload), content_type="application/vnd.api+json"
)
assert response.status_code == 422
assert response.status_code == 400


def test_post_relationship_no_data(client, register_routes, computer, person):
Expand Down