Denormalization_context groups doesnt work on Entity variables
See original GitHub issueAPI Platform version(s) affected: x.y.z
Description
My simple ApiResource entity denormalization group doesnt work
This is my entity
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName;
/**
* @ApiResource(
* attributes={
* "normalization_context"={"groups"={"read"}},
* "denormalization_context"={"groups"={"write"}},
* }
* )
* @ORM\Entity(repositoryClass=UserRepository::class)
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups("write")
*/
private $first_name;
/**
* @Groups("write")
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $last_name;
/**
* @ORM\Column(type="string", length=255)
* @Groups("write")
*/
private $api_key;
/**
* @Groups("read")
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $full_name;
public function getId(): ?int
{
return $this->id;
}
public function getFirstName(): ?string
{
return $this->first_name;
}
public function setFirstName(string $first_name): self
{
$this->first_name = $first_name;
return $this;
}
public function getLastName(): ?string
{
return $this->last_name;
}
public function setLastName(?string $last_name): self
{
$this->last_name = $last_name;
return $this;
}
public function getApiKey(): ?string
{
return $this->api_key;
}
public function setApiKey(string $api_key): self
{
$this->api_key = $api_key;
return $this;
}
public function getFullName(): ?string
{
return $this->full_name;
}
public function setFullName(?string $full_name): self
{
$this->full_name = $full_name;
return $this;
}
}
This is what I get
Possible Solution
There is a way to go around this issue just use denormalization groups on setter methods :
/**
* @param string $first_name
* @Groups("write")
* @return $this
*/
public function setFirstName(string $first_name): self
{
$this->first_name = $first_name;
return $this;
}
/**
* @param string|null $last_name
* @return $this
* @Groups("write")
*/
public function setLastName(?string $last_name): self
{
$this->last_name = $last_name;
return $this;
}
/**
* @param string $api_key
* @return $this
* @Groups("write")
*/
public function setApiKey(string $api_key): self
{
$this->api_key = $api_key;
return $this;
}
Then it works just fine -
Also in this https://github.com/api-platform/api-platform/issues/1576 issue which is same problem but only with normalization_context not denormalization it suggest to clear cache with php bin/console cache:clear
, i’ve tried it and it didn’t work.
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (1 by maintainers)
Top Results From Across the Web
The Serialization Process - API Platform
API Platform allows you to specify the $context variable used by the Symfony Serializer. This variable is an associative array that has a...
Read more >php - Specific Normalization and denormalization context not ...
I have a User Entity like this <?php namespace App\Entity; use ApiPlatform\Core\Annotation\ApiResource; use App\Repository\UserRepository; ...
Read more >The Serializer Component (Symfony Docs)
Many Serializer features can be configured using a context. Attributes Groups. Sometimes, you want to serialize different sets of attributes from your entities....
Read more >What is denormalization and how does it work? - TechTarget
Normalizing a database involves removing redundancy so only a single copy exists of each piece of information. Denormalizing a database requires data has...
Read more >What's New in EF Core 7.0 - Microsoft Learn
See Plan for Entity Framework Core 7.0 for details and . ... simple OUTPUT clause, because, on SQL Server, this doesn't work with...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
looks like this was the correct answer:
Declare your properties with camel case instead of snake case. The serializer expects camelcase to match your properties with getters/setters.
@andrekutianski may you open a new issue with your case? Thanks.
~I believe that I resolve. In my case a moved the normalization_context to the operation:~
False-positive. Not worked at all.
~Now the
@context
has the property and Hydra understudy which schema use.~