question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Denormalization_context groups doesnt work on Entity variables

See original GitHub issue

API 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 Screenshot 2021-01-14 155337

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 - image

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:closed
  • Created 3 years ago
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
soyukacommented, Feb 23, 2021

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.

0reactions
andrekutianskicommented, Feb 21, 2021

~I believe that I resolve. In my case a moved the normalization_context to the operation:~

False-positive. Not worked at all.

#[ApiResource(
    mercure: true,
    normalizationContext: ['groups' => ['read']],
    collectionOperations: [
        'get',
        'post' => [
            'normalization_context' => ['groups' => ['write']],
        ],
    ],
)]

~Now the @context has the property and Hydra understudy which schema use.~

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found