简体   繁体   English

如何通过 ApiPlatform 保存 json 属性?

[英]How to save json attribute via ApiPlatform?

<?php

/** 
 * @ApiResource(
 *     normalizationContext={"groups"={"fields_group:read"}},
 *     denormalizationContext={"groups"={"fields_group:write"}}
 * )
 */
class Entity 
{
    /**
     * @var array Settings.
     *
     * @ORM\Column(type="json", options={"comment":"Settings"})
     *
     * @Assert\NotBlank()
     * @Assert\Json()
     *
     * @Groups({"fields_group:read", "fields_group:write"})
     * @ApiProperty(
     *     attributes={
     *         "openapi_context"={
     *             "type"="object",
     *             "example"={"option":"value"}
     *         }
     *     }
     * )
     */
    private array $settings;

    /**
     * @return array
     */
    public function getSettings(): array
    {
        return $this->settings;
    }



    /**
     * @param array $settings
     *
     * @return self
     */
    public function setSettings(array $settings): self
    {
        $this->settings = $settings;

        return $this;
    }
}

Request要求

curl -X POST "https://localhost/api/entities" \
    -H  "accept: application/ld+json" \
    -H  "Content-Type: application/ld+json" \
    -d "{\"settings\":{\"option\":\"value\"}}"

Response回复

{
  "status": 400,
  "message": "Expected argument of type \"string\", \"array\" given"
}

How to save json attribute via ApiPlatform ?如何通过 ApiPlatform 保存 json 属性?

Json validator is guilty https://github.com/symfony/validator/blob/v5.1.3/Constraints/JsonValidator.php#L37 Json 验证器有罪https://github.com/symfony/validator/blob/v5.1.3/Constraints/JsonValidator.php#L37

It is redundant ApiPlatform will check type by comment @var array Settings多余的 ApiPlatform 将通过注释@var array Settings检查类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM