9

I'm able to get GET parameters with @QueryParam() annotation, but it looks like it works for Query String data only: /user?id=123.

I'd prefer to have it like /user/123 instead. For this, I might use @Get("/user/{id}") annotation, but I don't see it has additional metadata which @QueryParam() has:

name="id", requirements="\d+", default="1", description="User id"

If I use both of the annotations, I get an error:

ParamFetcher parameter conflicts with a path parameter 'id' for route 'getone'

My conflicting docblock:

/**
 * Finds and displays a Users entity.
 *
 * @Rest\View
 * @Rest\Get("/user/{id}")
 * @Rest\QueryParam(name="id", requirements="\d+", default="1", description="User id")
 * @ApiDoc(section="Partner Users")
 * @param int $id
 * @return array
 */

PS I need to have an id in the path (/user/123), not in query, and I also need to use @QueryParam() as it's read by NelmioApiDocBundle. How may I resolve this issue?

K. Weber
  • 2,643
  • 5
  • 45
  • 77
Roman Newaza
  • 11,405
  • 11
  • 58
  • 89

2 Answers2

20

FOSRestBundle's @Get annotation extends FOSRestBundle's @Route which in turn extends SensioFrameworkExtraBundle's @Route.

Have a look at the code and see the documentation chapter @Route and @Method.

The requirements and defaults attributes expect an array.

/**
  * @Rest\View
  * @Rest\Get("/user/{id}", requirements={"id" = "\d+"}, defaults={"id" = 1})
  * @ApiDoc(
  *  description="Returns a User Object",
  *  parameters={
  *      {"name"="id", "dataType"="integer", "required"=true, "description"="User Id"}
  *  }
  * )
  */
 public function getAction($id)
 {
    // ...
 }
Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130
  • nifr, thanks for your help! The only thing doesn't look nice is that single parameter is described twice, in `@Get` and `@ApiDoc`. – Roman Newaza Nov 23 '13 at 20:56
  • 1
    If you drop the `parameters` section from the `@ApiDoc` section and add `@param integer $id Description of id here` below it that seems to do the trick – lopsided Dec 12 '14 at 19:26
1

If you want a description for requirements just do this in your annotation

   /**
  * @Rest\View
  * @Rest\Get("/user/{id}", requirements={"id" = "\d+"}, defaults={"id" = 1})
  * @ApiDoc(
  *  description="Returns a User Object",
  *  requirements={
  *      {"name"="id", "dataType"="integer", "required"=true, "description"="User Id"}
  *  }
  * )
  */
Zwen2012
  • 3,360
  • 9
  • 40
  • 67