Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that ModelSerializer fields are in same order than in DRF #1182

Merged
merged 4 commits into from
Oct 12, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Adam Wróbel <https://adamwrobel.com>
Adam Ziolkowski <[email protected]>
Alan Crosswell <[email protected]>
Alex Seidmann <[email protected]>
Antoine Auger <https://antoineauger.fr/en>
Anton Shutik <[email protected]>
Arttu Perälä <[email protected]>
Ashley Loewen <[email protected]>
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ any parts of the framework not mentioned in the documentation should generally b
### Fixed

* Fixed OpenAPI schema generation for `Serializer` when used inside another `Serializer` or as a child of `ListField`.
* `ModelSerializer` fields are now returned in the same order than DRF

### Removed

Expand Down
10 changes: 5 additions & 5 deletions rest_framework_json_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,11 @@ def get_field_names(self, declared_fields, info):
"""
meta_fields = getattr(self.Meta, "meta_fields", [])

declared = {}
for field_name in set(declared_fields.keys()):
field = declared_fields[field_name]
if field_name not in meta_fields:
declared[field_name] = field
declared = {
field_name: field
for field_name, field in declared_fields.items()
if field_name not in meta_fields
}
fields = super().get_field_names(declared, info)
return list(fields) + list(getattr(self.Meta, "meta_fields", list()))

Expand Down
34 changes: 34 additions & 0 deletions tests/test_serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from django.db import models
from rest_framework.utils import model_meta

from rest_framework_json_api import serializers
from tests.models import DJAModel, ManyToManyTarget
Expand Down Expand Up @@ -50,3 +51,36 @@ class ReservedFieldNamesSerializer(serializers.Serializer):
"ReservedFieldNamesSerializer uses following reserved field name(s) which is "
"not allowed: meta, results"
)


def test_get_field_names():
class MyTestModel(DJAModel):
verified = models.BooleanField(default=False)
uuid = models.UUIDField()

class AnotherSerializer(serializers.Serializer):
ref_id = serializers.CharField()
reference_string = serializers.CharField()

class MyTestModelSerializer(AnotherSerializer, serializers.ModelSerializer):
an_extra_field = serializers.CharField()

class Meta:
model = MyTestModel
fields = "__all__"
extra_kwargs = {
"verified": {"read_only": True},
}

# Same logic than in DRF get_fields() method
declared_fields = MyTestModelSerializer._declared_fields
info = model_meta.get_field_info(MyTestModel)

assert MyTestModelSerializer().get_field_names(declared_fields, info) == [
"id",
"ref_id",
"reference_string",
"an_extra_field",
"verified",
"uuid",
]