Skip to content

Duplicate OpenAPI operation IDs when there are multiple generic view sets for the same model #6844

@reupen

Description

@reupen

Checklist

  • I have verified that that issue exists against the master branch of Django REST framework.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • This is not a usage question. (Those should be directed to the discussion group instead.)
  • This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • I have reduced the issue to the simplest possible case.
  • I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

  1. Define two generic view sets for the same model. E.g.:

    class PersonV1(ModelViewSet):
        queryset = Person.objects.all()
        serializer_class = PersonV1Serializer
    
    class PersonV2(ModelViewSet):
        queryset = Person.objects.all()
        serializer_class = PersonV2Serializer

    (There are multiple reasons why you might do this e.g. old and new versions, different filtering, different audiences...)

  2. Enable OpenAPI schema generation

Expected behavior

The operationID values generated are unique as required per the OpenAPI spec.

Actual behavior

The operation IDs are the same for both sets of views. (One problem this causes is that clicking on the operation in Swagger UI causes both operations to expand together.)

Discussion and possible solutions

The operation IDs generated are in the form of <action><model name> i.e. ListPerson etc. (The logic is in AutoSchema._get_operation_id.)

I think it is going to be difficult to come up with a pattern for generating meaningful unique names in all cases. However, there should be some effort to make sure that the generated operation IDs are unique.

One possible solution would be to:

  1. Detect duplicates.

  2. Append an automatically-generated suffix in such cases.

    This could, perhaps, be the first 8-characters of a hash of the view's dotted module path (i.e. {view.__module__}.{view.__name__}).

    (The reason for the suggestion of a hash is so that it is, at least, reasonably stable.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions