mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-11-04 09:57:55 +03:00 
			
		
		
		
	Fix OpenAPI path generation with common prefixes.
Closes #6675. Closes #6823.
This commit is contained in:
		
							parent
							
								
									30a21a98dc
								
							
						
					
					
						commit
						e309a4f0b8
					
				| 
						 | 
					@ -1,4 +1,5 @@
 | 
				
			||||||
import warnings
 | 
					import warnings
 | 
				
			||||||
 | 
					from urllib.parse import urljoin
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from django.core.validators import (
 | 
					from django.core.validators import (
 | 
				
			||||||
    DecimalValidator, EmailValidator, MaxLengthValidator, MaxValueValidator,
 | 
					    DecimalValidator, EmailValidator, MaxLengthValidator, MaxValueValidator,
 | 
				
			||||||
| 
						 | 
					@ -39,17 +40,18 @@ class SchemaGenerator(BaseSchemaGenerator):
 | 
				
			||||||
        # Only generate the path prefix for paths that will be included
 | 
					        # Only generate the path prefix for paths that will be included
 | 
				
			||||||
        if not paths:
 | 
					        if not paths:
 | 
				
			||||||
            return None
 | 
					            return None
 | 
				
			||||||
        prefix = self.determine_path_prefix(paths)
 | 
					 | 
				
			||||||
        if prefix == '/':  # no prefix
 | 
					 | 
				
			||||||
            prefix = ''
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for path, method, view in view_endpoints:
 | 
					        for path, method, view in view_endpoints:
 | 
				
			||||||
            if not self.has_view_permissions(path, method, view):
 | 
					            if not self.has_view_permissions(path, method, view):
 | 
				
			||||||
                continue
 | 
					                continue
 | 
				
			||||||
            operation = view.schema.get_operation(path, method)
 | 
					            operation = view.schema.get_operation(path, method)
 | 
				
			||||||
            subpath = path[len(prefix):]
 | 
					            # Normalise path for any provided mount url.
 | 
				
			||||||
            result.setdefault(subpath, {})
 | 
					            if path.startswith('/'):
 | 
				
			||||||
            result[subpath][method.lower()] = operation
 | 
					                path = path[1:]
 | 
				
			||||||
 | 
					            path = urljoin(self.url or '/', path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            result.setdefault(path, {})
 | 
				
			||||||
 | 
					            result[path][method.lower()] = operation
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return result
 | 
					        return result
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -247,7 +247,7 @@ class TestGenerator(TestCase):
 | 
				
			||||||
            url(r'^example/?$', views.ExampleListView.as_view()),
 | 
					            url(r'^example/?$', views.ExampleListView.as_view()),
 | 
				
			||||||
            url(r'^example/{pk}/?$', views.ExampleDetailView.as_view()),
 | 
					            url(r'^example/{pk}/?$', views.ExampleDetailView.as_view()),
 | 
				
			||||||
        ]
 | 
					        ]
 | 
				
			||||||
        generator = SchemaGenerator(patterns=patterns, url='/api/')
 | 
					        generator = SchemaGenerator(patterns=patterns, url='/api')
 | 
				
			||||||
        generator._initialise_endpoints()
 | 
					        generator._initialise_endpoints()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        paths = generator.get_paths()
 | 
					        paths = generator.get_paths()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user