Skip to content

Commit

Permalink
Format fields in OpenAPI Schema (#1003)
Browse files Browse the repository at this point in the history
  • Loading branch information
sha016 authored Nov 11, 2021
1 parent f32e823 commit d025163
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Safa AlFulaij <safa1996alfulaij@gmail.com>
santiavenda <santiavenda2@gmail.com>
Sergey Kolomenkin <https://kolomenkin.com>
Stas S. <stas@nerd.ro>
Steven A. <sha0160@protonmail.com>
Swaraj Baral <baralswaraj40@gmail.com>
Tim Selman <timcbaoth@gmail.com>
Tom Glowka <glowka.tom@gmail.com>
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ any parts of the framework not mentioned in the documentation should generally b
* Avoid error when `parser_context` is `None` while parsing.
* Raise comprehensible error when reserved field names `meta` and `results` are used.
* Use `relationships` in the error object `pointer` when the field is actually a relationship.
* Added missing inflection to the generated OpenAPI schema.

### Changed

Expand Down
4 changes: 2 additions & 2 deletions example/tests/__snapshots__/test_openapi.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
"entries": {
"$ref": "#/components/schemas/reltomany"
},
"first_entry": {
"firstEntry": {
"$ref": "#/components/schemas/reltoone"
},
"type": {
Expand Down Expand Up @@ -541,7 +541,7 @@
"entries": {
"$ref": "#/components/schemas/reltomany"
},
"first_entry": {
"firstEntry": {
"$ref": "#/components/schemas/reltoone"
},
"type": {
Expand Down
7 changes: 4 additions & 3 deletions rest_framework_json_api/schemas/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from rest_framework.schemas.utils import is_list_view

from rest_framework_json_api import serializers, views
from rest_framework_json_api.utils import format_field_name


class SchemaGenerator(drf_openapi.SchemaGenerator):
Expand Down Expand Up @@ -655,12 +656,12 @@ def map_serializer(self, serializer):
if isinstance(field, serializers.HiddenField):
continue
if isinstance(field, serializers.RelatedField):
relationships[field.field_name] = {
relationships[format_field_name(field.field_name)] = {
"$ref": "#/components/schemas/reltoone"
}
continue
if isinstance(field, serializers.ManyRelatedField):
relationships[field.field_name] = {
relationships[format_field_name(field.field_name)] = {
"$ref": "#/components/schemas/reltomany"
}
continue
Expand All @@ -682,7 +683,7 @@ def map_serializer(self, serializer):
schema["description"] = str(field.help_text)
self.map_field_validators(field, schema)

attributes[field.field_name] = schema
attributes[format_field_name(field.field_name)] = schema

result = {
"type": "object",
Expand Down

0 comments on commit d025163

Please sign in to comment.