【发布时间】:2020-08-26 12:48:53
【问题描述】:
<?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 属性?
【问题讨论】:
-
鉴于您的代码,它应该可以工作。您确定错误消息针对您的
settings属性吗?
标签: php symfony doctrine api-platform.com