axnsan12 / drf-yasg

Automated generation of real Swagger/OpenAPI 2.0 schemas from Django REST Framework code.
https://drf-yasg.readthedocs.io/en/stable/
Other
3.42k stars 440 forks source link

Hashid field type incorrect (int instead of str) in the path parameters #774

Open ayushin opened 2 years ago

ayushin commented 2 years ago

This is related https://github.com/axnsan12/drf-yasg/issues/383

However this requires overriding the entire generator get_path_parameters method in its entirety just to be able to do:

            if isinstance(model_field, HashidFieldMixin):
                attrs = {'type': openapi.TYPE_STRING}
            else:

While I agree HashidField implementation is somewhere incorrect, it would be great to have something like SWAGGER_BASIC_TYPE_INFO_OVERRIDES so that get_basic_type_info would first look there.

Then an easy solution would be:

SWAGGER_BASIC_TYPE_OVERRIDES=[
    (HashidFieldMixin, (openapi.TYPE_STRING, None)),

]

Any other ideas how to fix this easier?

ayushin commented 2 years ago

Alternatively, somewhere in your project?

from drf_yasg import openapi
from drf_yasg.inspectors.field import model_field_to_basic_type
from hashid_field.field import HashidFieldMixin

model_field_to_basic_type.insert(
    0, (HashidFieldMixin, (openapi.TYPE_STRING, None)),
)