Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Fix attribute name inflection #429

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ Contributors (chronological)
- Areeb Jamal `@iamareebjamal <https://github.com/iamareebjamal>`_
- Suren Khorenyan `@mahenzon <https://github.com/mahenzon>`_
- Karthikeyan Singaravelan `@tirkarthi <https://github.com/tirkarthi>`_
- Jannik Bamberger `@JBamberger <https://github.com/JBamberger>`_
8 changes: 4 additions & 4 deletions marshmallow_jsonapi/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def unwrap_request(self, data, many, **kwargs):

def on_bind_field(self, field_name, field_obj):
"""Schema hook override. When binding fields, set ``data_key`` to the inflected form of field_name."""
if not field_obj.data_key:
if not field_obj.data_key and not field_name == "id":
field_obj.data_key = self.inflect(field_name)
return None

Expand Down Expand Up @@ -321,7 +321,7 @@ def format_error(self, field_name, message, index=None):
# JSONAPI identifier is a special field that exists above the attribute object.
pointer.append("attributes")

pointer.append(self.inflect(field_name))
pointer.append(field_name)

if relationship:
pointer.append("data")
Expand Down Expand Up @@ -363,11 +363,11 @@ def format_item(self, item):
if value:
if "relationships" not in ret:
ret["relationships"] = self.dict_class()
ret["relationships"][self.inflect(field_name)] = value
ret["relationships"][field_name] = value
else:
if "attributes" not in ret:
ret["attributes"] = self.dict_class()
ret["attributes"][self.inflect(field_name)] = value
ret["attributes"][field_name] = value

links = self.get_resource_links(item)
if links:
Expand Down
38 changes: 38 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from tests.test_schema import make_serialized_author, get_error_by_field


def add_postfix_inflection(text):
return text + "_x"


def dasherize(text):
return text.replace("_", "-")

Expand Down Expand Up @@ -33,6 +37,15 @@ class Meta:
strict = True


class PostfixInflectionSchema(Schema):
id = fields.Str()
test_attr = fields.Str(required=True, allow_none=False)

class Meta:
type_ = "postfix_inflection_type"
inflect = add_postfix_inflection


class TestInflection:
@pytest.fixture()
def schema(self):
Expand Down Expand Up @@ -119,6 +132,31 @@ class Meta:
]
assert related_href == f"http://test.test/posts/{post.id}/comments/"

def test_postfix_inflection(self):
schema = PostfixInflectionSchema()
data = schema.dump({"id": "5", "test_attr": None})
print(data)
assert "test_attr_x" in data["data"]["attributes"]

def test_postfix_inflection_errors(self):
schema = PostfixInflectionSchema()
errors = schema.validate(
{
"data": {
"id": "5",
"type": "postfix_inflection_type",
"attributes": {"test_attr_x": None},
}
}
)

assert "errors" in errors
assert len(errors["errors"]) == 1

test_attr = get_error_by_field(errors, "test_attr_x")
assert test_attr
assert test_attr["source"]["pointer"] == "/data/attributes/test_attr_x"


class AuthorAutoSelfLinkSchema(Schema):
id = fields.Int(dump_only=True)
Expand Down