diff --git a/CHANGELOG.md b/CHANGELOG.md index 73a9a738..46dc3419 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ any parts of the framework not mentioned in the documentation should generally b * Fixed invalid relationship pointer in error objects when field naming formatting is used. * Properly resolved related resource type when nested source field is defined. * Prevented overwriting of pointer in custom error object +* Adhered to field naming format setting when generating schema parameters and required fields. ### Added diff --git a/example/migrations/0012_author_full_name.py b/example/migrations/0012_author_full_name.py new file mode 100644 index 00000000..1f67558e --- /dev/null +++ b/example/migrations/0012_author_full_name.py @@ -0,0 +1,19 @@ +# Generated by Django 4.1 on 2022-09-06 15:42 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("example", "0011_rename_type_author_author_type_and_more"), + ] + + operations = [ + migrations.AddField( + model_name="author", + name="full_name", + field=models.CharField(default="", max_length=50), + preserve_default=False, + ), + ] diff --git a/example/models.py b/example/models.py index c3850076..8fc86c22 100644 --- a/example/models.py +++ b/example/models.py @@ -53,6 +53,7 @@ class Meta: class Author(BaseModel): name = models.CharField(max_length=50) + full_name = models.CharField(max_length=50) email = models.EmailField() author_type = models.ForeignKey(AuthorType, null=True, on_delete=models.CASCADE) diff --git a/example/serializers.py b/example/serializers.py index b9bf71d6..75a1de11 100644 --- a/example/serializers.py +++ b/example/serializers.py @@ -268,6 +268,7 @@ class Meta: model = Author fields = ( "name", + "full_name", "email", "bio", "entries", diff --git a/example/tests/__snapshots__/test_openapi.ambr b/example/tests/__snapshots__/test_openapi.ambr index fb8c2abe..a0231471 100644 --- a/example/tests/__snapshots__/test_openapi.ambr +++ b/example/tests/__snapshots__/test_openapi.ambr @@ -104,6 +104,10 @@ "maxLength": 254, "type": "string" }, + "fullName": { + "maxLength": 50, + "type": "string" + }, "name": { "maxLength": 50, "type": "string" @@ -280,6 +284,24 @@ "type": "string" } }, + { + "description": "author_type", + "in": "query", + "name": "filter[authorType]", + "required": false, + "schema": { + "type": "string" + } + }, + { + "description": "name", + "in": "query", + "name": "filter[name]", + "required": false, + "schema": { + "type": "string" + } + }, { "description": "A search term.", "in": "query", @@ -399,6 +421,24 @@ "type": "string" } }, + { + "description": "author_type", + "in": "query", + "name": "filter[authorType]", + "required": false, + "schema": { + "type": "string" + } + }, + { + "description": "name", + "in": "query", + "name": "filter[name]", + "required": false, + "schema": { + "type": "string" + } + }, { "description": "A search term.", "in": "query", @@ -508,6 +548,10 @@ "maxLength": 254, "type": "string" }, + "fullName": { + "maxLength": 50, + "type": "string" + }, "name": { "maxLength": 50, "type": "string" @@ -515,6 +559,7 @@ }, "required": [ "name", + "fullName", "email" ], "type": "object" diff --git a/example/tests/test_format_keys.py b/example/tests/test_format_keys.py index b91cf595..d0e36d81 100644 --- a/example/tests/test_format_keys.py +++ b/example/tests/test_format_keys.py @@ -55,6 +55,7 @@ def test_options_format_field_names(db, client): data = response.json()["data"] expected_keys = { "name", + "fullName", "email", "bio", "entries", diff --git a/example/views.py b/example/views.py index 27ec6c2e..edb49ba8 100644 --- a/example/views.py +++ b/example/views.py @@ -222,6 +222,7 @@ class NoFiltersetEntryViewSet(EntryViewSet): class AuthorViewSet(ModelViewSet): queryset = Author.objects.all() + filterset_fields = ("author_type", "name") def get_serializer_class(self): serializer_classes = { diff --git a/rest_framework_json_api/django_filters/backends.py b/rest_framework_json_api/django_filters/backends.py index 5302bf09..365831c7 100644 --- a/rest_framework_json_api/django_filters/backends.py +++ b/rest_framework_json_api/django_filters/backends.py @@ -4,7 +4,7 @@ from rest_framework.exceptions import ValidationError from rest_framework.settings import api_settings -from rest_framework_json_api.utils import undo_format_field_name +from rest_framework_json_api.utils import format_field_name, undo_format_field_name class DjangoFilterBackend(DjangoFilterBackend): @@ -139,5 +139,6 @@ def get_schema_operation_parameters(self, view): result = super().get_schema_operation_parameters(view) for res in result: if "name" in res: - res["name"] = "filter[{}]".format(res["name"]).replace("__", ".") + name = format_field_name(res["name"].replace("__", ".")) + res["name"] = "filter[{}]".format(name) return result diff --git a/rest_framework_json_api/schemas/openapi.py b/rest_framework_json_api/schemas/openapi.py index c3d553b7..1aa690fa 100644 --- a/rest_framework_json_api/schemas/openapi.py +++ b/rest_framework_json_api/schemas/openapi.py @@ -667,7 +667,7 @@ def map_serializer(self, serializer): continue if field.required: - required.append(field.field_name) + required.append(format_field_name(field.field_name)) schema = self.map_field(field) if field.read_only:
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: