FOSRestBundle: routes and annotations for parameters
Asked Answered
M

2

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?

Ment answered 23/11, 2013 at 11:25 Comment(0)
F
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)
 {
    // ...
 }
Fortuitism answered 23/11, 2013 at 11:33 Comment(2)
nifr, thanks for your help! The only thing doesn't look nice is that single parameter is described twice, in @Get and @ApiDoc.Ment
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 trickDelafuente
B
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"}
  *  }
  * )
  */
Breeches answered 29/1, 2015 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.