django-api-versioning 0.3.1__tar.gz → 0.3.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/CHANGELOG.md +38 -1
  2. {django_api_versioning-0.3.1/django_api_versioning.egg-info → django_api_versioning-0.3.3}/PKG-INFO +21 -10
  3. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/README.md +17 -9
  4. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/django_api_versioning/lifecycle.py +47 -0
  5. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3/django_api_versioning.egg-info}/PKG-INFO +21 -10
  6. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/pyproject.toml +4 -1
  7. django_api_versioning-0.3.3/requirements.txt +1 -0
  8. django_api_versioning-0.3.3/tests/test_lifecycle.py +267 -0
  9. django_api_versioning-0.3.1/requirements.txt +0 -1
  10. django_api_versioning-0.3.1/tests/test_lifecycle.py +0 -107
  11. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/LICENSE +0 -0
  12. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/MANIFEST.in +0 -0
  13. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/django_api_versioning/__init__.py +0 -0
  14. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/django_api_versioning/channels.py +0 -0
  15. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/django_api_versioning/decorators.py +0 -0
  16. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/django_api_versioning/exceptions.py +0 -0
  17. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/django_api_versioning/management/__init__.py +0 -0
  18. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/django_api_versioning/management/commands/__init__.py +0 -0
  19. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/django_api_versioning/management/commands/check_versions.py +0 -0
  20. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/django_api_versioning/management/commands/migrate_endpoints.py +0 -0
  21. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/django_api_versioning/registry.py +0 -0
  22. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/django_api_versioning/reverse.py +0 -0
  23. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/django_api_versioning/settings.py +0 -0
  24. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/django_api_versioning/testing.py +0 -0
  25. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/django_api_versioning/urls.py +0 -0
  26. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/django_api_versioning.egg-info/SOURCES.txt +0 -0
  27. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/django_api_versioning.egg-info/dependency_links.txt +0 -0
  28. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/django_api_versioning.egg-info/requires.txt +0 -0
  29. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/django_api_versioning.egg-info/top_level.txt +0 -0
  30. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/pytest.ini +0 -0
  31. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/requirements-dev.txt +0 -0
  32. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/setup.cfg +0 -0
  33. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/tests/__init__.py +0 -0
  34. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/tests/conftest.py +0 -0
  35. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/tests/settings.py +0 -0
  36. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/tests/test_channels.py +0 -0
  37. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/tests/test_channels_extra.py +0 -0
  38. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/tests/test_decorators.py +0 -0
  39. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/tests/test_exceptions.py +0 -0
  40. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/tests/test_management_commands.py +0 -0
  41. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/tests/test_registry.py +0 -0
  42. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/tests/test_reverse.py +0 -0
  43. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/tests/test_segment_model.py +0 -0
  44. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/tests/test_settings.py +0 -0
  45. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/tests/test_testing_utils.py +0 -0
  46. {django_api_versioning-0.3.1 → django_api_versioning-0.3.3}/tests/test_urls.py +0 -0
@@ -8,6 +8,39 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8
8
 
9
9
  ## [Unreleased]
10
10
 
11
+ ## [0.3.3] - 2026-11-10
12
+
13
+ ### Fixed
14
+
15
+ - **LifecycleMiddleware version extraction:** Fixed version detection from static URL paths generated by `@endpoint` decorator. LifecycleMiddleware now correctly extracts API versions from the URL path as a fallback when `request.version` and `resolver_match.kwargs["version"]` are unavailable, enabling proper Deprecation/Sunset header injection and retired version enforcement for path-based versioned endpoints.
16
+ - **URL path pattern matching with leading slashes:** Fixed regex pattern to correctly handle both `API_BASE_PATH` configurations with and without leading slashes. The pattern `^/?{prefix}(\\d+){suffix}` now properly matches versioned URL paths regardless of leading slash configuration.
17
+
18
+ ### Added
19
+
20
+ - **Comprehensive lifecycle integration tests:** Added 15 focused integration tests covering all lifecycle scenarios: deprecated versions, retired versions, active versions, unversioned endpoints, parameterized routes, query strings, DRF integration, error responses, and url prefix anchoring.
21
+ - **Enhanced unit tests for version extraction:** Added 14 focused unit tests for `_extract_version()` and `_extract_version_from_path()` covering version precedence, multi-digit versions, query strings, path anchoring, and error handling.
22
+
23
+ ### Verified
24
+
25
+ - ✅ All lifecycle integration tests passing (15/15)
26
+ - ✅ All lifecycle unit tests passing (22/22 in test_lifecycle.py)
27
+ - ✅ Complete integration test suite passing (236/236 tests)
28
+ - ✅ Full package test suite passing (118/118 tests)
29
+ - ✅ Version extraction precedence: request.version → resolver_match.kwargs["version"] → static URL path
30
+ - ✅ Multi-digit API versions correctly extracted
31
+ - ✅ Leading and missing leading slashes handled correctly
32
+ - ✅ Regex escaping of literal path segments
33
+ - ✅ Anchored prefix matching (no false positives)
34
+ - ✅ Query string independence verified
35
+ - ✅ Numeric resource identifiers in URL paths don't affect version extraction
36
+ - ✅ DRF response integration verified
37
+
38
+ ## [0.3.2] - 2026-11-10
39
+
40
+ ### Fixed
41
+
42
+ - **PyPI Package Classifiers:** Updated to include all tested Django versions (4.2, 5.0, 5.1) in PyPI metadata. Previous v0.3.1 was published with incomplete Django version classifiers. This patch release ensures PyPI shows complete compatibility: Django 4.2, 5.0, 5.1, 5.2, 6.0, and 6.1.
43
+
11
44
  ## [0.3.1] - 2026-11-08
