Skip to content

Add a filter for serialization groups#1111

Closed
meyerbaptiste wants to merge 0 commit into
api-platform:masterfrom
meyerbaptiste:add_group_filter
Closed

Add a filter for serialization groups#1111
meyerbaptiste wants to merge 0 commit into
api-platform:masterfrom
meyerbaptiste:add_group_filter

Conversation

@meyerbaptiste

@meyerbaptiste meyerbaptiste commented May 10, 2017

Copy link
Copy Markdown
Member
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #1082
License MIT
Doc PR N/A

@meyerbaptiste
meyerbaptiste force-pushed the add_group_filter branch 3 times, most recently from 1af71a4 to 9694e58 Compare May 10, 2017 15:56
Comment thread src/Serializer/Filter/GroupFilter.php Outdated
*
* @author Baptiste Meyer <baptiste@les-tilleuls.coop>
*/
class GroupFilter implements FilterInterface

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final

@meyerbaptiste meyerbaptiste May 11, 2017

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed the same standards as ORM filters: not final + protected methods/attributes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So @dunglas, should we keep consistency with ORM filters or should I follow your requested changes?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ORM filters support only the array syntax. So, same for this new one.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I talk about final + protected @dunglas 😋

Comment thread src/Serializer/Filter/GroupFilter.php Outdated
*/
class GroupFilter implements FilterInterface
{
protected $overrideDefaultGroups;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private

Comment thread src/Serializer/Filter/GroupFilter.php Outdated
*/
public function apply(Request $request, bool $normalization, array $attributes, array &$context)
{
if (empty($groups = $this->extractGroups($request))) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty is useless.

Comment thread src/Serializer/Filter/GroupFilter.php Outdated
return;
}

if (!$this->overrideDefaultGroups && !empty($context['groups'])) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Comment thread src/Serializer/Filter/GroupFilter.php Outdated
*
* @return array
*/
protected function extractGroups(Request $request): array

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private

Comment thread src/Serializer/Filter/GroupFilter.php Outdated
$groups = [];

if (null !== $groupsParameter = $request->query->get($this->parameterName)) {
if (is_string($groupsParameter)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why supporting both syntax? We only support the second one in other filters.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should explode or do any transformation on query parameters. Just let symfony do the work here.

I'd propose:

$groups = $request->query->get($this->parameterName);

This method can then be removed as this one line should be enough.

@meyerbaptiste meyerbaptiste May 11, 2017

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed what you described in your issue: ?groups=foo,bar,baz and I added the array syntax. So... should I implement the issue syntax or array syntax or both?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After a second though, I was wrong :) We should just support the array syntax, just like for other fields (consistency...).

$resourceFilters = $resourceMetadata->getItemOperationAttribute($attributes['item_operation_name'], 'filters', [], true);
}

if (empty($resourceFilters)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty is useless

@meyerbaptiste
meyerbaptiste force-pushed the add_group_filter branch 3 times, most recently from 9caf397 to abe708b Compare May 11, 2017 12:41
Comment thread src/Serializer/Filter/GroupFilter.php Outdated
return;
}

if (!$this->overrideDefaultGroups && isset($context['groups']) && is_array($context['groups'])) {

@meyerbaptiste meyerbaptiste May 11, 2017

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About is_array($context['groups']), I'm not really sure how we should handle the case where $context['groups'] exists and is not an array:

  • cast $context['groups'] to array and apply the filter
  • do not apply the filter when $overrideDefaultGroups is true or false (so in all cases)
  • apply the filter when $overrideDefaultGroups is true
  • apply the filter and overwrite the value of $context['groups'] in all cases (current behavior)

WDYT?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is $context['groups'] if not an array? I think we should cast is as one.

@meyerbaptiste meyerbaptiste May 12, 2017

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is $context['groups'] if not an array?

I don't know, it's just an assumption!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a string, therefore we could cast it as array.

*
* @author Baptiste Meyer <baptiste.meyer@gmail.com>
*/
final class SerializerContextBuilderFilter implements SerializerContextBuilderInterface

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SerializerFilterContextBuilder?

@teohhanhui

Copy link
Copy Markdown
Contributor

To restate what I've raised in #1082 (comment), I think we should not introduce misfeatures like this...

@teohhanhui

teohhanhui commented May 15, 2017

Copy link
Copy Markdown
Contributor

If we are to have a "property group" filter, then the "property groups" MUST be exposed as resources. That's the way to keep things as hypermedia.

We need to always keep these principles in mind, as we claim that API Platform is a hypermedia framework / library. 😄

@teohhanhui teohhanhui left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take #1111 (comment) into account. Which also means this needs to be generalized into a "property groups" filter, not a "serializer groups" filter (implementation detail).

@dunglas

dunglas commented May 17, 2017

Copy link
Copy Markdown
Member

@teohhanhui I'm not sure to get exactly what you have in mind? Can you give us some details?
For me, this filter is a purely technical feature to save bandwidth. It's one of the most wanted feature by our customers and I often see projects having this kind of filters (as custom filters).

It's also a 100% optin filter.

@dunglas

dunglas commented May 17, 2017

Copy link
Copy Markdown
Member

Regarding the implementation details leak, this is not true. As stated just before, filter is 100% optional and must be enabled manually by the developper. When doing this, you know that the groups will be exposed publicly, and you need to take care of this. It's the developper responsibility.

@meyerbaptiste
meyerbaptiste force-pushed the add_group_filter branch 3 times, most recently from 61b97bb to cb4d458 Compare May 18, 2017 09:19
@teohhanhui

Copy link
Copy Markdown
Contributor

Yeah, I guess it must be very hard to expose the property groups as resources.

But I wish we could change "groups" to "property_groups"?

@teohhanhui

Copy link
Copy Markdown
Contributor

When I said it's a leak of implementation detail, I meant that these groups are not defined anywhere. They are not exposed as resources, they are not in the Swagger doc. So they're magical as far as the client is concerned... Out-of-band information which is a big nono.

@dunglas

dunglas commented May 18, 2017

Copy link
Copy Markdown
Member

I agree about the out of band information. However this is not the case the fields filter, so it should be privileged when developing hypermedia API.

About the name, I prefer groups (shorter is better), but it should be configurable, and it is :)

@meyerbaptiste
meyerbaptiste force-pushed the add_group_filter branch 4 times, most recently from b2df66a to 3d71742 Compare May 22, 2017 13:26
@meyerbaptiste
meyerbaptiste deleted the add_group_filter branch May 23, 2017 08:42
@meyerbaptiste

Copy link
Copy Markdown
Member Author

Merged with #1123, see e3c17c8.

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

Successfully merging this pull request may close these issues.

4 participants