title: API Platform and custom POST operation with custom body
body: <p>I hope I'm right to ask this. I've looked at (almost) all similar concern but I ain't satisfied yet.</p>
  
  <p>I'm working on a User entity and for days (weeks actually) now i'm trying to POST a user with a custom body. Here's some part of my entity User : </p>
  
  <pre><code>/**
   * @ApiResource(
   *      normalizationContext={"groups"={"read"}},
   *      denormalizationContext={"groups"={"write"}},
   *      itemOperations={
   *          "get",
   *          "put",
   *          "delete",
   *          "get_active_user"={
   *              "method"="GET",
   *              "path"="/users/active/me",
   *              "controller"=UserReadAction::class,
   *              "defaults"={"_api_receive"=false},
   *              "swagger_context"={
   *                  "parameters"={
   *
   *                  }
   *              }
   *          },
   *      },
   *      collectionOperations={
   *          "change_password"={
   *              "method"="POST",
   *              "path"="/users/active/changepassword",
   *              "controller"=UserChangePasswordAction::class,
   *              "normalization_context"={"groups"={"afup"}},
   *              "defaults"={"_api_receive"=false},
   *              "swagger_context"={
   *                  "summary" = "Change user password",
   *                  "parameters"={
   *                      {
   *                          "name" = "User",
   *                          "in" = "body",
   *                          "schema" = {
   *                              "type" = "object",
   *                              "properties" = {
   *                                  "password" = {"type"="string"},
   *                                  "nom" = {"type"="string"},
   *                              }
   *                           },
   *                          "required" = "true",
   *                      }
   *                  },
   *              }
   *          }
   *      }
   * )
   * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
   * @ORM\Table(name="users")
   */
  class User implements UserInterface
  {
      /**
       * @ORM\Id()
       * @ORM\GeneratedValue()
       * @ORM\Column(type="integer")
       * @Groups({"read", "write", "afup"})
       */
      private $id;
  </code></pre>
  
  <p>Here is the controller:</p>
  
  <pre><code>namespace App\Controller\SDK;
  
  
  use App\Entity\User;
  use App\Service\SDK\UserService;
  use Symfony\Component\Security\Core\Security;
  
  class UserChangePasswordAction
  {
      public function __invoke(User $data)
      {
          var_dump($data);die;
      }
  }
  </code></pre>
  
  <p>And the services.yaml (some part) file </p>
  
  <pre><code>services:
      # default configuration for services in *this* file
      _defaults:
          autowire: true      # Automatically injects dependencies in your services.
          autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
          public: false       # Allows optimizing the container by removing unused services; this also means
                              # fetching services directly from the container via $container-&gt;get() won't work.
                              # The best practice is to be explicit about your dependencies anyway.
  
      # makes classes in src/ available to be used as services
      # this creates a service per class whose id is the fully-qualified class name
      App\:
          resource: '../src/*'
          exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
  
      # controllers are imported separately to make sure services can be injected
      # as action arguments even if you don't extend any base controller class
      App\Controller\:
          resource: '../src/Controller/*'
          tags: ['controller.service_arguments']
  </code></pre>
  
  <p>When I try this (see var_dump in controller), i get an error saying:</p>
  
  <blockquote>
    <p>Cannot autowire argument $data of "App\Controller\SDK\UserChangePasswordAction()": it references class "App\Entity\User" no such service exists</p>
  </blockquote>
  
  <p>I read the official doc and it seems that the _invoke method should automatically retrieve the entity. But it does not work for me.</p>
  
  <p>Notice: I also defined a custom item operation "get_active_user" and it works fine.</p>
  
  <p>Please I would like to understand :</p>
  
  <ul>
  <li>what I did wrong,</li>
  <li>how it actually works,</li>
  </ul>
  
  <p>Thank you.</p>
  
  <p>EDIT: 
  In the collectionOperation definition, i removed the following setting which means that we manually want to handle data (User) retrieval  :</p>
  
  <blockquote>
    <ul>
    <li>"defaults"={"_api_receive"=false},</li>
    </ul>
  </blockquote>
  
  <p>Now, the controller returns an empty User entity, not an error. I still can't get the submitted data. </p>