Skip to content

Commit

Permalink
Merge pull request #7 from ngseer/allow-missing-attributes
Browse files Browse the repository at this point in the history
Add support for resources without attributes
  • Loading branch information
DeanWay authored Oct 7, 2019
2 parents 135f9c7 + 28a649b commit 49eb4c5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
7 changes: 4 additions & 3 deletions pydantic_jsonapi/response.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from typing import Generic, TypeVar, Optional, List, Any, Type
from typing_extensions import Literal

from pydantic import validator
from pydantic.generics import GenericModel

from pydantic_jsonapi.errors import Error
from pydantic_jsonapi.filter import filter_none
from pydantic_jsonapi.relationships import RelationshipsType
from pydantic_jsonapi.resource_links import ResourceLinks
Expand All @@ -17,9 +15,12 @@ class ResponseDataModel(GenericModel, Generic[TypeT, AttributesT]):
"""
id: str
type: TypeT
attributes: AttributesT
attributes: AttributesT = {}
relationships: Optional[RelationshipsType]

class Config:
validate_all = True


DataT = TypeVar('DataT')
class ResponseModel(GenericModel, Generic[DataT]):
Expand Down
44 changes: 38 additions & 6 deletions tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def test_attributes_as_dict(self):
'data': {'id': '123', 'type': 'item', 'attributes': {}},
'included': [{'id': '456', 'type': 'not-an-item', 'attributes': {}}]
}
my_request_obj = MyResponse(**obj_to_validate)
assert my_request_obj.dict() == {
my_response_object = MyResponse(**obj_to_validate)
assert my_response_object.dict() == {
'data': {
'id': '123',
'type': 'item',
Expand All @@ -27,6 +27,38 @@ def test_attributes_as_dict(self):
}]
}

def test_missing_attributes_dict(self):
MyResponse = JsonApiResponse('item', dict)
obj_to_validate = {
'data': {'id': '123', 'type': 'item'}
}
my_response_object = MyResponse(**obj_to_validate)
assert my_response_object.dict() == {
'data': {
'id': '123',
'type': 'item',
'attributes': {},
}
}

def test_missing_attributes_empty_model(self):
class EmptyModel(BaseModel):
pass

MyResponse = JsonApiResponse('item', EmptyModel)
obj_to_validate = {
'data': {'id': '123', 'type': 'item'}
}
my_response_object = MyResponse(**obj_to_validate)
assert my_response_object.dict() == {
'data': {
'id': '123',
'type': 'item',
'attributes': {},
}
}
assert isinstance(my_response_object.data.attributes, EmptyModel)

def test_attributes_as_item_model(self):
ItemResponse = JsonApiResponse('item', ItemModel)
obj_to_validate = {
Expand All @@ -47,8 +79,8 @@ def test_attributes_as_item_model(self):
},
}
}
my_request_obj = ItemResponse(**obj_to_validate)
assert my_request_obj.dict() == {
my_response_obj = ItemResponse(**obj_to_validate)
assert my_response_obj.dict() == {
'data': {
'id': '123',
'type': 'item',
Expand Down Expand Up @@ -91,8 +123,8 @@ def test_list_item_model(self):
},
],
}
my_request_obj = ItemResponse(**obj_to_validate)
assert my_request_obj.dict() == {
my_response_obj = ItemResponse(**obj_to_validate)
assert my_response_obj.dict() == {
'data': [
{
'id': '123',
Expand Down

0 comments on commit 49eb4c5

Please sign in to comment.