Skip to content

fix openapi // path prefix#6724

Merged
carltongibson merged 1 commit intoencode:masterfrom
n2ygk:fix_determine_path_prefix
Jun 9, 2019
Merged

fix openapi // path prefix#6724
carltongibson merged 1 commit intoencode:masterfrom
n2ygk:fix_determine_path_prefix

Conversation

@n2ygk
Copy link
Copy Markdown
Contributor

@n2ygk n2ygk commented Jun 2, 2019

Description

Fixes // prefix for a path that has a common prefix. Prior to this fix, urls with a common prefix
would result in a doubled /. For example:

router = routers.DefaultRouter()
router.register(r'courses', views.CourseViewSet)

urlpatterns = [
    path('v1/', include(router.urls)),

generates this schema:

paths:
  //courses/:
    get:

Here's reproducing it showing that the assumption of trailing / being returned (as documented) is incorrect:

def determine_path_prefix(self, paths):
"""
Given a list of all paths, return the common prefix which should be
discounted when generating a schema structure.
This will be the longest common string that does not include that last
component of the URL, or the last component before a path parameter.
For example:
/api/v1/users/
/api/v1/users/{pk}/
The path prefix is '/api/v1/'
"""

Python 3.6.6 (default, Jul 27 2018, 14:31:43) 
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from rest_framework.schemas.openapi import SchemaGenerator
>>> s=SchemaGenerator()
>>> paths=['/api/v1/users/', '/api/v1/users/{pk}/']
>>> s.determine_path_prefix(paths)
'/api/v1'
>>> 

With the fix, this generates:

paths:
  /courses/:
    get:

@carltongibson carltongibson self-assigned this Jun 3, 2019
Copy link
Copy Markdown
Collaborator

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, super. Thank you for this @n2ygk

@carltongibson carltongibson added this to the 3.10 Release milestone Jun 9, 2019
@carltongibson carltongibson merged commit a63860f into encode:master Jun 9, 2019
@n2ygk n2ygk deleted the fix_determine_path_prefix branch June 9, 2019 12:52
pchiquet pushed a commit to pchiquet/django-rest-framework that referenced this pull request Nov 17, 2020
sigvef pushed a commit to sigvef/django-rest-framework that referenced this pull request Dec 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants