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

Enum value from request cannot be injected directly in response serializer #339

Open
AMontagu opened this issue Jan 27, 2025 · 0 comments
Open

Comments

@AMontagu
Copy link
Collaborator

This is an edge case but it is actually happening.

I created a branch name issue/requestDataCannotBeInjectedDirectlyInSerializerData that have a test that fail because of the issue.

I could fix it but their is different way of doing it and we may not want to fix it but only document it.

The example:

@grpc_action(
        request=[{"name": "char_choices", "type": EnumModel.MyTestStrEnum}],
        response=EnumServiceSerializerOnlyOneField,
        use_generation_plugins=[InMessageWrappedEnumGenerationPlugin()],
        override_default_generation_plugins=True,
    )
    async def BasicEnumRequestInSerializerOut(self, request, context):
        data = {"char_choices": request.char_choices}
        # data = {"char_choices": EnumModel.MyTestStrEnum.values[request.char_choices - 1]}
        serializer = EnumServiceSerializerOnlyOneField(data=data)
        serializer.is_valid(raise_exception=True)

        """
        message = EnumServiceOnlyOneFieldResponse(char_choices=request.char_choices)
        return message
        """
        return serializer.message

This action fail.

When using either data = {"char_choices": EnumModel.MyTestStrEnum.values[request.char_choices - 1]} or message = EnumServiceOnlyOneFieldResponse(char_choices=request.char_choices) it fix the issue but it's a manual behavior that the user need to know and need to be documented.

An other way is to check in proto_serializers.BaseProtoSerializer.data_to_message if the data[field_name] is an integer then we do not try to map the data but it may create issue with integer enum.

It also possible to add a kwargs argument to the serializer to stop conversion for specified field.

What do you think @Giiltham @legau ? I can fix it I just need to be agree on the fix we implement

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

1 participant