1

I have an entity which is exposed as an api-platform resource, and contains the following property:

/**
 * @ORM\Column(type="string", nullable=true)
 */
private $note;

When I try to update the entity (via PUT), sending the following json:

{
  "note": null
}

I get the following error from the Symfony Serializer:

[2017-06-29 21:47:33] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Serializer\Exception\UnexpectedValueException: "Expected argument of type "string", "NULL" given" at /var/www/html/testapp/server/vendor/symfony/symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php line 196 {"exception":"[object] (Symfony\Component\Serializer\Exception\UnexpectedValueException(code: 0):Expected argument of type \"string\", \"NULL\" given at /var/www/html/testapp/server/vendor/symfony/symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php:196, Symfony\Component\PropertyAccess\Exception\InvalidArgumentException(code: 0): Expected argument of type \"string\", \"NULL\" given at /var/www/html/testapp/server/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php:275)"} []

It seems like I'm missing some config to allow nulls on this property? To make things weirder, when I GET a resource containing a null note, then the note is correctly returned as null:

{
  "@context": "/contexts/RentPayment",
  "@id": "/rent_payments/1",
  "@type": "RentPayment",
  "id": 1,
  "note": null,
  "date": "2016-03-01T00:00:00+00:00"
}

What am I missing - ps I'm a massive newb to api-platform

2
  • 1
    Just a wild stab but are you using a type hinted setter? Commented Jun 30, 2017 at 8:00
  • Thank you! Ugh, why didn't I think of that - add it as an answer if you want. Commented Jun 30, 2017 at 9:16

1 Answer 1

5

Alright then as determined in the comments you were using a type hinted setter à la:

public function setNote(string $note) {
    $this->note = $note;
    return $this;
}

As of PHP 7.1 we have nullable types so the following would be preferred as it actually checks null or string instead of any type.

public function setNote(?string $note = null) {

In previous versions just remove the type hint and if preferred add some type checking inside.

public function setNote($note) {
    if ((null !== $note) && !is_string($note)) {
        // throw some type exception!
    }
    
    $this->note = $note;
    return $this;
}

Another thing you might want to consider is using something like:

$this->note = $note ?: null;

This is the sorthand if (ternary operator). To set the value to null if the string is empty (but bugs on '0' so you might need to do the longer version).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.