drf-spectacular icon indicating copy to clipboard operation
drf-spectacular copied to clipboard

Ability to access the direction and auto_schema in MarshmallowSerializerExtension.get_name method

Open mohannad-musleh opened this issue 3 years ago • 5 comments

is it possible to get the direction and auto_schema (or at least the ViewSet class) in MarshmallowSerializerExtension get_name methods?

I need the direction and some properties from ViewSet class to generate the name correctly. (and in some cases to solve the naming conflict issue)

mohannad-musleh avatar May 19 '21 06:05 mohannad-musleh

the fix should be easy. problem is that this breaks everybody using get_name in an extension. i failed to include a **kwargs in the beginning so that its forward compatible. i'm undecided whether the reward outweighs the risk here.

the SerializerFieldExtension.get_name is rather special but SerializerExtension.get_name might hit more users.

tfranzel avatar May 20 '21 09:05 tfranzel

@tfranzel thank you!

for now, as a workaround, I implemented a custom AutoSchema class with a custom implementation of _get_serializer_name and _get_response_for_code methods to inject/pass the data I need

mohannad-musleh avatar May 20 '21 10:05 mohannad-musleh

awesome! i'll leave this open to gather some support from other users. if this becomes a more common pain point we can look at it again.

in the meantime i should add kwargs to future-proof this for new user from here on out.

tfranzel avatar May 22 '21 14:05 tfranzel

so I just hit this problem without a proper way to achieve the goal. Direction was indeed needed. I found a way to safely call legacy get_name without the new arguments. Also added auto_schema, as this might be helpful and I don't want to change it again, i.e. breaking people twice. This should be rather safe but you never know.

Even got a new feature on top of that. When this is merged, your custom AutoSchema might break. Just as a heads-up

tfranzel avatar Jul 28 '22 20:07 tfranzel

Cool!

I liked how you did the backward compatibility (by Checking the method signature), and I think the changes are safe (the changes should only affect the users with custom AutoSchema implemented - as I did -).

I think I can easily update my implementation to use the new signature of the get_name method.

Thank you!

mohannad-musleh avatar Aug 02 '22 12:08 mohannad-musleh