symfony 如何使用API平台收集序列化器中的反规范化类型错误并在http响应中将其作为约束违规返回?

yhived7q  于 2023-01-26  发布在  其他
关注(0)|答案(2)|浏览(120)

我想使用Symfony 5.4串行化器的特性(https://symfony.com/blog/new-in-symfony-5-4-serializer-improvements)来收集反规范化类型错误,并使用API Platform 2.6将它们作为约束冲突的响应返回。我如何实现这一点?

实体:

<?php
declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ApiResource(
 *  collectionOperations={
 *      "get",
 *      "post",
 *  },
 *  itemOperations={
 *      "get",
 *  "put",
 *  "delete",
 * )
 * @ORM\Entity
 */
class Property
{

    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="App\EntityIdGenerator")
     * @ORM\Column(type="string", length=12)
     */
    private string $id;

    /**
     *
     * @ORM\Column(type="string", length=50)
     */
    public string $name;

    public function getId(): string
    {
        return $this->id;
    }

}

请求创建新属性:

POST /properties HTTP/1.1
Host: 127.0.0.1:8000
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE2NDE4MTU0MzQsImV4cCI6MTY3MzM1MTQzNCwicm9sZXMiOlsiUk9MRV9VU0VSIl0sInVzZXJuYW1lIjoiaG9zdEBleGFtcGxlLmNvbSIsImlkIjoiaUthRmp6QjZaRWNDIn0.e6PuoDFrPF4HUFE24rrkAoUMQPqvtWaGwTo-Ec4BOdo1_IyyoxIee7dIGx4fY0yvjhNwH7_XtZKYg3LPzRqusZonT4ysqHe1V7MNepSHNosyAhZNsBdZ8md-2G-MBNYowzgb3_TFlYZxWNVf2cs0R5Du18QN6xQLv0hfZjG1MVOnygzo6nP9ywTwEi7hrIt7cdXsyLdOnf_2yRFQJ4bc80-E8uYQ_HzNo7J1Cq-uwyaNlJbEHHleSEgx3xBqDccPu3DtvhRuXNwStDWgOjv8uYEYHs19zzXV4BQv93dIVMExOZ5_IysDsVGCO74kwC_T1kXFlQvuNMmHBYmCIWBpJefkyutvoBZXR1Oorij0nXZ9M96my2r6z_I-kH2KNkAdi8u3rz8k7rOfQpZrYNNtYj4t_jkNghGaXm2Syalb7XZ-XUTD24rOKYlniLY-6U14S-IyyrVl3k6iDvWDqJrqbWT8PXJqzppDnXc_qgMF309TqavpI17xH2x_KFQHW07Fg0y8NZBIK_k9hCVBPxqQOg6VbaXJFm4tu7oR1m6AAQoXGu_uVopiJz5DMrbzEZ5cTxP7Gla2OohpeLQBnOhAqf_VEjKUs9ajht_mIqs1MjECA_aJEWL62EghtjGNdhNUfUh1mss73Tstah9IKSULZkTlelRhLZ_OhCkogfWkbLU
Content-Type: application/json
Content-Length: 19

{
  "name": null,
}

当前服务器响应:

HTTP响应代码400。
回复正文:

{
    "@context": "/contexts/Error",
    "@type": "hydra:Error",
    "hydra:title": "An error occurred",
    "hydra:description": "The type of the \"name\" attribute must be \"string\", \"NULL\" given.",
    "trace": [
...]

所需的服务器响应:

HTTP响应代码422。
回复正文:

{
    "@context": "/contexts/ConstraintViolationList",
    "@type": "ConstraintViolationList",
    "hydra:title": "An error occurred",
    "hydra:description": "name: This value cannot be omitted.",
    "violations": [
        {
            "propertyPath": "name",
            "message": "This value is cannot be omitted.",
            "code": "9ff3fdc4-b214-49db-8718-39c315e33d45"
        }
    ]
}
p8ekf7hl

p8ekf7hl1#

您是否尝试过使用规则在字段顶部添加@Assert注解?
例如,我使用DTO,如果不使用@Assert,我的输出与您相同,但使用它,我得到了所需的输出:

<?php

namespace App\DTO\Event;

use DateTime;
use Symfony\Component\Validator\Constraints as Assert;

class EventPostInput
{
    /**
     * @Assert\NotBlank
     */
    public string $title;

    /**
     * @Assert\NotBlank
     */
    public string $description;
}

这是我的实体

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\DTO\Event\EventPostInput;
use App\Repository\EventRepository;
use Doctrine\ORM\Mapping as ORM;

#[ApiResource(
    collectionOperations: [
        'post' => [
            'method' => 'POST',
            'input' => EventPostInput::class,
        ]
    ]
)]
/**
 * @ORM\Entity(repositoryClass=EventRepository::class)
 */
class Event
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private int $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private string $title;

    /**
     * @ORM\Column(type="string", length=2000)
     */
    private string $description;
}
gkn4icbw

gkn4icbw2#

如果有人也在看的话,将把它留在这里。PR不久前被合并,它在3.1.0版本中结束。要对所有资源启用此功能:

defaults:
        collectDenormalizationErrors: true

相关问题