Home > OS >  API Platform "Unexpected non-object value for object property."
API Platform "Unexpected non-object value for object property."

Time:01-09

I am trying to use API Platform serialization with calculated field as in here enter image description here

But when I change the return to something else (for example return new \DateTime('now') ), I get it working.

I wonder why this is happening, I tried with string too, but it doesn't work.

CodePudding user response:

I supose that your entity property $timePassed is type of DateTime. Return type of method getTimePassed() also must be the DateTime type. You can change type of property $timePassed to int instead of DateTime or make your custom TimePassedSerializer service.

/** this **/
private int $timePasede;
/** insted of this **/
private DateTime $timePasede

CodePudding user response:

The example defines the return type of the function with : int at the end, so that it's clear that this function will return an integer.

I guess that api-platform will expect an object per default until a different return type has been defined.

So this should work from my understanding. Note that I haven't tested it because I don't have the possibility for it right now.

/**
 * @Groups({
 *     "read:actionJeu"
 * })
 */
public function getTimePassed(): int {
    return 4;
}
  •  Tags:  
  • Related