Skip to content

Commit 897305c

Browse files
jokiefersliverc
andauthored
Adhered to field naming format setting when generating schema (#1048)
Co-authored-by: Oliver Sauder <[email protected]>
1 parent c016b8b commit 897305c

File tree

9 files changed

+73
-3
lines changed

9 files changed

+73
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ any parts of the framework not mentioned in the documentation should generally b
1515
* Fixed invalid relationship pointer in error objects when field naming formatting is used.
1616
* Properly resolved related resource type when nested source field is defined.
1717
* Prevented overwriting of pointer in custom error object
18+
* Adhered to field naming format setting when generating schema parameters and required fields.
1819

1920
### Added
2021

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Generated by Django 4.1 on 2022-09-06 15:42
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("example", "0011_rename_type_author_author_type_and_more"),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name="author",
15+
name="full_name",
16+
field=models.CharField(default="", max_length=50),
17+
preserve_default=False,
18+
),
19+
]

example/models.py

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class Meta:
5353

5454
class Author(BaseModel):
5555
name = models.CharField(max_length=50)
56+
full_name = models.CharField(max_length=50)
5657
email = models.EmailField()
5758
author_type = models.ForeignKey(AuthorType, null=True, on_delete=models.CASCADE)
5859

example/serializers.py

+1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ class Meta:
268268
model = Author
269269
fields = (
270270
"name",
271+
"full_name",
271272
"email",
272273
"bio",
273274
"entries",

example/tests/__snapshots__/test_openapi.ambr

+45
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@
104104
"maxLength": 254,
105105
"type": "string"
106106
},
107+
"fullName": {
108+
"maxLength": 50,
109+
"type": "string"
110+
},
107111
"name": {
108112
"maxLength": 50,
109113
"type": "string"
@@ -280,6 +284,24 @@
280284
"type": "string"
281285
}
282286
},
287+
{
288+
"description": "author_type",
289+
"in": "query",
290+
"name": "filter[authorType]",
291+
"required": false,
292+
"schema": {
293+
"type": "string"
294+
}
295+
},
296+
{
297+
"description": "name",
298+
"in": "query",
299+
"name": "filter[name]",
300+
"required": false,
301+
"schema": {
302+
"type": "string"
303+
}
304+
},
283305
{
284306
"description": "A search term.",
285307
"in": "query",
@@ -399,6 +421,24 @@
399421
"type": "string"
400422
}
401423
},
424+
{
425+
"description": "author_type",
426+
"in": "query",
427+
"name": "filter[authorType]",
428+
"required": false,
429+
"schema": {
430+
"type": "string"
431+
}
432+
},
433+
{
434+
"description": "name",
435+
"in": "query",
436+
"name": "filter[name]",
437+
"required": false,
438+
"schema": {
439+
"type": "string"
440+
}
441+
},
402442
{
403443
"description": "A search term.",
404444
"in": "query",
@@ -508,13 +548,18 @@
508548
"maxLength": 254,
509549
"type": "string"
510550
},
551+
"fullName": {
552+
"maxLength": 50,
553+
"type": "string"
554+
},
511555
"name": {
512556
"maxLength": 50,
513557
"type": "string"
514558
}
515559
},
516560
"required": [
517561
"name",
562+
"fullName",
518563
"email"
519564
],
520565
"type": "object"

example/tests/test_format_keys.py

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def test_options_format_field_names(db, client):
5555
data = response.json()["data"]
5656
expected_keys = {
5757
"name",
58+
"fullName",
5859
"email",
5960
"bio",
6061
"entries",

example/views.py

+1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ class NoFiltersetEntryViewSet(EntryViewSet):
222222

223223
class AuthorViewSet(ModelViewSet):
224224
queryset = Author.objects.all()
225+
filterset_fields = ("author_type", "name")
225226

226227
def get_serializer_class(self):
227228
serializer_classes = {

rest_framework_json_api/django_filters/backends.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from rest_framework.exceptions import ValidationError
55
from rest_framework.settings import api_settings
66

7-
from rest_framework_json_api.utils import undo_format_field_name
7+
from rest_framework_json_api.utils import format_field_name, undo_format_field_name
88

99

1010
class DjangoFilterBackend(DjangoFilterBackend):
@@ -139,5 +139,6 @@ def get_schema_operation_parameters(self, view):
139139
result = super().get_schema_operation_parameters(view)
140140
for res in result:
141141
if "name" in res:
142-
res["name"] = "filter[{}]".format(res["name"]).replace("__", ".")
142+
name = format_field_name(res["name"].replace("__", "."))
143+
res["name"] = "filter[{}]".format(name)
143144
return result

rest_framework_json_api/schemas/openapi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ def map_serializer(self, serializer):
667667
continue
668668

669669
if field.required:
670-
required.append(field.field_name)
670+
required.append(format_field_name(field.field_name))
671671

672672
schema = self.map_field(field)
673673
if field.read_only:

0 commit comments

Comments
 (0)