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

Function similar to COMPONENT_SPLIT_REQUEST but for responses? #912

Closed
tsepulvedacaroca01 opened this issue Jan 6, 2023 · 3 comments
Closed

Comments

@tsepulvedacaroca01
Copy link

tsepulvedacaroca01 commented Jan 6, 2023

I have tried to find out if there is a way to generate a different component for each response from my endpoints, since I have implemented the dynamic fields of the serializers (https://www.django-rest-framework.org/api-guide/serializers/#example). My problem occurs when a serializer is reused and the component always gives the same response, but it must change based on the fields to be used in the serializer.

My PermissionSerializer uses the RoleSerializer and should display the id and name fields as shown in the image and there are no problems with this.

image

Instead my main endpoint for my RoleSerializer should display all the fields but it doesn't because it should be using the record stored by the RoleSerializer.

image

Removing the PermissionSerializer endpoint documentation found the RoleSerializer documentation correct.

image

It should be noted that the serializer used by PermissionSerializer is the same as RoleSerializer, only that the fields are filtered through the fields attribute as shown in the link provided above.

class RoleSerializer(DynamicFieldsModelSerializer):
    class Meta:
        model = Role
        fields = (
            'id',
            'name',
            'description',
            'order',
            'administrator',
            'permissions',
            'groups'
        )

class PermissionSerializer(DynamicFieldsModelSerializer):
    roles = RoleSerializer(fields=['id', 'name'])
    class Meta:
        model = Permission
        fields = (
            'id',
            'app',
            'name',
            'display_name',
            'description',
            'order',
            'roles'
        )

I hope to explain my question and problem in detail

@tfranzel
Copy link
Owner

tfranzel commented Jan 6, 2023

Ok, so this is completely unrelated to COMPONENT_SPLIT_REQUEST. This setting already means one component for request and one component for response.

This is basically a duplicate of #375 (comment)

The expectation is that serializers don't change their form under the same class (name). We have not attempted to solve this as there is no optimal solution. You can however fix this with a OpenApiSerializerExtension

Add another arg for name to DynamicFieldsModelSerializer and use that in OpenApiSerializerExtension.get_name. This should resolve the comonent naming conflict.

tfranzel added a commit that referenced this issue Jan 6, 2023
@tfranzel
Copy link
Owner

tfranzel commented Jan 6, 2023

since this has been asked before and writing that extension is non-trivial, I added a test/example. please refer to this commit on how to solve the issue.

@tsepulvedacaroca01
Copy link
Author

Thanks for the help!

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

2 participants