使用 DTO 会抛出 500 验证异常
Using a DTO throws a 500 validation exception
按照文档 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'
实际上,我正在调用端点发送 FormData
编码的有效负载。原来是"API Platform only supports raw documents as request input" (cf. https://api-platform.com/docs/core/form-data/).
尽管如此,我认为在这种情况下抛出一个更明确的异常比让 Symfony 的 RecursiveContextualValidator
处理带有模糊消息的情况更好。
按照文档 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'
实际上,我正在调用端点发送 FormData
编码的有效负载。原来是"API Platform only supports raw documents as request input" (cf. https://api-platform.com/docs/core/form-data/).
尽管如此,我认为在这种情况下抛出一个更明确的异常比让 Symfony 的 RecursiveContextualValidator
处理带有模糊消息的情况更好。