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

fix route generator not ignoring body param converters #1934

Merged
merged 5 commits into from
Aug 21, 2018

Conversation

tonivdv
Copy link
Contributor

@tonivdv tonivdv commented Aug 21, 2018

This is an update on PR #1771 with the missing test case. All credits should go to @Parent5446

When fetching a list of function parameters from a controller action
for use as URL parameters, check for the @ParamConverter
annotation. If a parameter has the annotation with the
"fos_rest.request_body" converter, then ignore it and do not include
it in the list of parameters (thus removing it from the URL).

This allows automatic route generation and ParamConverter to co-exist,
as otherwise a parameter that is expected to be derived from the body
would also be included as a URL parameter, requiring the developer to
manually specify the route.

fixes #1198

@tonivdv
Copy link
Contributor Author

tonivdv commented Aug 21, 2018

I was not able to run the php-cs fixer. Somehow it fails on my machine :/ And there is no doc on how it should be executed. It would be cool to have a docker config for that to avoid this kind of situations ;)

@tonivdv
Copy link
Contributor Author

tonivdv commented Aug 21, 2018

ping @GuilhemN

@tonivdv tonivdv force-pushed the fix/paramconverter branch from 266617d to cde9656 Compare August 21, 2018 09:55
@tonivdv
Copy link
Contributor Author

tonivdv commented Aug 21, 2018

Fixed the CS issue with the provided diff by styleci 👍

When fetching a list of function parameters from a controller action
for use as URL parameters, check for the @ParamConverter
annotation. If a parameter has the annotation with the
"fos_rest.request_body" converter, then ignore it and do not include
it in the list of parameters (thus removing it from the URL).

This allows automatic route generation and ParamConverter to co-exist,
as otherwise a parameter that is expected to be derived from the body
would also be included as a URL parameter, requiring the developer to
manually specify the route.

fixes FriendsOfSymfony#1198
@tonivdv tonivdv force-pushed the fix/paramconverter branch from cde9656 to d9e9d2e Compare August 21, 2018 09:57
@@ -475,6 +475,17 @@ private function getMethodArguments(\ReflectionMethod $method)
// ignore all query params
$params = $this->paramReader->getParamsFromMethod($method);

// check if a parameter is coming from the request body
$ignoreParameters = [];
if (class_exists('Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\ParamConverter')) {
Copy link
Member

Choose a reason for hiding this comment

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

let's use the class constant here

$this->assertNotNull($collection->get('post_something'), 'route for "post_something" does not exist');
$this->assertEquals('/somethings.{_format}', $collection->get('post_something')->getPath());

$this->assertTrue(true);
Copy link
Member

Choose a reason for hiding this comment

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

this should be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops

$collection = $this->loadFromControllerFixture('ParamConverterController');

$this->assertNotNull($collection->get('post_something'), 'route for "post_something" does not exist');
$this->assertEquals('/somethings.{_format}', $collection->get('post_something')->getPath());
Copy link
Member

Choose a reason for hiding this comment

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

should be assertSame()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. Just copied from previous tests.

@@ -475,6 +475,17 @@ private function getMethodArguments(\ReflectionMethod $method)
// ignore all query params
$params = $this->paramReader->getParamsFromMethod($method);

// check if a parameter is coming from the request body
$ignoreParameters = [];
if (class_exists(\Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter::class)) {
Copy link
Member

@xabbuh xabbuh Aug 21, 2018

Choose a reason for hiding this comment

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

but please also add use case statements for the classes :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure I understand what you mean, can you elaborate.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I meant use statements. 🙈

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, I prefer that too, but since a lot in the code is with FQN I thought it was some standard or something. Will do that simplification.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed all FQN in that method so it's cleaned up at the same time.

@@ -500,6 +511,10 @@ private function getMethodArguments(\ReflectionMethod $method)
}
}

if (in_array($argument->getName(), $ignoreParameters)) {
Copy link
Member

Choose a reason for hiding this comment

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

we should also pass true as the third argument

\FOS\RestBundle\Request\ParamFetcherInterface::class,
\Symfony\Component\Validator\ConstraintViolationListInterface::class,
\Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter::class,
Request::class,
Copy link
Member

Choose a reason for hiding this comment

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

👍

*/
private function getMethodArguments(\ReflectionMethod $method)
private function getMethodArguments(ReflectionMethod $method)
Copy link
Member

Choose a reason for hiding this comment

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

In fact, for classes from the global namespace we follow the same approach as the Symfony code style and keep the namespaced variant. Sorry for the confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah lol. Alright no problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Member

@xabbuh xabbuh left a comment

Choose a reason for hiding this comment

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

LGTM

@GuilhemN GuilhemN merged commit 3725279 into FriendsOfSymfony:master Aug 21, 2018
@GuilhemN
Copy link
Member

Thank you @tonivdv for taking care of this!

@tonivdv
Copy link
Contributor Author

tonivdv commented Aug 22, 2018

@xabbuh @GuilhemN you plan a new fix release shortly? I noticed that releases are not done very often?!

@GuilhemN
Copy link
Member

GuilhemN commented Aug 22, 2018

We usually release when there are a significant number of bug fixes/features awaiting (see 2.3.1...master). I guess we can release 2.4 though, important fixes are awaiting right now. Is it ok for you @xabbuh?

@xabbuh
Copy link
Member

xabbuh commented Aug 22, 2018

👍 let's go for it

@GuilhemN
Copy link
Member

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.

Make a better marriage between body and param converter
3 participants