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

[django-filter] Filter Data Type / Description Issues #1267

Open
SorianoMarmol opened this issue Jul 29, 2024 · 2 comments
Open

[django-filter] Filter Data Type / Description Issues #1267

SorianoMarmol opened this issue Jul 29, 2024 · 2 comments

Comments

@SorianoMarmol
Copy link

SorianoMarmol commented Jul 29, 2024

Hello, first of all, thank you for your work and effort. It’s an incredible library.

Describe the bug

I’m having two problems with filters:

  1. When I define a filter as “integer,” I can’t get it to appear as an integer in the schema (it appears as "number"). Could you guide me on where I should indicate this, please? (type hinting, extend_schema_field...)
class PositiveIntegerFilter(filters.NumberFilter):
    field_class = forms.IntegerField

I’ve tried in many places but haven’t been able to achieve it.
I should note that I am applying the filter in a custom way, meaning it doesn’t apply to a specific field of an object (it's a filter for a list that has nothing to do with objects). Maybe that’s why it can’t identify the integer type through model introspection (that’s my theory).

  1. If I specify my filters adding extend_schema_field(OpenApiTypes.XXX), the help_text doesn’t transfer to the description (it gets lost in the schema).
    Is this a bug? Any way to propagate the help_text to the description?

Both problems are related for me because I can’t specify the integer type with extend_schema_field since I lose the description 😆 .

Example:

    asset_currency = extend_schema_field(OpenApiTypes.STR)(filters.ModelChoiceFilter(
        help_text=_('Filter by the currency code of the asset')
    ))

Result (without description) :

                    {
                        "in": "query",
                        "name": "asset_currency",
                        "schema": {
                            "type": "string"
                        }

To Reproduce

Define a filter with help_text like this

    asset_currency = extend_schema_field(OpenApiTypes.STR)(filters.ModelChoiceFilter(
        help_text=_('Filter by the currency code of the asset')
    ))

Expected behavior

expected result (with description=help_text):

                    {
                        "in": "query",
                        "name": "asset_currency",
                        "schema": {
                            "type": "string"
                        },
                        "description": "Filter by the currency code of the asset"
                    },

Thanks in advance

@SorianoMarmol SorianoMarmol changed the title Issues with filters: types/description Filter Data Type / Description Issues Jul 29, 2024
@SorianoMarmol SorianoMarmol changed the title Filter Data Type / Description Issues [django-filter] Filter Data Type / Description Issues Jul 29, 2024
@tfranzel
Copy link
Owner

Hi and thx.

So regarding point 2: This is by design as you take control of the field schema when using extend_schema_field. Mixing together the detection with the override just creates confusion. Also imagine the case where you try to remove the help_text, which would be very hard when mixing.

Regarding 1: We basically chose to route to ignore the field_class and use the estimated model field instead. If I remember correctly, we did that because the form field_class often did not provide enough information by itself and using both sources seemed quite awkward and opaque.

schema = build_basic_type(OpenApiTypes.NUMBER)

I can see that this is not optimal for your use-case, but we had to make some concessions here.

@keikoro
Copy link

keikoro commented Aug 21, 2024

Re: point 2 as well as e.g. #1257:

Could the reason for people bringing this up be that extend_schema_field's name is misleading? I personally would assume that something which "extends" something else adds to it rather than replace it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants