如何解决如何通过ApiPlatform保存json属性?
<?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;
}
}
请求
curl -X POST "https://localhost/api/entities" \
-H "accept: application/ld+json" \
-H "Content-Type: application/ld+json" \
-d "{\"settings\":{\"option\":\"value\"}}"
响应
{
"status": 400,"message": "Expected argument of type \"string\",\"array\" given"
}
如何通过ApiPlatform保存json属性?
解决方法
Json验证者有罪https://github.com/symfony/validator/blob/v5.1.3/Constraints/JsonValidator.php#L37
这是多余的ApiPlatform将通过注释检查类型
@var array Settings
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。