-
Notifications
You must be signed in to change notification settings - Fork 770
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
Add URLFilter for URLField. #1479
base: main
Are you sure you want to change the base?
Conversation
@tfranzel can I ask you to comment? (I guess I don't mind adding a URLFilter but... 🤷♂️) |
hey! sure @carltongibson, hope you doing fine. this actually has a bit of history behind it.
in the beginning we went all out with the model introspection because we simply had the code available, sometimes doing a better job than people wanted/anticipated. that is why we now stop at
not entirely true @jeking3, but it is a bit awkward. the test do this like so (for you of course now when I think about it, allowing a bit more flexibility on TLDR: It can't hurt adding the standard field types that DRF provides anyway. on our side, adding the field would be a one-liner. |
@tfranzel I had to "type: ignore" that one to make it work, after I made my own local URLFilter based on URLField. Already got this working locally so thought I would enhance filters then spectacular, so we could have more spectacular filters and API specs. :) Love both projects. |
@tfranzel consider perhaps allowing |
So does the example here work (when adapted for
? This seems like a general problem:
And I'm not sure adding a filter for every model field is the way forward 🤔 Taking away the schema generation question, a potential |
that sounds like a good idea. i'll look into it.
yes, it does work!
can't really judge this, but since django-filters is very tightly coupled to the ORM, it wouldn't be the craziest idea to support all Django-native model fields I suppose.
I think of it as a collaborative effort to help people getting most out of their filters with correct schema generation. if I could solve it I would, but due to the way |
URLFilter which leverages URLField uses a URLValidator that ensures the content is a valid URL, so it is a stronger validation than using a CharFilter. Additionally, the separate type allows downstream code generators to annotate the format of the string field properly. Having it would be a similar reason as to why UUIDFilter exists already |
@jeking3 I totally agree. Imho the added validation makes django-filter more robust while also having a more specific interface. Having that info downstream is just a nice perk that helps introspection. So the specificity should be beneficial here regardless of what we do downstream. |
Now, that is more down my line of concern... 😜 I'm worried this is a breaking change for folks expecting a Let me ponder a while. It's not a No. 😄 |
Here is one possible way to do that with a URLFilter. It could also be done with a CharFilter, however the downstream intention is lost unless you decorate things for downstream tools; URLFilter (like UUIDFilter) allows downstream tools to discern a string from a url (or a uuid) format as the intended input. At least for drf-spectacular, adding URLFilter would cause no change to its behavior as it falls back to other methods to determine the intention, ending up at
|
Problem
Consider this fragment:
With drf_spectacular emitting a string type out, we lose the uri formatting designation. Due to the way drf_spectacular works, once it matches
CharFilter
(see: code) then it assumes a standard OpenApiTypes.STR type. There is no way to annotate your way around it due to the logic there.Resulting generated web client with swagger-typescript-api shows:
Solution
Resulting generated web client with swagger-typescript-api shows: