Skip to content

Commit b4c37c1

Browse files
committed
make future DRF 3.12 openapi changes work for DRF 3.11
1 parent 3689f92 commit b4c37c1

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

example/tests/test_openapi.py

+9
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ def test_delete_request(snapshot):
103103
{'delete': 'delete'}
104104
)
105105
inspector = AutoSchema()
106+
# DRF >=3.12 changes the capitalization of these method mappings which breaks the snapshot,
107+
# so just override them to be consistent with >=3.12
108+
inspector.method_mapping = {
109+
'get': 'retrieve',
110+
'post': 'create',
111+
'put': 'update',
112+
'patch': 'partialUpdate',
113+
'delete': 'destroy',
114+
}
106115
inspector.view = view
107116

108117
operation = inspector.get_operation(path, method)

rest_framework_json_api/schemas/openapi.py

+14
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ class SchemaGenerator(drf_openapi.SchemaGenerator):
273273
def __init__(self, *args, **kwargs):
274274
self.openapi_schema = {}
275275
super().__init__(*args, **kwargs)
276+
if not hasattr(self, 'check_duplicate_operation_id'):
277+
self.check_duplicate_operation_id = lambda paths: None
276278

277279
def get_schema(self, request=None, public=False):
278280
"""
@@ -453,6 +455,18 @@ def __init__(self, openapi_schema=None, **kwargs):
453455
# TODO: shallow or deep merge?
454456
self.openapi_schema = {**self.openapi_schema, **jsonapi_ref}
455457

458+
# DRF >= 3.12 (not yet released) has changed a bunch of private methods to public.
459+
# Accommodate those renamings for DRF < 3.12
460+
# TODO: Since the DRF version string is not updated until after a release, for now,
461+
# still need to check for it like this:
462+
if not hasattr(self, 'get_path_parameters'): # use this as a proxy for < 3.12
463+
self.get_path_parameters = self._get_path_parameters
464+
self.get_pagination_parameters = self._get_pagination_parameters
465+
self.get_filter_parameters = self._get_filter_parameters
466+
self.get_components = lambda path, method: {} # no get_components for <= 3.12
467+
self.map_field = self._map_field
468+
self.map_field_validators = self._map_field_validators
469+
456470
def get_operation(self, path, method, action=None):
457471
""" basically a copy of AutoSchema.get_operation """
458472
operation = {}

0 commit comments

Comments
 (0)