12
45
 
13
46
  ### Added
@@ -15,7 +48,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
15
48
  - **Comprehensive Integration Testing:** 222+ integration tests covering DRF APIView, GenericAPIView, ViewSets, routers, custom actions, authentication, permissions, and Django-native views.
16
49
  - **Enhanced Testing Infrastructure:** Management command tests for `check_versions` and version matrix validation.
17
50
  - **Feature Verification Matrix:** Complete traceability matrix documenting 90+ features verified against implementation.
18
- - **Compatibility Report:** Comprehensive Python 3.10+, Django 5.2–6.1, and DRF 3.14+ compatibility matrix with test results.
51
+ - **Compatibility Report:** Comprehensive Python 3.10+, Django 4.2–6.1, and DRF 3.14+ compatibility matrix with test results.
52
+
53
+ ### Changed
54
+
55
+ - **Clarified Django support:** Documentation now correctly lists Django 4.2 as minimum supported version (verified working). Previous documentation incorrectly listed Django 5.2 as minimum. Tested versions: Django 4.2, 5.0, 5.1, 5.2, 6.0, 6.1.
19
56
 
20
57
  ### Fixed
21
58
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: django-api-versioning
3
- Version: 0.3.1
3
+ Version: 0.3.3
4
4
  Summary: Django API versioning for Django and Django REST Framework (DRF), with decorator-based endpoint versioning, automatic URL registration, backward compatibility, and segment-based forward inheritance.
5
5
  Author-email: Mojtaba Arvin <ArvinDevDay@gmail.com>
6
6
  License: MIT
@@ -17,6 +17,9 @@ Classifier: Programming Language :: Python :: 3.13
17
17
  Classifier: Programming Language :: Python :: 3.14
18
18
  Classifier: Programming Language :: Python :: 3.15
19
19
  Classifier: Framework :: Django
20
+ Classifier: Framework :: Django :: 4.2
21
+ Classifier: Framework :: Django :: 5.0
22
+ Classifier: Framework :: Django :: 5.1
20
23
  Classifier: Framework :: Django :: 5.2
21
24
  Classifier: Framework :: Django :: 6.0
22
25
  Classifier: Framework :: Django :: 6.1
