NULL value found, but an object is required in assertMatchesResourceCollectionJsonSchema

Viewed 34

I have created and entity with OneToOne relation to other entity.

For the sake of simplicity I will only show related props of those entities

class Comment
{
    #[ORM\OneToOne(inversedBy: 'comment', targetEntity: Answer::class)]
    #[ApiProperty(readableLink: true)]
    #[ORM\JoinColumn(nullable: true)]
    private ?Answer $answer = null;
}

class Answer
{
    #[ORM\OneToOne(mappedBy: 'answer', targetEntity: Comment::class)]
    private Comment $comment;
}

Comment can exist without Answer but Answer cannot exist without Comment and each Comment can only have exactly one Answer.
Those entities works just fine but when it comes to tests I have and error mentioned in title of this question.

body of test:

use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;

class CommentTest extends ApiTestCase
{
    public function testGetComments(): void
    {
        $response = static::createClient()->request('GET', '<url>');

        self::assertResponseIsSuccessful();
        self::assertMatchesResourceCollectionJsonSchema(Comment::class);
    }
}

In this test I am getting an error in assertMatchesResourceCollectionJsonSchema.

hydra:member[0].answer: NULL value found, but an object is required
hydra:member[0].answer: Failed to match at least one schema

Why object is required when in Comment entity there is provided ? and #[ORM\JoinColumn(nullable: true)] which states that this property can be "NULL"


Update

After some investigation I have found that the culprit is #[ApiProperty(readableLink: true)] in Comment entity. API works just fine with this but for some reason tests do not want to cooperate.

0 Answers
Related