Closed
Description
- I was not able to find an open or closed issue matching what I'm seeing.
- This is not a question. (Questions should be asked on slack (Signup for Slack here) or our forums.)
Provide a narrative description of what you are trying to accomplish.
Code to reproduce the issue
User.php
namespace App;
class User
{
/**
* @var string
*/
protected $id;
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $email;
/**
* @var Avatar
*/
protected $avatar;
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): User
{
$this->name = $name;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): User
{
$this->email = $email;
return $this;
}
public function getAvatar(): ?Avatar
{
return $this->avatar;
}
public function setAvatar(?Avatar $avatar): User
{
$this->avatar = $avatar;
return $this;
}
}
Avatar.php
namespace App;
class Avatar
{
/**
* @var string|null
*/
protected $id;
/**
* @var string|null
*/
protected $url;
public function getId(): ?string
{
return $this->id;
}
public function setId(?string $id): User
{
$this->id = $id;
return $this;
}
public function getUrl(): ?string
{
return $this->url;
}
public function setUrl(?string $url): User
{
$this->url = $url;
return $this;
}
}
config.php
MetadataMap::class => [
[
'__class__' => RouteBasedResourceMetadata::class,
'resource_class' => App\User::class,
'route' => 'api.user',
'extractor' => ClassMethodsHydrator::class,
],
[
'__class__' => RouteBasedResourceMetadata::class,
'resource_class' => App\Avatar::class,
'route' => 'api.avatar',
'extractor' => ClassMethodsHydrator::class,
],
],
$renderer = new JsonRenderer();
$avatar = new Avatar();
$avatar->setId('1234');
$avatar->setUrl('https://superfastcdn.com/myavatar.png');
$user = new User();
$user->setId('5678');
$user->setName('John Doe');
$user->setEmail('[email protected]');
$user->setAvatar($avatar);
Expected results
$resource = $resourceGenerator->fromObject($user, $request);
echo $renderer->render($resource);
$user->setAvatar(null);
$resource = $resourceGenerator->fromObject($user, $request);
echo $renderer->render($resource);
{
"name": "John Doe",
"email": "[email protected]",
"_embedded": {
"avatar": {
"id": "1234",
"url": "https://superfastcdn.com/myavatar.png",
"_links": {
"self": {
"href": "https://api.acme.com/avatars/1234"
}
}
}
},
"_links": {
"self": {
"href": "https://api.acme.com/users/5678"
}
}
}
{
"name": "John Doe",
"email": "[email protected]",
"_embedded": {
"avatar": null
},
"_links": {
"self": {
"href": "https://api.acme.com/users/5678"
}
}
}
Actual results
{
"name": "John Doe",
"email": "[email protected]",
"_embedded": {
"avatar": {
"id": "1234",
"url": "https://superfastcdn.com/myavatar.png",
"_links": {
"self": {
"href": "https://api.acme.com/avatars/1234"
}
}
}
},
"_links": {
"self": {
"href": "https://api.acme.com/users/5678"
}
}
}
{
"name": "John Doe",
"email": "[email protected]",
"avatar": null,
"_links": {
"self": {
"href": "https://api.acme.com/users/5678"
}
}
}
Originally posted by @jguittard at zendframework/zend-expressive-hal#52
Metadata
Metadata
Assignees
Labels
No labels