@@ -49,7 +52,7 @@ Dynamic: license-file
49
52
  [![PyPI version](https://badge.fury.io/py/django-api-versioning.svg)](https://badge.fury.io/py/django-api-versioning)
50
53
  [![PyPI Downloads](https://img.shields.io/pypi/dm/django-api-versioning.svg)](https://pypi.org/project/django-api-versioning/)
51
54
  [![Build Status](https://github.com/mojtaba-arvin/django-api-versioning/actions/workflows/tests.yml/badge.svg)](https://github.com/mojtaba-arvin/django-api-versioning/actions)
52
- [![Django versions](https://img.shields.io/badge/django-5.2%20%7C%206.0%20%7C%206.1-blue)](https://www.djangoproject.com/)
55
+ [![Django versions](https://img.shields.io/badge/django-4.2%20%7C%205.x%20%7C%206.x-blue)](https://www.djangoproject.com/)
53
56
  [![Python versions](https://img.shields.io/pypi/pyversions/django-api-versioning.svg)](https://pypi.org/project/django-api-versioning/)
54
57
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
55
58
 
@@ -124,9 +127,11 @@ Install [Django API Versioning](https://pypi.org/project/django-api-versioning/)
124
127
  pip install django-api-versioning
125
128
  ```
126
129
 
127
- Requires **Django >= 5.2** and **Python >= 3.10**.
130
+ Requires **Django >= 4.2** and **Python >= 3.10**.
128
131
  Django REST Framework is optional and auto-detected.
129
132
 
133
+ **Tested Versions**: Django 4.2, 5.0, 5.1, 5.2, 6.0, 6.1 (all verified working)
134
+
130
135
  ## Quick Start
131
136
 
132
137
  ### 1. Configure Django settings
@@ -539,12 +544,18 @@ The `migrate_endpoints` command rewrites 0.2.0 decorators to 0.3.0 syntax automa
539
544
 
540
545
  ## Requirements
541
546
 
542
- | Django | Python |
543
- | --- | --- |
544
- | 5.2 (LTS, until April 2028) | 3.10+ |
545
- | 6.0 | 3.10+ |
546
- | 6.1 | 3.10+ |
547
- Django 5.2 is the minimum supported release; Django 4.2 reached end-of-life in April 2026. DRF is optional. The package reads `request.version`, which is what DRF's `URLPathVersioning`, `AcceptHeaderVersioning`, `NamespaceVersioning`, and `QueryParameterVersioning` set.
547
+ | Django | Python | Status |
548
+ | --- | --- | --- |
549
+ | 4.2 (LTS) | 3.10+ | ✅ Supported |
550
+ | 5.0 | 3.10+ | ✅ Supported |
551
+ | 5.1 | 3.10+ | ✅ Supported |
552
+ | 5.2 (LTS, until April 2028) | 3.10+ | ✅ Supported |
553
+ | 6.0 | 3.10+ | ✅ Supported |
554
+ | 6.1 | 3.10+ | ✅ Supported |
555
+
556
+ **Minimum requirements:** Django >= 4.2, Python >= 3.10.
557
+
558
+ DRF is optional. The package reads `request.version`, which is what DRF's `URLPathVersioning`, `AcceptHeaderVersioning`, `NamespaceVersioning`, and `QueryParameterVersioning` set.
548
559
 
549
560
  ---
550
561
 
@@ -582,7 +593,7 @@ docker run --rm dapi:py311
582
593
 
583
594
  1. Bump `version` in `pyproject.toml` and update `CHANGELOG.md`
584
595
  2. Commit, push to `main`
585
- 3. `git tag v0.3.1 && git push origin v0.3.1` → CI publishes to PyPI
596
+ 3. `git tag vX.Y.Z && git push origin vX.Y.Z` → CI publishes to PyPI
586
597
 
587
598
  ## Reporting issues
588
599
 
@@ -3,7 +3,7 @@
3
3
  [![PyPI version](https://badge.fury.io/py/django-api-versioning.svg)](https://badge.fury.io/py/django-api-versioning)
4
4
  [![PyPI Downloads](https://img.shields.io/pypi/dm/django-api-versioning.svg)](https://pypi.org/project/django-api-versioning/)
5
5
  [![Build Status](https://github.com/mojtaba-arvin/django-api-versioning/actions/workflows/tests.yml/badge.svg)](https://github.com/mojtaba-arvin/django-api-versioning/actions)
6
- [![Django versions](https://img.shields.io/badge/django-5.2%20%7C%206.0%20%7C%206.1-blue)](https://www.djangoproject.com/)
6
+ [![Django versions](https://img.shields.io/badge/django-4.2%20%7C%205.x%20%7C%206.x-blue)](https://www.djangoproject.com/)
7
7
  [![Python versions](https://img.shields.io/pypi/pyversions/django-api-versioning.svg)](https://pypi.org/project/django-api-versioning/)
8
8
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
9
9
 
@@ -78,9 +78,11 @@ Install [Django API Versioning](https://pypi.org/project/django-api-versioning/)
78
78
  pip install django-api-versioning
79
79
  ```
80
80
 
81
- Requires **Django >= 5.2** and **Python >= 3.10**.
81
+ Requires **Django >= 4.2** and **Python >= 3.10**.
82
82
  Django REST Framework is optional and auto-detected.
83
83
 
84
+ **Tested Versions**: Django 4.2, 5.0, 5.1, 5.2, 6.0, 6.1 (all verified working)
85
+
84
86
  ## Quick Start
85
87
 
86
88
  ### 1. Configure Django settings
@@ -493,12 +495,18 @@ The `migrate_endpoints` command rewrites 0.2.0 decorators to 0.3.0 syntax automa
493
495
 
494
496
  ## Requirements
495
497
 
496
- | Django | Python |
497
- | --- | --- |
498
- | 5.2 (LTS, until April 2028) | 3.10+ |
499
- | 6.0 | 3.10+ |
500
- | 6.1 | 3.10+ |
501
- Django 5.2 is the minimum supported release; Django 4.2 reached end-of-life in April 2026. DRF is optional. The package reads `request.version`, which is what DRF's `URLPathVersioning`, `AcceptHeaderVersioning`, `NamespaceVersioning`, and `QueryParameterVersioning` set.
498
+ | Django | Python | Status |
499
+ | --- | --- | --- |
500
+ | 4.2 (LTS) | 3.10+ | ✅ Supported |
501
+ | 5.0 | 3.10+ | ✅ Supported |
502
+ | 5.1 | 3.10+ | ✅ Supported |
503
+ | 5.2 (LTS, until April 2028) | 3.10+ | ✅ Supported |
504
+ | 6.0 | 3.10+ | ✅ Supported |
505
+ | 6.1 | 3.10+ | ✅ Supported |
506
+
507
+ **Minimum requirements:** Django >= 4.2, Python >= 3.10.
508
+
509
+ DRF is optional. The package reads `request.version`, which is what DRF's `URLPathVersioning`, `AcceptHeaderVersioning`, `NamespaceVersioning`, and `QueryParameterVersioning` set.
502
510
 
503
511
  ---
504
512
 
@@ -536,7 +544,7 @@ docker run --rm dapi:py311
536
544
 
537
545
  1. Bump `version` in `pyproject.toml` and update `CHANGELOG.md`
538
546
  2. Commit, push to `main`
539
- 3. `git tag v0.3.1 && git push origin v0.3.1` → CI publishes to PyPI
547
+ 3. `git tag vX.Y.Z && git push origin vX.Y.Z` → CI publishes to PyPI
540
548
 
541
549
  ## Reporting issues
542
550
 
@@ -1,4 +1,5 @@
1
1
  import logging
2
+ import re
2
3
  from datetime import datetime, timezone
3
4
  from typing import Callable
4
5
 
@@ -62,6 +63,52 @@ def _extract_version(request: HttpRequest) -> int | None:
62
63
  return int(match.kwargs["version"])
63
64
  except (TypeError, ValueError):
64
65
  pass
66
+ # Fallback: extract from static URL path using API_BASE_PATH pattern.
67
+ version = _extract_version_from_path(request)
68
+ if version is not None:
69
+ return version
70
+ return None
71
+
72
+
73
+ def _extract_version_from_path(request: HttpRequest) -> int | None:
74
+ """
75
+ Extract API version from static URL paths generated by @endpoint.
76
+
77
+ For example, with API_BASE_PATH = "api/v{version}/", a request to
78
+ /api/v2/users/ will extract version 2.
79
+
80
+ Returns None if no valid version is found.
81
+ """
82
+ api_base_path = settings.API_BASE_PATH
83
+ if not api_base_path or "{version}" not in api_base_path:
84
+ return None
85
+
86
+ # Get the request path (without query string)
87
+ path = request.path_info or request.path or ""
88
+
89
+ # Build regex pattern from API_BASE_PATH
90
+ # Split on {version}, escape each part, then rejoin with version pattern
91
+ parts = api_base_path.split("{version}")
92
+ if len(parts) != 2:
93
+ return None
94
+
95
+ # Escape regex special characters in each part
96
+ prefix = re.escape(parts[0])
97
+ suffix = re.escape(parts[1])
98
+
99
+ # Create anchored pattern to match only at the beginning of the relevant path.
100
+ # The pattern may have a leading slash (from request.path_info), but API_BASE_PATH
101
+ # typically doesn't, so we allow an optional leading slash.
102
+ pattern = f"^/?{prefix}(\\d+){suffix}"
103
+
104
+ try:
105
+ regex_match = re.match(pattern, path)
106
+ if regex_match:
107
+ version_str = regex_match.group(1)
108
+ return int(version_str)
109
+ except (IndexError, ValueError, re.error):
110
+ pass
111
+
65
112
  return None
66
113
 
67
114
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: django-api-versioning
3
- Version: 0.3.1
3
+ Version: 0.3.3
4
4
  Summary: Django API versioning for Django and Django REST Framework (DRF), with decorator-based endpoint versioning, automatic URL registration, backward compatibility, and segment-based forward inheritance.
5
5
  Author-email: Mojtaba Arvin <ArvinDevDay@gmail.com>
6
6
  License: MIT
@@ -17,6 +17,9 @@ Classifier: Programming Language :: Python :: 3.13
17
17
  Classifier: Programming Language :: Python :: 3.14
18
18
  Classifier: Programming Language :: Python :: 3.15
19
19
  Classifier: Framework :: Django
20
+ Classifier: Framework :: Django :: 4.2
21
+ Classifier: Framework :: Django :: 5.0
22
+ Classifier: Framework :: Django :: 5.1
20
23
  Classifier: Framework :: Django :: 5.2
21
24
  Classifier: Framework :: Django :: 6.0
22
25
  Classifier: Framework :: Django :: 6.1
@@ -49,7 +52,7 @@ Dynamic: license-file
49
52
  [![PyPI version](https://badge.fury.io/py/django-api-versioning.svg)](https://badge.fury.io/py/django-api-versioning)
50
53
  [![PyPI Downloads](https://img.shields.io/pypi/dm/django-api-versioning.svg)](https://pypi.org/project/django-api-versioning/)
51
54
  [![Build Status](https://github.com/mojtaba-arvin/django-api-versioning/actions/workflows/tests.yml/badge.svg)](https://github.com/mojtaba-arvin/django-api-versioning/actions)
52
- [![Django versions](https://img.shields.io/badge/django-5.2%20%7C%206.0%20%7C%206.1-blue)](https://www.djangoproject.com/)
55
+ [![Django versions](https://img.shields.io/badge/django-4.2%20%7C%205.x%20%7C%206.x-blue)](https://www.djangoproject.com/)
53
56
  [![Python versions](https://img.shields.io/pypi/pyversions/django-api-versioning.svg)](https://pypi.org/project/django-api-versioning/)
54
57
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
55
58
 
@@ -124,9 +127,11 @@ Install [Django API Versioning](https://pypi.org/project/django-api-versioning/)
124
127
  pip install django-api-versioning
125
128
  ```
126
129
 
127
- Requires **Django >= 5.2** and **Python >= 3.10**.
130
+ Requires **Django >= 4.2** and **Python >= 3.10**.
128
131
  Django REST Framework is optional and auto-detected.
129
132
 
133
+ **Tested Versions**: Django 4.2, 5.0, 5.1, 5.2, 6.0, 6.1 (all verified working)
134
+
130
135
  ## Quick Start
131
136
 
132
137
  ### 1. Configure Django settings
@@ -539,12 +544,18 @@ The `migrate_endpoints` command rewrites 0.2.0 decorators to 0.3.0 syntax automa
539
544
 
540
545
  ## Requirements
541
546
 
542
- | Django | Python |
543
- | --- | --- |
544
- | 5.2 (LTS, until April 2028) | 3.10+ |
545
- | 6.0 | 3.10+ |
546
- | 6.1 | 3.10+ |
547
- Django 5.2 is the minimum supported release; Django 4.2 reached end-of-life in April 2026. DRF is optional. The package reads `request.version`, which is what DRF's `URLPathVersioning`, `AcceptHeaderVersioning`, `NamespaceVersioning`, and `QueryParameterVersioning` set.
547
+ | Django | Python | Status |
548
+ | --- | --- | --- |
549
+ | 4.2 (LTS) | 3.10+ | ✅ Supported |
550
+ | 5.0 | 3.10+ | ✅ Supported |
551
+ | 5.1 | 3.10+ | ✅ Supported |
552
+ | 5.2 (LTS, until April 2028) | 3.10+ | ✅ Supported |
553
+ | 6.0 | 3.10+ | ✅ Supported |
554
+ | 6.1 | 3.10+ | ✅ Supported |
555
+
556
+ **Minimum requirements:** Django >= 4.2, Python >= 3.10.
557
+
558
+ DRF is optional. The package reads `request.version`, which is what DRF's `URLPathVersioning`, `AcceptHeaderVersioning`, `NamespaceVersioning`, and `QueryParameterVersioning` set.
548
559
 
549
560
  ---
550
561
 
@@ -582,7 +593,7 @@ docker run --rm dapi:py311
582
593
 
583
594
  1. Bump `version` in `pyproject.toml` and update `CHANGELOG.md`
584
595
  2. Commit, push to `main`
585
- 3. `git tag v0.3.1 && git push origin v0.3.1` → CI publishes to PyPI
596
+ 3. `git tag vX.Y.Z && git push origin vX.Y.Z` → CI publishes to PyPI
586
597
 
587
598
  ## Reporting issues
588
599
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "django-api-versioning"
7
- version = "0.3.1"
7
+ version = "0.3.3"
8
8
  description = "Django API versioning for Django and Django REST Framework (DRF), with decorator-based endpoint versioning, automatic URL registration, backward compatibility, and segment-based forward inheritance."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -37,6 +37,9 @@ classifiers = [
37
37
  "Programming Language :: Python :: 3.14",
38
38
  "Programming Language :: Python :: 3.15",
39
39
  "Framework :: Django",
40
+ "Framework :: Django :: 4.2",
41
+ "Framework :: Django :: 5.0",
42
+ "Framework :: Django :: 5.1",
40
43
  "Framework :: Django :: 5.2",
41
44
  "Framework :: Django :: 6.0",
42
45
  "Framework :: Django :: 6.1",
@@ -0,0 +1 @@
1
+ Django>=4.2
@@ -0,0 +1,267 @@
1
+ from datetime import date, timedelta
2
+
3
+ from django.http import HttpResponse
4
+ from django.test import RequestFactory
5
+
6
+ from django_api_versioning.lifecycle import LifecycleMiddleware
7
+ from django_api_versioning.settings import api_settings as settings
8
+
9
+ # Dates relative to "today" so the tests never depend on the current date.
10
+ FUTURE = (date.today() + timedelta(days=365)).isoformat()
11
+ PAST = (date.today() - timedelta(days=365)).isoformat()
12
+
13
+
14
+ def _request(version: str):
15
+ request = RequestFactory().get(f"/api/v{version}/users")
16
+ request.version = version
17
+ return request
18
+
19
+
20
+ def test_deprecated_version_gets_headers(monkeypatch):
21
+ monkeypatch.setitem(
22
+ settings.API_VERSION_LIFECYCLE,
23
+ 2,
24
+ {"status": "deprecated", "sunset": FUTURE},
25
+ )
26
+ middleware = LifecycleMiddleware(lambda req: HttpResponse("ok"))
27
+ response = middleware(_request("2"))
28
+
29
+ assert response["Deprecation"] == "true"
30
+ assert "Sunset" in response
31
+
32
+
33
+ def test_deprecated_without_sunset_only_deprecation(monkeypatch):
34
+ monkeypatch.setitem(
35
+ settings.API_VERSION_LIFECYCLE,
36
+ 2,
37
+ {"status": "deprecated"},
38
+ )
39
+ middleware = LifecycleMiddleware(lambda req: HttpResponse("ok"))
40
+ response = middleware(_request("2"))
41
+
42
+ assert response["Deprecation"] == "true"
43
+ assert "Sunset" not in response
44
+
45
+
46
+ def test_retired_version_returns_410(monkeypatch):
47
+ monkeypatch.setitem(
48
+ settings.API_VERSION_LIFECYCLE,
49
+ 1,
50
+ {"status": "retired", "sunset": PAST},
51
+ )
52
+ middleware = LifecycleMiddleware(lambda req: HttpResponse("ok"))
53
+ response = middleware(_request("1"))
54
+
55
+ assert response.status_code == 410
56
+
57
+
58
+ def test_future_sunset_with_retired_status_still_serves(monkeypatch):
59
+ """status=retired but sunset is in the future -> serve normally with no 410."""
60
+ monkeypatch.setitem(
61
+ settings.API_VERSION_LIFECYCLE,
62
+ 1,
63
+ {"status": "retired", "sunset": FUTURE},
64
+ )
65
+ middleware = LifecycleMiddleware(lambda req: HttpResponse("ok"))
66
+ response = middleware(_request("1"))
67
+
68
+ assert response.status_code == 200
69
+
70
+
71
+ def test_active_version_has_no_headers(monkeypatch):
72
+ monkeypatch.setitem(settings.API_VERSION_LIFECYCLE, 3, {"status": "active"})
73
+ middleware = LifecycleMiddleware(lambda req: HttpResponse("ok"))
74
+ response = middleware(_request("3"))
75
+
76
+ assert "Deprecation" not in response
77
+ assert "Sunset" not in response
78
+
79
+
80
+ def test_unknown_version_is_passthrough(monkeypatch):
81
+ monkeypatch.setitem(settings.API_VERSION_LIFECYCLE, 3, {"status": "active"})
82
+ middleware = LifecycleMiddleware(lambda req: HttpResponse("ok"))
83
+ response = middleware(_request("99"))
84
+
85
+ assert response.status_code == 200
86
+ assert "Deprecation" not in response
87
+
88
+
89
+ def test_request_without_version_is_passthrough():
90
+ request = RequestFactory().get("/some/path")
91
+ middleware = LifecycleMiddleware(lambda req: HttpResponse("ok"))
92
+ response = middleware(request)
93
+
94
+ assert response.status_code == 200
95
+
96
+
97
+ def test_invalid_sunset_date_is_ignored(monkeypatch, caplog):
98
+ monkeypatch.setitem(
99
+ settings.API_VERSION_LIFECYCLE,
100
+ 2,
101
+ {"status": "deprecated", "sunset": "not-a-date"},
102
+ )
103
+ middleware = LifecycleMiddleware(lambda req: HttpResponse("ok"))
104
+ response = middleware(_request("2"))
105
+
106
+ assert response["Deprecation"] == "true"
107
+ assert "Invalid sunset date" in caplog.text
108
+
109
+
110
+ # Tests for _extract_version() and _extract_version_from_path()
111
+ class TestVersionExtraction:
112
+ """Focused unit tests for version extraction logic."""
113
+
114
+ def test_extract_version_prefers_request_version(self):
115
+ """Version extraction prefers request.version when set."""
116
+ from django_api_versioning.lifecycle import _extract_version
117
+
118
+ request = RequestFactory().get("/api/v2/users/")
119
+ request.version = "3"
120
+ # Even though path says v2, request.version should win
121
+ version = _extract_version(request)
122
+ assert version == 3
123
+
124
+ def test_extract_version_prefers_resolver_match_kwargs(self):
125
+ """Version extraction prefers resolver_match.kwargs["version"] over path."""
126
+ from django_api_versioning.lifecycle import _extract_version
127
+ from unittest.mock import Mock
128
+
129
+ request = RequestFactory().get("/api/v2/users/")
130
+ request.version = None
131
+ request.resolver_match = Mock()
132
+ request.resolver_match.kwargs = {"version": "4"}
133
+ # resolver_match should be second priority
134
+ version = _extract_version(request)
135
+ assert version == 4
136
+
137
+ def test_extract_version_falls_back_to_path(self):
138
+ """Version extraction falls back to URL path when others are unavailable."""
139
+ from django_api_versioning.lifecycle import _extract_version
140
+
141
+ request = RequestFactory().get("/api/v2/users/")
142
+ request.version = None
143
+ request.resolver_match = None
144
+ # Should extract version from path
145
+ version = _extract_version(request)
146
+ assert version == 2
147
+
148
+ def test_extract_version_from_path_static_url(self):
149
+ """Extract version from static URL path."""
150
+ from django_api_versioning.lifecycle import _extract_version_from_path
151
+
152
+ request = RequestFactory().get("/api/v1/users/")
153
+ request.version = None
154
+ version = _extract_version_from_path(request)
155
+ assert version == 1
156
+
157
+ def test_extract_version_multi_digit_version(self):
158
+ """Extract multi-digit version numbers from path."""
159
+ from django_api_versioning.lifecycle import _extract_version_from_path
160
+
161
+ request = RequestFactory().get("/api/v42/users/")
162
+ request.version = None
163
+ version = _extract_version_from_path(request)
164
+ assert version == 42
165
+
166
+ def test_extract_version_with_url_parameters(self):
167
+ """Version is extracted from prefix, not from URL parameters."""
168
+ from django_api_versioning.lifecycle import _extract_version_from_path
169
+
170
+ request = RequestFactory().get("/api/v2/users/42/")
171
+ request.version = None
172
+ # Should extract v2, not 42
173
+ version = _extract_version_from_path(request)
174
+ assert version == 2
175
+
176
+ def test_extract_version_with_query_string(self):
177
+ """Query string doesn't affect version extraction."""
178
+ from django_api_versioning.lifecycle import _extract_version_from_path
179
+
180
+ request = RequestFactory().get("/api/v2/users/?page=1&limit=10")
181
+ request.version = None
182
+ version = _extract_version_from_path(request)
183
+ assert version == 2
184
+
185
+ def test_extract_version_no_match_returns_none(self):
186
+ """Returns None when path doesn't match API_BASE_PATH pattern."""
187
+ from django_api_versioning.lifecycle import _extract_version_from_path
188
+
189
+ request = RequestFactory().get("/unrelated/path/")
190
+ request.version = None
191
+ version = _extract_version_from_path(request)
192
+ assert version is None
193
+
194
+ def test_extract_version_invalid_version_format(self):
195
+ """Returns None for non-numeric version strings."""
196
+ from django_api_versioning.lifecycle import _extract_version_from_path
197
+
198
+ request = RequestFactory().get("/api/vx/users/")
199
+ request.version = None
200
+ version = _extract_version_from_path(request)
201
+ assert version is None
202
+
203
+ def test_extract_version_anchored_pattern(self):
204
+ """Version is only extracted from start of path, not middle."""
205
+ from django_api_versioning.lifecycle import _extract_version_from_path
206
+
207
+ # A path where "api/v2" appears but not at the start
208
+ # This should not match (returns None)
209
+ request = RequestFactory().get("/prefix/api/v2/users/")
210
+ request.version = None
211
+ version = _extract_version_from_path(request)
212
+ # Should be None because pattern is anchored at ^
213
+ assert version is None
214
+
215
+ def test_extract_version_with_request_version_invalid(self):
216
+ """Invalid request.version falls back to path extraction."""
217
+ from django_api_versioning.lifecycle import _extract_version
218
+
219
+ request = RequestFactory().get("/api/v2/users/")
220
+ request.version = "not_a_number"
221
+ version = _extract_version(request)
222
+ # Should fall back to path and get 2
223
+ assert version == 2
224
+
225
+ def test_extract_version_empty_lifecycle_config(self):
226
+ """Version extraction works even with empty lifecycle config."""
227
+ from django_api_versioning.lifecycle import _extract_version
228
+
229
+ request = RequestFactory().get("/api/v2/users/")
230
+ request.version = None
231
+ request.resolver_match = None
232
+ version = _extract_version(request)
233
+ # Should still extract from path
234
+ assert version == 2
235
+
236
+ def test_extract_version_no_resolver_match(self):
237
+ """Handles missing resolver_match gracefully."""
238
+ from django_api_versioning.lifecycle import _extract_version
239
+
240
+ request = RequestFactory().get("/api/v1/users/")
241
+ request.version = None
242
+ # Don't set resolver_match at all
243
+ version = _extract_version(request)
244
+ assert version == 1
245
+
246
+ def test_extract_version_precedence_order(self):
247
+ """Verifies version extraction precedence: request.version > kwargs > path."""
248
+ from django_api_versioning.lifecycle import _extract_version
249
+ from unittest.mock import Mock
250
+
251
+ # When all three are set, request.version wins
252
+ request = RequestFactory().get("/api/v1/users/")
253
+ request.version = "5"
254
+ request.resolver_match = Mock()
255
+ request.resolver_match.kwargs = {"version": "3"}
256
+ version = _extract_version(request)
257
+ assert version == 5
258
+
259
+ # When request.version is None, kwargs wins
260
+ request.version = None
261
+ version = _extract_version(request)
262
+ assert version == 3
263
+
264
+ # When both are None, path wins
265
+ request.resolver_match.kwargs = {}
266
+ version = _extract_version(request)
267
+ assert version == 1
@@ -1 +0,0 @@
1
- Django>=5.2
@@ -1,107 +0,0 @@
1
- from datetime import date, timedelta
2
-
3
- from django.http import HttpResponse
4
- from django.test import RequestFactory
5
-
6
- from django_api_versioning.lifecycle import LifecycleMiddleware
7
- from django_api_versioning.settings import api_settings as settings
8
-
9
- # Dates relative to "today" so the tests never depend on the current date.
10
- FUTURE = (date.today() + timedelta(days=365)).isoformat()
11
- PAST = (date.today() - timedelta(days=365)).isoformat()
12
-
13
-
14
- def _request(version: str):
15
- request = RequestFactory().get(f"/api/v{version}/users")
16
- request.version = version
17
- return request
18
-
19
-
20
- def test_deprecated_version_gets_headers(monkeypatch):
21
- monkeypatch.setitem(
22
- settings.API_VERSION_LIFECYCLE,
23
- 2,
24
- {"status": "deprecated", "sunset": FUTURE},
25
- )
26
- middleware = LifecycleMiddleware(lambda req: HttpResponse("ok"))
27
- response = middleware(_request("2"))
28
-
29
- assert response["Deprecation"] == "true"
30
- assert "Sunset" in response
31
-
32
-
33
- def test_deprecated_without_sunset_only_deprecation(monkeypatch):
34
- monkeypatch.setitem(
35
- settings.API_VERSION_LIFECYCLE,
36
- 2,
37
- {"status": "deprecated"},
38
- )
39
- middleware = LifecycleMiddleware(lambda req: HttpResponse("ok"))
40
- response = middleware(_request("2"))
41
-
42
- assert response["Deprecation"] == "true"
43
- assert "Sunset" not in response
44
-
45
-
46
- def test_retired_version_returns_410(monkeypatch):
47
- monkeypatch.setitem(
48
- settings.API_VERSION_LIFECYCLE,
49
- 1,
50
- {"status": "retired", "sunset": PAST},
51
- )
52
- middleware = LifecycleMiddleware(lambda req: HttpResponse("ok"))
53
- response = middleware(_request("1"))
54
-
55
- assert response.status_code == 410
56
-
57
-
58
- def test_future_sunset_with_retired_status_still_serves(monkeypatch):
59
- """status=retired but sunset is in the future -> serve normally with no 410."""
60
- monkeypatch.setitem(
61
- settings.API_VERSION_LIFECYCLE,
62
- 1,
63
- {"status": "retired", "sunset": FUTURE},
64
- )
65
- middleware = LifecycleMiddleware(lambda req: HttpResponse("ok"))
66
- response = middleware(_request("1"))
67
-
68
- assert response.status_code == 200
69
-
70
-
71
- def test_active_version_has_no_headers(monkeypatch):
72
- monkeypatch.setitem(settings.API_VERSION_LIFECYCLE, 3, {"status": "active"})
73
- middleware = LifecycleMiddleware(lambda req: HttpResponse("ok"))
74
- response = middleware(_request("3"))
75
-
76
- assert "Deprecation" not in response
77
- assert "Sunset" not in response
78
-
79
-
80
- def test_unknown_version_is_passthrough(monkeypatch):
81
- monkeypatch.setitem(settings.API_VERSION_LIFECYCLE, 3, {"status": "active"})
82
- middleware = LifecycleMiddleware(lambda req: HttpResponse("ok"))
83
- response = middleware(_request("99"))
84
-
85
- assert response.status_code == 200
86
- assert "Deprecation" not in response
87
-
88
-
89
- def test_request_without_version_is_passthrough():
90
- request = RequestFactory().get("/some/path")
91
- middleware = LifecycleMiddleware(lambda req: HttpResponse("ok"))
92
- response = middleware(request)
93
-
94
- assert response.status_code == 200
95
-
96
-
97
- def test_invalid_sunset_date_is_ignored(monkeypatch, caplog):
98
- monkeypatch.setitem(
99
- settings.API_VERSION_LIFECYCLE,
100
- 2,
101
- {"status": "deprecated", "sunset": "not-a-date"},
102
- )
103
- middleware = LifecycleMiddleware(lambda req: HttpResponse("ok"))
104
- response = middleware(_request("2"))
105
-
106
- assert response["Deprecation"] == "true"
107
- assert "Invalid sunset date" in caplog.text