Skip to content

Commit dfe00fb

Browse files
Merge pull request AdCombo#51 from samuelfirst/fix_raise_404_ObjectNotFound
fix AdCombo#35 Raise 404 ObjectNotFound when a single resource does not exist
2 parents 825842e + e921239 commit dfe00fb

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

flask_combo_jsonapi/resource.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
from flask_combo_jsonapi.querystring import QueryStringManager as QSManager
1414
from flask_combo_jsonapi.pagination import add_pagination_links
15-
from flask_combo_jsonapi.exceptions import InvalidType, BadRequest, RelationNotFound, PluginMethodNotImplementedError
15+
from flask_combo_jsonapi.exceptions import InvalidType, BadRequest, RelationNotFound, PluginMethodNotImplementedError,\
16+
ObjectNotFound
1617
from flask_combo_jsonapi.decorators import check_headers, check_method_requirements, jsonapi_exception_formatter
1718
from flask_combo_jsonapi.schema import compute_schema, get_relationships, get_model_field
1819
from flask_combo_jsonapi.data_layers.base import BaseDataLayer
@@ -234,6 +235,11 @@ def get(self, *args, **kwargs):
234235

235236
obj = self.get_object(kwargs, qs)
236237

238+
if obj is None:
239+
url_field = getattr(self._data_layer, "url_field", "id")
240+
value = f" '{kwargs.get(url_field)}'" if kwargs.get(url_field) else ""
241+
raise ObjectNotFound(f"{self.data_layer['model'].__name__}{value} not found.")
242+
237243
self.before_marshmallow(args, kwargs)
238244

239245
schema = compute_schema(self.schema, getattr(self, "get_schema_kwargs", dict()), qs, qs.include)

tests/test_sqlalchemy_data_layer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,7 @@ def test_get_list_invalid_sort(client, register_routes):
12171217
def test_get_detail_object_not_found(client, register_routes):
12181218
with client:
12191219
response = client.get("/persons/3", content_type="application/vnd.api+json")
1220-
assert response.status_code == 200
1220+
assert response.status_code == 404
12211221

12221222

12231223
def test_post_relationship_related_object_not_found(client, register_routes, person):

0 commit comments

Comments
 (0)