使用DTO会引发500个验证异常

时间:2018-11-16 10:02:28

标签: php symfony api-platform.com

在文档How to Use a DTO for Writing之后,我收到500个验证错误:

{
    "@context": "/api/contexts/Error",
    "@type": "hydra:Error",
    "hydra:title": "An error occurred",
    "hydra:description": "Cannot validate values of type \"NULL\" automatically. Please provide a constraint.",
    ...

DTO与记录的相同(请注意特定的名称空间):

namespace Acme\Infrastructure\Dto;

use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ApiResource(
 *      collectionOperations={
 *          "post"={
 *              "path"="/do-something",
 *          },
 *      },
 *      itemOperations={},
 * )
 */
final class DoSomethingRequest
{
    /**
     * @Assert\NotBlank
     * @Assert\Email
     */
    public $email;
}

这是配置:

# api_platform.yaml
api_platform:
    mapping:
        paths:
            - '%kernel.project_dir%/src/Infrastructure/Dto'

1 个答案:

答案 0 :(得分:0)

实际上,我正在调用发送FormData编码有效负载的端点。事实证明,“ API平台仅支持将原始文档作为请求输入”(参见https://api-platform.com/docs/core/form-data/)。

尽管如此,我认为在这种情况下抛出一个更明确的异常会比让Symfony的RecursiveContextualValidator用模糊的消息处理该情况更好。