Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make operation_id action position configurable #1264 #1289

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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'

Check warning on line 468 in drf_spectacular/openapi.py

View check run for this annotation

Codecov / codecov/patch

drf_spectacular/openapi.py#L468

Added line #L468 was not covered by tests

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'
Loading