Skip to content

Conversation

@dunglas
Copy link
Member

@dunglas dunglas commented Jun 25, 2016

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #583
License MIT
Doc PR todo

Unit tests must be fixed.

@dunglas dunglas force-pushed the deserializer_view_listener branch from 8611730 to 77c46ac Compare June 25, 2016 13:26
@dunglas dunglas changed the title [WIP] Deserialize in a view listener Deserialize in a view listener Jun 25, 2016
@dunglas
Copy link
Member Author

dunglas commented Jun 25, 2016

ping @api-platform/core-team

I took advantage of this PR to transform the get ride of one more trait (@theofidry and @sroze will be happy) and to improve the unit test coverage.

@theofidry
Copy link
Contributor

I could only peak a glance for now but looks like a good refactoring 👍👍👍

@Simperfit
Copy link
Contributor

👍

namespace ApiPlatform\Core\Action;

/**
* Empty action. Useful to trigger the kernel.view event without doing anything specific in the action
Copy link
Contributor

@teohhanhui teohhanhui Jun 27, 2016

Choose a reason for hiding this comment

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

I don't see the value of having this. It's far too easy to create an empty action, so there's no benefit of reuse here. This should just be PostCollectionAction, as before... And we will be free to change it maybe to do something later on without the name tying it down.

Copy link
Member Author

Choose a reason for hiding this comment

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

It allows to create custom operations triggering the event system with only config. It's why I've renamed it. Why not doing it now?

Copy link
Contributor

Choose a reason for hiding this comment

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

Why not go all the way and remove all other actions? 😆

So we will end up with an event listener which fetches data from the data provider, and another which performs the deserialization (if applicable).

Copy link
Member Author

Choose a reason for hiding this comment

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

What is the benefit of getting data from a listener (which is triggered at every request) instead of in an action?

Copy link
Contributor

Choose a reason for hiding this comment

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

You're right. We should probably not try to fight against how Symfony works.

Copy link
Contributor

Choose a reason for hiding this comment

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

The benefit is turtles all the way (kind of... at least, events all the way). And we sidestep the confusion of what the controller is supposed to do by consistently leaving it out of the picture (as a no-op).

Copy link
Member Author

Choose a reason for hiding this comment

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

Can we try that in another PR?

@teohhanhui
Copy link
Contributor

teohhanhui commented Jun 27, 2016

@dunglas Just realized we do not need to change this to achieve the use case in #583 (comment).

Just create a custom normalizer and call the voter from DenormalizerInterface::denormalize before denormalization:

if (isset($context['object_to_populate'])) {
    if (!$this->authorizationChecker->isGranted('EDIT', $context['object_to_populate'])) {
        throw new AccessDeniedException();
    }
}

$context['object_to_populate'] = $data;
}

$event->setControllerResult($this->serializer->deserialize($content, $resourceClass, $format, $context));
Copy link
Contributor

Choose a reason for hiding this comment

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

What prevents this from being run on safe methods (e.g. GET)?

Copy link
Member Author

Choose a reason for hiding this comment

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

if ($data instanceof Response || '' === $content) {

(In the first implementation I was listing non-safe methods but this trick looks more extensible to me).

Copy link
Contributor

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 make such an assumption, especially when this Symfony test was reverted so we're basically on shaky ground: symfony/symfony@58fcb8d

@dunglas
Copy link
Member Author

dunglas commented Jun 27, 2016

@teohhanhui right but IMO it's not the responsibility of a denormalizer to call the security component (its responsibility is only to hydrate an object from a document). I think this PR provides a better extension point. WDYT?

@teohhanhui
Copy link
Contributor

@dunglas

it's not the responsibility of a denormalizer to call the security component

Agreed.

So I suppose point no. 2 in #583 (comment) is bad too... But let's continue that discussion in #583

@dunglas dunglas force-pushed the deserializer_view_listener branch from 44a46e6 to 100cabd Compare July 1, 2016 17:18
@dunglas dunglas merged commit 1150b69 into master Jul 1, 2016
@dunglas dunglas mentioned this pull request Jul 4, 2016
2 tasks
@dunglas dunglas deleted the deserializer_view_listener branch July 15, 2016 09:55
magarzon pushed a commit to magarzon/core that referenced this pull request Feb 12, 2017
…ew_listener

Deserialize in a view listener
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.

5 participants