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

fix non-translated enum override hash #1198 #1217

Merged
merged 1 commit into from
Apr 1, 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/plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
)
from django.utils.functional import Promise, cached_property
from django.utils.module_loading import import_string
from django.utils.translation import get_language
from django.utils.translation import gettext_lazy as _
from rest_framework import exceptions, fields, mixins, serializers, versioning
from rest_framework.compat import unicode_http_header
Expand Down Expand Up @@ -859,8 +860,12 @@ def deep_import_string(string: str) -> Any:
pass


@cache
def load_enum_name_overrides():
return _load_enum_name_overrides(get_language())


@functools.lru_cache()
def _load_enum_name_overrides(language: str):
overrides = {}
for name, choices in spectacular_settings.ENUM_NAME_OVERRIDES.items():
if isinstance(choices, str):
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ def clear_generator_settings():

@pytest.fixture()
def clear_caches():
from drf_spectacular.plumbing import get_openapi_type_mapping, load_enum_name_overrides
load_enum_name_overrides.cache_clear()
from drf_spectacular.plumbing import _load_enum_name_overrides, get_openapi_type_mapping
_load_enum_name_overrides.cache_clear()
get_openapi_type_mapping.cache_clear()
yield
load_enum_name_overrides.cache_clear()
_load_enum_name_overrides.cache_clear()
get_openapi_type_mapping.cache_clear()


Expand Down
Binary file modified tests/locale/de/LC_MESSAGES/django.mo
Binary file not shown.
8 changes: 7 additions & 1 deletion tests/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ msgid "Internationalization"
msgstr "Internätiönalisierung"

msgid "Internationalizations"
msgstr "Internätiönalisierungen"
msgstr "Internätiönalisierungen"

msgid "Car"
msgstr "Auto"

msgid "Bicycle"
msgstr "Fahrrad"
18 changes: 18 additions & 0 deletions tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
from tests import assert_schema, generate_schema

TRANSPORT_CHOICES = (
('car', _('Car')),
('bicycle', _('Bicycle')),
)


class I18nModel(models.Model):
field_str = models.TextField()
field_choice = models.CharField(max_length=10, choices=TRANSPORT_CHOICES)

class Meta:
verbose_name = _("Internationalization")
Expand Down Expand Up @@ -90,3 +96,15 @@ def test_i18n_schema(no_warnings, url, header, translated):
def test_i18n_schema_ui(no_warnings):
response = APIClient().get('/api/schema/swagger-ui/?lang=de')
assert b'/api/schema/?lang\\u003Dde' in response.content


@mock.patch('drf_spectacular.settings.spectacular_settings.ENUM_NAME_OVERRIDES', {
'SpecialLanguageEnum': TRANSPORT_CHOICES
})
@pytest.mark.urls(__name__)
def test_lazily_translated_enum_overrides(no_warnings, clear_caches):
schema_de = yaml.load(APIClient().get('/api/schema/?lang=de').content, Loader=yaml.SafeLoader)
schema_en = yaml.load(APIClient().get('/api/schema/').content, Loader=yaml.SafeLoader)

assert 'SpecialLanguageEnum' in schema_de['components']['schemas']
assert 'SpecialLanguageEnum' in schema_en['components']['schemas']
11 changes: 11 additions & 0 deletions tests/test_i18n.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ paths:
description: ''
components:
schemas:
FieldChoiceEnum:
enum:
- car
- bicycle
type: string
description: |-
* `car` - Auto
* `bicycle` - Fahrrad
X:
type: object
properties:
Expand All @@ -131,7 +139,10 @@ components:
readOnly: true
field_str:
type: string
field_choice:
$ref: '#/components/schemas/FieldChoiceEnum'
required:
- field_choice
- field_str
- id
securitySchemes:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
TextChoices = object # type: ignore # django < 3.0 handling
IntegerChoices = object # type: ignore # django < 3.0 handling

from drf_spectacular.plumbing import list_hash, load_enum_name_overrides
from drf_spectacular.plumbing import _load_enum_name_overrides, list_hash, load_enum_name_overrides
from drf_spectacular.utils import OpenApiParameter, extend_schema
from tests import assert_schema, generate_schema

Expand Down Expand Up @@ -264,7 +264,7 @@ def test_enum_override_variations(no_warnings):
'drf_spectacular.settings.spectacular_settings.ENUM_NAME_OVERRIDES',
{'LanguageEnum': f'tests.test_postprocessing.{variation}'}
):
load_enum_name_overrides.cache_clear()
_load_enum_name_overrides.cache_clear()
assert list_hash(expected_hashed_keys) in load_enum_name_overrides()


Expand All @@ -286,7 +286,7 @@ def test_enum_override_variations_with_blank_and_null(no_warnings):
'drf_spectacular.settings.spectacular_settings.ENUM_NAME_OVERRIDES',
{'LanguageEnum': f'tests.test_postprocessing.{variation}'}
):
load_enum_name_overrides.cache_clear()
_load_enum_name_overrides.cache_clear()
# Should match after None and blank strings are removed
assert list_hash(expected_hashed_keys) in load_enum_name_overrides()

Expand Down
Loading