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
<?phpnamespaceApp\Document;
useApiPlatform\Metadata\ApiResource;
useDoctrine\Common\Collections\ArrayCollection;
useDoctrine\Common\Collections\Collection;
useDoctrine\ODM\MongoDB\Mapping\AnnotationsasMongoDB;
#[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')]
publicCollection|iterable$answers;
publicfunction__construct()
{
$this->answers = newArrayCollection();
}
publicfunctiongetId()
{
return$this->id;
}
publicfunctiongetText(): ?string
{
return$this->text;
}
publicfunctionsetText(string$text): self
{
$this->text = $text;
return$this;
}
publicfunctiongetAnswers()
{
return$this->answers;
}
publicfunctionsetAnswers($answers): self
{
$this->answers = $answers;
return$this;
}
}src\Document\Answer.php
<?phpnamespaceApp\Document;
useApiPlatform\Metadata\ApiResource;
useApiPlatform\Metadata\Delete;
useApiPlatform\Metadata\GetCollection;
useApiPlatform\Metadata\Link;
useApiPlatform\Metadata\Patch;
useApiPlatform\Metadata\Post;
useDoctrine\ODM\MongoDB\Mapping\AnnotationsasMongoDB;
#[MongoDB\Document(collection: 'answers')]
#[ApiResource(
uriTemplate: '/questions/{id}/answers',
operations: [
newGetCollection(),
newPost(),
newPatch(),
newDelete(),
],
uriVariables: [
'id' => newLink(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;
publicfunctiongetId()
{
return$this->id;
}
publicfunctiongetText(): ?string
{
return$this->text;
}
publicfunctionsetText(string$text): self
{
$this->text = $text;
return$this;
}
publicfunctiongetQuestion()
{
return$this->question;
}
publicfunctionsetQuestion(?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.