core: JSON POST not working since version 2.4.7

Since version 2.4.7 the fallowing POST request with application/json is not working anymore:

{
"title": "Test document",
"articles": [
     {
        "title": "New Article", 
        "originalArticle": {"id": 21}
     }
  ]
}

Throws an error:

No route matches “21”.

Version 2.4.6 is working.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 16 (11 by maintainers)

Most upvoted comments

Even if it is new, this shouldn’t be done in a patch release. Definitely a BC break.

My investigation has revealed that it’s not a bug. In fact, it’s because we’ve fixed a bug which in turn revealed the problem with incorrect property metadata set on your API resources.

See #3145

To expand on the earlier examples, this is the resource config and groups relation in my User entity. To the best of my knowledge this is exactly what you described? A serializer group in the denormalization context, right?

@ApiResource(
 *     attributes={
 *          "pagination_items_per_page"=100,
 *          "model_names"={
 *              "outin"="out",
 *              "in"="in",
 *              "public"="public"
 *          },
 *         "normalization_context"={"groups"={"out", "in"}},
 *         "denormalization_context"={"groups"={"in"}},
 *         "pagination_enabled"=true
 *      },
 *     collectionOperations={
 *          "post"={"method"="POST"},
 *          "get"={"method"="GET"},
 *          "public"={"method"="GET","route_name"="user_public_information","normalization_context"={"groups"={"public"}}}
 *      },
 *     itemOperations={
 *          "get"={"method"="GET"},
 *          "put"={"method"="PUT"},
 *          "delete"={"method"="DELETE"},
 *      }
 * )
    /**
     * @var ArrayCollection|Group[]
     * @ORM\ManyToMany(targetEntity="App\Entity\Group",inversedBy="users")
     * @ORM\JoinTable(name="user_group_assoc",
     *      joinColumns={@ORM\JoinColumn(name="userId", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="groupId", referencedColumnName="id")}
     * )
     * @Groups({"in"})
     */
    protected $groups;

@nicklasaloq

If “book”: {“id”: 1} is not right, how come it has been working up until now (all i am saying is, something must have changed)?

It was never supposed to work that way, as far as I could tell. It just happened to be working, but that’s never been a supported use case (you can look at our test suite).

I have tried this:

POST https://localhost:8002/api/reviews
Accept: application/json
Content-Type: application/json

{
  "book": 1,
  "text": "Awesome!"
}

It works in 2.4.6 and 2.4.7 as soon as allow_plain_identifiers is turned on, as @teohhanhui pointed out, and your Content-Type header is application/json (not application/ld+json).

Beware that the example you provided is not correct: the server doesn’t expect "book": {"id": 1} but "book": 1.

@nicklasaloq Referencing the object by its IRI, e.g. "book": "/api/books/1" is the recommended way indeed.