ObjectMapper wrong association with child entity
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 52/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 领域
- api, backend, backend-api-design
调研方向
先从提供的 Product 和 Category entity/DTO 定义以及 Symfony ObjectMapper 配置开始,然后复现 Category 关系的已报告 GET 映射错误。当 Product DTO 的 category 能够从 Category entity 正确映射,且不再出现已报告的类型错误时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
API Platform version(s) affected: 4.2.14
Description
Hi !
When using ObjectMapper, child DTO management does not seem to work on a GET / GET Collection method.
When I have a Parent A entity that contains a child Aa and I have a DTO corresponding to these two entities, DtoA and DtoAa, then in my DtoA I am unable to map the child Dto.
Or perhaps I am missing something in my configuration or in my understanding of how to use ObjectMapper with ApiPlatform.
Maybe it's more related to ObjectMapper than ApiPlatform ?
How to reproduce
With this example, I get a mapping error on the category relationship. It seems that ObjectMapper is unable to connect to my resource.
Expected argument of type "?App\Api\ApiResource\Category", "App\Entity\Category" given at property path "category".
Entity Product :
declare(strict_types=1);
namespace App\Entity;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Gedmo\Mapping\Annotation\Translatable;
use Symfony\Component\Validator\Constraints\NotBlank;
#[Entity]
class Product
{
#[Id]
#[GeneratedValue]
#[Column(type: Types::INTEGER)]
private ?int $id = null;
#[ManyToOne(targetEntity: Category::class)]
#[JoinColumn(nullable: true, onDelete: 'SET NULL')]
private ?Category $category = null;
#[Column(type: Types::STRING)]
private string $name;
public function getId(): ?int
{
return $this->id;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): void
{
$this->category = $category;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
}
Entity Category :
<?php
declare(strict_types=1);
namespace App\Entity;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
#[Entity]
class Category
{
#[Id]
#[GeneratedValue]
#[Column(type: Types::INTEGER)]
private ?int $id = null;
#[Column(name: 'label', type: Types::STRING, nullable: false)]
private ?string $label = null;
public function getId(): ?int
{
return $this->id;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(?string $label): void
{
$this->label = $label;
}
}
DTO Product :
declare(strict_types=1);
namespace App\Api\ApiResource;
use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use App\Entity\Product as ProductEntity;
use Symfony\Component\ObjectMapper\Attribute\Map;
#[ApiResource(
operations: [
new Get(
uriTemplate: '/products/{id}',
),
],
stateOptions: new Options(entityClass: ProductEntity::class)
)]
#[Map(source: ProductEntity::class)]
class Product
{
public int $id;
public string $name;
public ?Category $category = null;
}
DTO Category
<?php
declare(strict_types=1);
namespace App\Api\ApiResource;
use App\Entity\Category as CategoryEntity;
use Symfony\Component\ObjectMapper\Attribute\Map;
#[Map(source: CategoryEntity::class)]
class Category
{
public int $id;
public string $label;
}
Thank you for your help.
- 主要语言
- PHP
- 星标
- 2.6k
- 派生
- 987
- 平均合并
- 1 天 10 小时
- 30 天内合并 PR
- 76
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
api-platform/core 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 75/100
api-platform/core#8665 ·
维护者通常 1 天内回复
-
Doctrine\Orm\OrderExtension fails on SortDirection可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
api-platform/core#8660 ·
维护者通常 1 天内回复
-
DeserializeProvider calls PartialDenormalizationException::getErrors(), deprecated in Symfony 8.1可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
api-platform/core#8650 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 75/100
api-platform/core#8649 ·
维护者通常 1 天内回复
-
`OrderExtension` and `OrderFilter` pass string sort directions, deprecated since `doctrine/orm` 3.7未关闭
难度 2/5 1-3 小时 新手友好度 78/100
api-platform/core#8648 · 1 条评论 ·
维护者通常 1 天内回复
查看 api-platform/core 的全部 Issue
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 78/100
backdrop-contrib/commerce#60 ·
-
难度 1/5 1 小时以内 新手友好度 76/100
-
难度 1/5 1 小时以内 新手友好度 84/100
TYPO3-Documentation/TYPO3CMS-Reference-CoreApi#7330 ·
维护者通常 1 天内回复
-
dependencies
难度 2/5 1-3 小时 新手友好度 67/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100