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

JSONRenderer does not extract includes from PolymorphicModelSerializer properly #1190

Open
dmuseychuk-invgate opened this issue Dec 5, 2023 · 2 comments
Labels

Comments

@dmuseychuk-invgate
Copy link

When using PolymorphicModelSerializer with JSONRenderer, the extract_included method is retrieving the base polymorphic serializer instead of the specific serializer corresponding to the object instance. This results in the includes not being processed correctly, as the base polymorphic serializer is not equipped to handle the specifics of each derived instance.

It appears that the issue can be resolved by modifying the render method to correctly identify and use the appropriate serializer based on the object instance during the rendering process. This adjustment should enable includes to function as expected with polymorphic serializers.

if serializer is not None:
    # Extract root meta for any type of serializer
    json_api_meta.update(self.extract_root_meta(serializer, serializer_data))
    if getattr(serializer, "many", False):
        ...
    else:
        if isinstance(serializer, PolymorphicModelSerializer):
            resource_instance = serializer.instance
            serializer_class = serializer.get_polymorphic_serializer_for_instance(resource_instance)
            serializer = serializer_class(resource_instance, context=serializer.context)

            fields = utils.get_serializer_fields(serializer)
            ...
@sliverc
Copy link
Member

sliverc commented Dec 11, 2023

Thanks for your report. This certainly seems to be an issue. I think your proposed fixed will only work partially, though, as the default included resource will still not work.

In any case, I am very surprised that this issue only comes up now, and I am wondering how many people use Django Polymorphic and DJA together. The thing is that the integration of Django Polymorphic has not been done very well and the implementation is cluttered all over the code base and the maintainability of DJA suffered because of it. In general, Django Polymorphic is an optional feature and should not affect other users (e.g. the code path should not change for normal use because of it), but how it is implemented now it certainly does.

Before we jump into fixing this, I will start a discussion on how we should continue with Django Polymorphic support.

@sliverc
Copy link
Member

sliverc commented May 8, 2024

I have noticed that I talked about starting a discussion on Polymorphic support within DJA, but actually never referenced it here.

So the discussion can be found at #1194 and any feedback and use cases what it is used for are very welcome.

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

No branches or pull requests

2 participants