Search code examples
phprestapirestler

Request parameter is not casted to object


I have a simple API call, that should accept a request parameter from type Message. What I actually get in $data is an array

...

    /**
     * @param Message $data Message to push {@from body}
     *
     * @url POST uploadedFile
     * @return bool
     */
     public function uploadedFile(Message $data) {
        return $this->send(...);
     }

}

class Message
{
     /**
      * @var string */
     private $action;


     /**
      * @var array object to return
      */
     private $parameters;

     /**
      * @var array $type {@type int}
      */
     private $type;

     /**
      * @var string $message {@max 50}
      */
     private $message;
}

Here is my json:

{
    "action": "test",
    "parameters": [],
    "type": [1,2],
    "message": "test"
}

And here is the error I get:

Fatal error: Uncaught TypeError: Argument 1 passed to FCM::uploadedFile() must be an instance of Message, array given in ... on line 22

Solution

  • To answer my own question: For the automatic casting to object to work, the validation for the API class must be switched on. This is done with those two variables:

    // Excluding classes from validation
    Defaults::$autoValidationForbiddenCalls = [ ... ];
    
    // Version from which to auto validate (using PHPDocs)
    Defaults::$mandatoryValidationFromVersion = 3;
    

    The class I was using was excluded from validation, thus the PHPDoc was ignored.