API Platform version(s) affected: 3.1.12
Description
When querying relation results in API Platform version 3.1.12, the subresources do not respect the relation field type.
Possibly related to issue #5269.
How to reproduce
Let's say we've followed up documentation and created a similar Question to Answer relation subresource.
/src/Document/Question.php
<?php
namespace App\Document;
use ApiPlatform\Metadata\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
#[MongoDB\Document(collection: 'questions')]
#[ApiResource]
class Question
{
#[MongoDB\Id]
public $id;
#[MongoDB\Field(type: 'string')]
public ?string $text = null;
#[MongoDB\ReferenceMany(storeAs: 'id', targetDocument: Answer::class, mappedBy: 'question')]
public Collection|iterable $answers;
public function __construct()
{
$this->answers = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function getText(): ?string
{
return $this->text;
}
public function setText(string $text): self
{
$this->text = $text;
return $this;
}
public function getAnswers()
{
return $this->answers;
}
public function setAnswers($answers): self
{
$this->answers = $answers;
return $this;
}
}
src\Document\Answer.php
<?php
namespace App\Document;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
#[MongoDB\Document(collection: 'answers')]
#[ApiResource(
uriTemplate: '/questions/{id}/answers',
operations: [
new GetCollection(),
new Post(),
new Patch(),
new Delete(),
],
uriVariables: [
'id' => new Link(toProperty: 'question', fromClass: Question::class),
],
)]
class Answer
{
#[MongoDB\Id]
public $id;
#[MongoDB\Field(type: 'string')]
public ?string $text = null;
#[MongoDB\ReferenceOne(storeAs: 'id', targetDocument: Question::class)]
public ?Question $question;
public function getId()
{
return $this->id;
}
public function getText(): ?string
{
return $this->text;
}
public function setText(string $text): self
{
$this->text = $text;
return $this;
}
public function getQuestion()
{
return $this->question;
}
public function setQuestion(?Question $question): self
{
$this->question = $question;
return $this;
}
}
Which creates a query:
{
"aggregate": "answers",
"pipeline": [
{
"$lookup": {
"from": "questions",
"as": "question_lkup",
"localField": "question",
"foreignField": "_id"
}
},
{
"$match": {
"question_lkup._id": "647b60f72a3e4fac1f08b752"
}
},
{
"$sort": {
"_id": {
"$numberInt": "1"
}
}
},
{
"$facet": {
"results": [
{
"$skip": {
"$numberInt": "0"
}
},
{
"$limit": {
"$numberInt": "30"
}
}
],
"count": [
{
"$count": "count"
}
]
}
}
],
"cursor": {}
}
Possible Solution
The subresources should respect the relation field type, ensuring that the correct related entities are returned in the query results.
The query should be like:
"$match": {
"question_lkup._id": ObjectId("647b60f72a3e4fac1f08b752")
}
Additional Context
Let me know if I can help somehow.
API Platform version(s) affected: 3.1.12
Description
When querying relation results in API Platform version 3.1.12, the subresources do not respect the relation field type.
Possibly related to issue #5269.
How to reproduce
Let's say we've followed up documentation and created a similar Question to Answer relation subresource.
/src/Document/Question.php
src\Document\Answer.php
Which creates a query:
{ "aggregate": "answers", "pipeline": [ { "$lookup": { "from": "questions", "as": "question_lkup", "localField": "question", "foreignField": "_id" } }, { "$match": { "question_lkup._id": "647b60f72a3e4fac1f08b752" } }, { "$sort": { "_id": { "$numberInt": "1" } } }, { "$facet": { "results": [ { "$skip": { "$numberInt": "0" } }, { "$limit": { "$numberInt": "30" } } ], "count": [ { "$count": "count" } ] } } ], "cursor": {} }Possible Solution
The subresources should respect the relation field type, ensuring that the correct related entities are returned in the query results.
The query should be like:
Additional Context
Let me know if I can help somehow.