symfony-jsonapi icon indicating copy to clipboard operation
symfony-jsonapi copied to clipboard

Mapping self url does not works

Open regniblod opened this issue 7 years ago • 1 comments

I try to serialise an object but I get an error with the URL generation for the self url property.

This is my mapping:

mapping:
  class: AppBundle\User
  alias: User
  id_properties:
    - id
  urls:
    self: get_user

This is get_user route definition (which works if I call it directly):

@Route(
    path="/user/{id}",
    name="get_user",
    requirements={"id": "\d+"}
)

And this is the error I get:

Parameter "id" for route "get_user" must match "\d+" ("{id}" given) to generate a corresponding URL.

If I change the mapping to something like

# ...
  urls:
    self: route_without_parameters
# ...

I get a response but without any url referencing that route:

{
  "data": {
    "type": "user",
    "id": "1",
    "attributes": {
        ...
    }
  },
  "jsonapi": {
    "version": "1.0"
  }
}

regniblod avatar Jun 05 '17 13:06 regniblod

Can be closed, if I change the requirements of the route to requirements={"{id}": "\d+"} (note the braces in the parameter name) it works.

I still don't know why Symfony allows both ways of defining requirements tho.

regniblod avatar Jun 05 '17 15:06 regniblod