Merge pull request #1589 from nelmio/FOSRestkey

Use FOSRest *Param::$key when defined
This commit is contained in:
Guilhem Niot 2020-02-21 11:00:04 +01:00 committed by GitHub
commit f5fa7d751a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 4 deletions

View File

@ -39,8 +39,10 @@ final class FosRestDescriber implements RouteDescriberInterface
foreach ($this->getOperations($api, $route) as $operation) { foreach ($this->getOperations($api, $route) as $operation) {
foreach ($annotations as $annotation) { foreach ($annotations as $annotation) {
$parameterName = $annotation->key ?? $annotation->getName(); // the key used by fosrest
if ($annotation instanceof QueryParam) { if ($annotation instanceof QueryParam) {
$name = $annotation->getName().($annotation->map ? '[]' : ''); $name = $parameterName.($annotation->map ? '[]' : '');
$parameter = $operation->getParameters()->get($name, 'query'); $parameter = $operation->getParameters()->get($name, 'query');
$parameter->setAllowEmptyValue($annotation->nullable && $annotation->allowBlank); $parameter->setAllowEmptyValue($annotation->nullable && $annotation->allowBlank);
@ -48,11 +50,11 @@ final class FosRestDescriber implements RouteDescriberInterface
} else { } else {
$body = $operation->getParameters()->get('body', 'body')->getSchema(); $body = $operation->getParameters()->get('body', 'body')->getSchema();
$body->setType('object'); $body->setType('object');
$parameter = $body->getProperties()->get($annotation->getName()); $parameter = $body->getProperties()->get($parameterName);
if (!$annotation->nullable && $annotation->strict) { if (!$annotation->nullable && $annotation->strict) {
$requiredParameters = $body->getRequired(); $requiredParameters = $body->getRequired();
$requiredParameters[] = $annotation->getName(); $requiredParameters[] = $parameterName;
$body->setRequired(array_values(array_unique($requiredParameters))); $body->setRequired(array_values(array_unique($requiredParameters)));
} }

View File

@ -25,7 +25,7 @@ class FOSRestController
/** /**
* @Route("/fosrest.{_format}", methods={"POST"}) * @Route("/fosrest.{_format}", methods={"POST"})
* @QueryParam(name="foo", requirements=@Regex("/^\d+$/")) * @QueryParam(name="foo", requirements=@Regex("/^\d+$/"))
* @RequestParam(name="bar", requirements="\d+") * @RequestParam(name="Barraa", key="bar", requirements="\d+")
* @RequestParam(name="baz", requirements=@IsTrue) * @RequestParam(name="baz", requirements=@IsTrue)
*/ */
public function fosrestAction() public function fosrestAction()