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

Custom django path's param convert type feature #502

Closed
jtamm-red opened this issue Sep 6, 2021 · 4 comments
Closed

Custom django path's param convert type feature #502

jtamm-red opened this issue Sep 6, 2021 · 4 comments
Labels
enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@jtamm-red
Copy link
Contributor

Custom param convert is very useful. Example it is possible to convert from string to date.

I tried to use decorator extend_schema_field. It is not working.

from drf_spectacular.utils import extend_schema_field

@extend_schema_field({'type': 'integer'})
class NegativeIntConverter:
    regex = '-\d+'

    def to_python(self, value) -> int:
        return int(value)

    def to_url(self, value):
        return '%d' % value


from django.urls import path, include, register_converter
register_converter(NegativeIntConverter, 'negint')

urlpatterns = [
    path('calc/<negint:value>', get_calc)
]

I received warning log:
could not derive type of path parameter "value" because because it is untyped and obtaining queryset from the viewset failed. Consider adding a type to the path (e.g. <int:value>) or annotating the parameter type with @extend_schema. Defaulting to "string".

@tfranzel
Copy link
Owner

tfranzel commented Sep 6, 2021

hi! extend_schema_field does not apply here as it is not a field. at the moment we only support the built-in types:

DJANGO_PATH_CONVERTER_MAPPING = {

however this is an interesting feature request. parsing the type hint is likely too limiting and introducing a new decorator only for this purpose would probably be overkill.

either a new setting or adding a schema attr on the converter. unsure atm what the cleanest solution is.

@jtamm-red
Copy link
Contributor Author

I will try solve this solution and make PR.

jtamm-red added a commit to jtamm-red/drf-spectacular that referenced this issue Sep 7, 2021
@tfranzel
Copy link
Owner

hey @jtamm-red, thank you for the PR. i decided to go a slightly different route as outlined in my comment. the new approach has a bit more flexibility and does not encourage using extend_schema_field where it is semantically not correct. also it is possible now to change existing converters with little effort.

i solved this as part of a bigger refactoring, so it was easier to rewrite it myself. thank you however for the pointers and input!

the new config is called PATH_CONVERTER_OVERRIDES. if no override is found, spectacular defaults to the regex that is used by the converter.

@tfranzel tfranzel added enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending labels Sep 19, 2021
@tfranzel
Copy link
Owner

closing this issue for now. feel free to comment if anything is missing or not working and we will follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending
Projects
None yet
Development

No branches or pull requests

2 participants