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

Add option to provide a callable for PolymorphicProxySerializer.serializers #953

Conversation

glennmatthews
Copy link
Contributor

Adding a minor feature that we found useful in our own use of PolymorphicProxySerializer - the ability to initialize it with a callable (such as a lambda) that returns a list/dict of serializers when evaluated, rather than needing to provide the list of serializers up front at declaration time.

In our case this was useful because we have a polymorphic schema method field that can return a serializer corresponding to any subclass of a given abstract base model, not all of which may already be defined/imported at the time the field is declared. Being able to provide a callback that can be deferred until the time the schema is actually generated made our implementation easier.

@codecov
Copy link

codecov bot commented Mar 3, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.01 🎉

Comparison is base (eeb8a11) 98.61% compared to head (2828132) 98.63%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #953      +/-   ##
==========================================
+ Coverage   98.61%   98.63%   +0.01%     
==========================================
  Files          68       68              
  Lines        8074     8110      +36     
==========================================
+ Hits         7962     7999      +37     
+ Misses        112      111       -1     
Impacted Files Coverage Δ
drf_spectacular/contrib/django_filters.py 90.44% <100.00%> (+0.73%) ⬆️
drf_spectacular/utils.py 99.57% <100.00%> (+0.01%) ⬆️
tests/contrib/test_django_filters.py 100.00% <100.00%> (ø)
tests/test_polymorphic.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@tfranzel
Copy link
Owner

tfranzel commented Mar 3, 2023

@glennmatthews, awesome stuff! That make it even more meta 😆, I like it!

Just one thought, do you think providing arguments is superfluous? (e.g. self a.k.a. instance of PolymorphicProxySerializer).

Not sure if this adds something, you have seen this pattern recurring. What do you think? Just a question, not a change request.

@tfranzel
Copy link
Owner

tfranzel commented Mar 3, 2023

Whatever, let's just merge it. We have a method to tack on optional kwargs to this callback. Still interested in your opinion though.

def filter_supported_arguments(func, **kwargs):

@tfranzel tfranzel merged commit 4c23bde into tfranzel:master Mar 3, 2023
@glennmatthews glennmatthews deleted the u/glennmatthews-polymorphicproxyserializer-enhancements branch March 3, 2023 21:04
@glennmatthews
Copy link
Contributor Author

Thus far I haven't seen any need to pass args into the callable, but I like the way you're thinking. :-) Thanks for the approval and merge!

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

Successfully merging this pull request may close these issues.

None yet

2 participants