JMSSerializerBundle icon indicating copy to clipboard operation
JMSSerializerBundle copied to clipboard

How to Exclude the nested property of an Entity class?

Open praditha-hidayat opened this issue 2 years ago • 1 comments

Q A
Bug report? no
Feature request? no
BC Break report? no
RFC? no

Steps required to reproduce the problem

I have an entity class called SeoConfig that has a field called $seo_data which is an object of the SeoData class. Here is the code

<?php

use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Groups;
use Symfony\Component\HttpFoundation\File\File;

class SeoConfig
{
    private $id;

    public function getId()
    {
        return $this->id;
    }

    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @Groups(groups={"bootstrap_config"})
     */
    private $path;

    public function getPath()
    {
        return $this->path;
    }

    public function setPath($path)
    {
        $this->path = $path;
    }

    /**
     * @var SeoData
     * @Groups(groups={"bootstrap_config"})
     */
    private $seo_data;

    public function getSeoData()
    {
        return $this->seo_data;
    }

    public function setSeoData($seo_data)
    {
        $this->seo_data = $seo_data;
    }
}
<?php

use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\Annotation\Groups;
use Symfony\Component\HttpFoundation\File\File;

/**
 * @Serializer\ExclusionPolicy('none')
 */
class SeoData
{
    /**
     * @var string
     * @Groups(groups={"bootstrap_config"})
     */
    private $title;

    public function getTitle()
    {
        return $this->title;
    }

    public function setTitle($title)
    {
        $this->title = $title;
    }

    /**
     * @var string
     * @Groups(groups={"bootstrap_config"})
     */
    private $description;

    public function getDescription()
    {
        return $this->description;
    }

    public function setDescription($description)
    {
        $this->description = $description;
    }

    /**
     * I want to exclude this property because it causes the Error
     * @var File
     * @Serializer\Exclude()
     */
    private $file_image;

    public function getFileImage()
    {
        return $this->file_image;
    }

    public function setFileImage($file_image)
    {
        $this->file_image = $file_image;
    }
}

As you can see on the SeoData class, I want to @Exclude() the $file_image property, because it cause an error: Serialization of 'Symfony\Component\HttpFoundation\File\File' is not allowed and this error is happening when I try to upload a file and saving the SeoConfig data.

Here is my jms_serializer.yml file:

jms_serializer:
  default_context:
    serialization:
      serialize_null: false
      attributes:
        allows_root_null: true
  visitors:
    xml_serialization:
      format_output: false
    json_serialization:
      options:
        - JSON_UNESCAPED_SLASHES
        - JSON_PRESERVE_ZERO_FRACTION

So, I want to avoid the above error and that's why I try to @Exclude it. But it seems it's not working on the embedded object like the sample above. If I don't upload a file, and just save the rest data, it's working.

Expected Result

  • The $file_image property should be excluded from serializing process and it doesn't throw the Serialization of 'Symfony\Component\HttpFoundation\File\File' is not allowed error

Actual Result

  • Even though I have put the @Exclude() annotation on the above $file_image property, it still throws the error.

praditha-hidayat avatar Apr 06 '22 09:04 praditha-hidayat

Hi @praditha-hidayat!

Could you provide some simple repository that reproduce your issue or stack trace for the error? I've tried it, but exclusion works for me with your example. The error that you provided seems to be connected with call to serialize function over Symfony\Component\HttpFoundation\File\File, but not JSON / XML serialisation.

Best, Marcin

scyzoryck avatar Apr 26 '22 22:04 scyzoryck