Repository: django-json-api/django-rest-framework-json-api

Issue:
id column integer primary key of 0 gets returned as null
If a model has an id column that's an integer primary key
```python
class Person(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=200)
```
and a fixture is loaded where the id has value 0
```json
[
    {"model": "project.person", "fields": {"id": 0, "name": "Example"}},
    {"model": "project.person","fields": {"id": 1, "name": "Someone"}}
]
```
then the JSON response returned from the API replaces the id of 0 with null
```json
    "data": [
        {
            "type": "person",
            "id": null,
            "attributes": {
                "name": "Example"
            }
        },
        {
            "type": "person",
            "id": "1",
            "attributes": {
                "name": "Someone"
            }
        }
    ],
```

Discussion:
humayunah: @sliverc please review this and let me know if anything needs to be changed/added.

Patch:
diff --git a/AUTHORS b/AUTHORS
index 7f4a823..d421d5e 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -15,6 +15,7 @@ David Guillot, for Contexte <dguillot@contexte.com>
 David Vogt <david.vogt@adfinis-sygroup.ch>
 Felix Viernickel <felix@gedankenspieler.org>
 Greg Aker <greg@gregaker.net>
+Humayun Ahmad <humayunahbh@gmail.com>
 Jamie Bliss <astronouth7303@gmail.com>
 Jason Housley <housleyjk@gmail.com>
 Jeppe Fihl-Pearson <jeppe@tenzer.dk>
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8c3c772..4fb30d1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 Note that in line with [Django REST framework policy](https://www.django-rest-framework.org/topics/release-notes/),
 any parts of the framework not mentioned in the documentation should generally be considered private API, and may be subject to change.
 
+## [Unreleased]
+
+### Fixed
+
+* Handled zero as a valid ID for resource (regression since 6.1.0)
+
 ## [7.0.2] - 2024-06-28
 
 ### Fixed
diff --git a/rest_framework_json_api/utils.py b/rest_framework_json_api/utils.py
index e12080a..805f5f0 100644
--- a/rest_framework_json_api/utils.py
+++ b/rest_framework_json_api/utils.py
@@ -307,13 +307,11 @@ def get_resource_type_from_serializer(serializer):
 def get_resource_id(resource_instance, resource):
     """Returns the resource identifier for a given instance (`id` takes priority over `pk`)."""
     if resource and "id" in resource:
-        return resource["id"] and encoding.force_str(resource["id"]) or None
+        _id = resource["id"]
+        return encoding.force_str(_id) if _id is not None else None
     if resource_instance:
-        return (
-            hasattr(resource_instance, "pk")
-            and encoding.force_str(resource_instance.pk)
-            or None
-        )
+        pk = getattr(resource_instance, "pk", None)
+        return encoding.force_str(pk) if pk is not None else None
     return None
 
 


Test patch:
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 4e103ae..a3beb12 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -403,6 +403,13 @@ def test_get_resource_type_from_serializer_without_resource_name_raises_error():
         (None, {"id": 11}, "11"),
         (object(), {"pk": 11}, None),
         (BasicModel(id=6), {"id": 11}, "11"),
+        (BasicModel(id=0), None, "0"),
+        (None, {"id": 0}, "0"),
+        (
+            BasicModel(id=0),
+            {"id": 0},
+            "0",
+        ),
     ],
 )
 def test_get_resource_id(resource_instance, resource, expected):