diff --git a/AUTHORS.rst b/AUTHORS.rst index b32babb..d8e5f1e 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -33,3 +33,4 @@ Contributors (chronological) - Areeb Jamal `@iamareebjamal `_ - Suren Khorenyan `@mahenzon `_ - Karthikeyan Singaravelan `@tirkarthi `_ +- Jannik Bamberger `@JBamberger `_ diff --git a/marshmallow_jsonapi/schema.py b/marshmallow_jsonapi/schema.py index ee9c2cf..6fb0776 100644 --- a/marshmallow_jsonapi/schema.py +++ b/marshmallow_jsonapi/schema.py @@ -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 @@ -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") @@ -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: diff --git a/tests/test_options.py b/tests/test_options.py index 6363236..1dfa027 100644 --- a/tests/test_options.py +++ b/tests/test_options.py @@ -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("_", "-") @@ -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): @@ -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)