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

Issue:
Inflect component attributes when using the OpenAPI SchemaGenerator
### Discussed in https://github.com/django-json-api/django-rest-framework-json-api/discussions/966

<div type='discussions-op-text'>

<sup>Originally posted by **mattis-rosenkranz** July 29, 2021</sup>
The generated OpenaAPI-Scheme component attributes are not inflected using the settings described in the docs.
This is not a problem as the parser just leaves the attribute names as they are, but it would be nice to conform the OpenAPI-scheme to the chosen field format.</div>

Most likely this will be fixed in [map_serialzer](https://github.com/django-json-api/django-rest-framework-json-api/blob/master/rest_framework_json_api/schemas/openapi.py#L641) function.


Discussion:
Hey @sliverc, I would like to work on this. Can you guide me a bit so that I can get started ?
When `JSON_API_FORMAT_FIELD_NAMES` is configured openapi schema still shows attribute and relatioship field names underscored instead of formatted.

When looking quickly at the code it seems that the field name [here](https://github.com/django-json-api/django-rest-framework-json-api/blob/master/rest_framework_json_api/schemas/openapi.py#L685), [here](https://github.com/django-json-api/django-rest-framework-json-api/blob/master/rest_framework_json_api/schemas/openapi.py#L658) and [here](https://github.com/django-json-api/django-rest-framework-json-api/blob/master/rest_framework_json_api/schemas/openapi.py#L663) needs to be formatted with `utils.format_field_name` to make this work.

@SwarajBaral 
Does this help you to get started?
@sliverc If all that's needed is formatting field.field_name on those lines I'd love to start contributing with that.
@sha016 Yes as far as I see is formatting field name enough + adjusting the tests. Feel free to give it a try.
@sliverc I've made those changes and ran tests. The tests fail because the changes that are applied don't match to the last snapshot if I'm reading this right:
```

E       assert '{\n  "descri...hors"\n  ]\n}' == '{\n  "descri...hors"\n  ]\n}'
E         Skipping 2040 identical leading characters in diff, use -v to show
E         -     "first_entry": {
E         ?           ^^
E         +     "firstEntry": {
E         ?           ^
E                                 "$ref": "#/components/schemas/reltoone"
E                               },...
E         
E         ...Full output truncated (110 lines hidden), use '-vv' to show

.tox/py37-django22-drf312/lib/python3.7/site-packages/snapshottest/module.py:233: AssertionError
========================================================================================== SnapshotTest summary ==========================================================================================
12 snapshots passed.
2 snapshots failed in 1 test suites. Inspect your code or run with `pytest --snapshot-update` to update them.
```

When I run `pytest --snapshot-update` the tests all pass. Am I good to make a PR? Should I include the changed "example/tests/snapshots/snap_test_openapi.py" file in the PR after running `pytest --snapshot-update`? I'm in unfamiliar territory here.
@sliverc Yes snapshot needs to be updated and commited as well. But do not worry just open a PR with what you have. If something is missing I can then always comment there.

Resolving patch:
diff --git a/rest_framework_json_api/schemas/openapi.py b/rest_framework_json_api/schemas/openapi.py
--- a/rest_framework_json_api/schemas/openapi.py
+++ b/rest_framework_json_api/schemas/openapi.py
@@ -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):
@@ -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
@@ -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",


Test patch:
diff --git a/example/tests/__snapshots__/test_openapi.ambr b/example/tests/__snapshots__/test_openapi.ambr
--- a/example/tests/__snapshots__/test_openapi.ambr
+++ b/example/tests/__snapshots__/test_openapi.ambr
@@ -133,7 +133,7 @@
                       "entries": {
                         "$ref": "#/components/schemas/reltomany"
                       },
-                      "first_entry": {
+                      "firstEntry": {
                         "$ref": "#/components/schemas/reltoone"
                       },
                       "type": {
@@ -541,7 +541,7 @@
                       "entries": {
                         "$ref": "#/components/schemas/reltomany"
                       },
-                      "first_entry": {
+                      "firstEntry": {
                         "$ref": "#/components/schemas/reltoone"
                       },
                       "type": {