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

Set Serialization Groups Manually [feature] #1610

Open
tebaly opened this issue Aug 1, 2020 · 5 comments
Open

Set Serialization Groups Manually [feature] #1610

tebaly opened this issue Aug 1, 2020 · 5 comments

Comments

@tebaly
Copy link

@tebaly tebaly commented Aug 1, 2020

Description

Make it possible to define the serialization group through the request attributes.
Something like this:

Example

https://github.com/api-platform/core/blob/22ce6399180f9408f19b0b3c857317d2ddd7ea9d/src/Serializer/SerializerContextBuilder.php#L60

        $context = $resourceMetadata->getTypedOperationAttribute($operationType, $attributes[$operationKey], $key, [], true);
        if ($context and $context['groups']) {
            $serializerContext = $request->attributes->get('_api_serializer_context', []);
            $context = array_merge_recursive($context, $serializerContext);
        }
@dunglas
Copy link
Member

@dunglas dunglas commented Aug 2, 2020

You can already do this easily by implementing the SerializerContextBuilderInterface: https://api-platform.com/docs/core/serialization/#changing-the-serialization-context-dynamically

Also, please note that using request attributes will not work when using GraphQL (because a single HTTP request can contain many GraphQL request).

Can you tell us a bit more about your use case?

@tebaly
Copy link
Author

@tebaly tebaly commented Aug 2, 2020

I didn't like your "way". Now I'm trying to make interceptors.
api-platform/core#3624


Then I can make a group just like that (by Intercepting the request)

$user = $this->security->getUser();
if ($user and ($this->security->isGranted('ROLE_ADMIN'))) {
    // $key '_api_normalization_context' or  '_api_serializer_context'
    $context = $request->attributes->get($key);
    if (!$context) {
        $context = [];
    }
    $context['groups'][] = 'admin';
    $context['groups'] = array_unique($context['groups']);
    $request->attributes->set($key, $context);
}
@dunglas
Copy link
Member

@dunglas dunglas commented Aug 3, 2020

You can do it as easily by doing as in the example I provided isn't it? That being said I'm not against supporting such attribute (especially because it will be a very tiny patch to support it).

@tebaly
Copy link
Author

@tebaly tebaly commented Aug 3, 2020

I don't understand well how it works. I did an ugly hack on my fork.
https://github.com/tebaly/core/blob/95d4b1cc9b57bd1debcea7387c2af6efdee6cf28/src/Serializer/SerializerContextBuilder.php#L60

If you do it right. And it will work as well. I will grateful to you.

@tebaly
Copy link
Author

@tebaly tebaly commented Aug 3, 2020

I'm not sure. Combining REST and GraphQL will end well. I think they are too different to combine. One will limit the other. Undoubtedly.

  • api-rest (rest-platform)
  • api-graphql (graphql-platform)

It is possible to leave common components as a dependency for both (api-core or something else).

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

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.