Skip to content

Commit

Permalink
Merge pull request #1289 from tfranzel/operation_id_method_position
Browse files Browse the repository at this point in the history
make operation_id action position configurable #1264
  • Loading branch information
tfranzel committed Sep 7, 2024
2 parents 2b4d5ab + a43a2b8 commit b401672
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion drf_spectacular/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,12 @@ def get_operation_id(self) -> str:
if re.search(r'<drf_format_suffix\w*:\w+>', self.path_regex):
tokenized_path.append('formatted')

return '_'.join(tokenized_path + [action])
if spectacular_settings.OPERATION_ID_METHOD_POSITION == 'PRE':
return '_'.join([action] + tokenized_path)
elif spectacular_settings.OPERATION_ID_METHOD_POSITION == 'POST':
return '_'.join(tokenized_path + [action])
else:
assert False, 'Invalid value for OPERATION_ID_METHOD_POSITION. Allowed: PRE, POST'

def is_deprecated(self) -> bool:
""" override this for custom behaviour """
Expand Down
5 changes: 5 additions & 0 deletions drf_spectacular/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@
# ``djangorestframework_camel_case``, while CAMELIZE_NAMES itself does not.
'CAMELIZE_NAMES': False,

# Changes the location of the action/method on the generated OperationId. For example,
# "POST": "group_person_list", "group_person_create"
# "PRE": "list_group_person", "create_group_person"
'OPERATION_ID_METHOD_POSITION': 'POST',

# Determines if and how free-form 'additionalProperties' should be emitted in the schema. Some
# code generator targets are sensitive to this. None disables generic 'additionalProperties'.
# allowed values are 'dict', 'bool', None
Expand Down
11 changes: 11 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3410,3 +3410,14 @@ class XViewset(viewsets.ModelViewSet):
assert schema['components']['schemas']['X']["properties"]["field_shirts"] == {
"readOnly": True, 'type': 'string'
}


@mock.patch('drf_spectacular.settings.spectacular_settings.OPERATION_ID_METHOD_POSITION', "PRE")
def test_operation_id_method_position(no_warnings):
class XViewSet(viewsets.ReadOnlyModelViewSet):
queryset = SimpleModel.objects.all()
serializer_class = SimpleSerializer

schema = generate_schema('/x', XViewSet)
assert schema['paths']['/x/']['get']["operationId"] == 'list_x'
assert schema['paths']['/x/{id}/']['get']["operationId"] == 'retrieve_x'

0 comments on commit b401672

Please sign in to comment.