sonata-project/SonataPageBundle

A circular reference has been detected when serializing the object of class when saving snapshot

mesiarm opened this issue · 3 comments

When i try to sa Sonata page snapshop I get circular reference exception

Environment

Sonata packages

show

$ composer show --latest 'sonata-project/*'
sonata-project/admin-bundle              4.29.3 4.29.3 The missing Symfony Admin Generator
sonata-project/block-bundle              5.1.0  5.1.0  Symfony SonataBlockBundle
sonata-project/doctrine-extensions       2.3.0  2.4.0  Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 4.15.0 4.16.0 Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/exporter                  3.3.0  3.3.0  Lightweight Exporter library
sonata-project/form-extensions           2.3.0  2.3.0  Symfony form extensions
sonata-project/formatter-bundle          5.3.0  5.4.1  Symfony SonataFormatterBundle
sonata-project/intl-bundle               3.2.0  3.2.0  Symfony SonataIntlBundle
sonata-project/media-bundle              4.11.0 4.13.0 Symfony SonataMediaBundle
sonata-project/page-bundle               4.7.1  4.7.1  This bundle provides a Site and Page management through container and block services
sonata-project/seo-bundle                3.8.0  3.8.0  Symfony SonataSeoBundle
sonata-project/translation-bundle        3.3.0  3.3.0  SonataTranslationBundle
sonata-project/twig-extensions           2.4.0  2.4.0  Sonata twig extensions
sonata-project/user-bundle               5.10.0 5.11.0 Symfony SonataUserBundle

Symfony packages

show

$ composer show --latest 'symfony/*'
symfony/asset                      v5.4.31 v7.0.3  Manages URL generation and versioning of web assets such as CSS stylesheets, JavaScript files and image files
symfony/browser-kit                v5.4.31 v7.0.3  Simulates the behavior of a web browser, allowing you to make requests, click on links and submit forms programmatically
symfony/cache                      v5.4.34 v7.0.4  Provides extended PSR-6, PSR-16 (and tags) implementations
symfony/cache-contracts            v2.5.2  v3.4.0  Generic abstractions related to caching
symfony/config                     v5.4.31 v7.0.4  Helps you find, load, combine, autofill and validate configuration values of any kind
symfony/console                    v5.4.34 v7.0.4  Eases the creation of beautiful and testable command line interfaces
symfony/css-selector               v5.4.26 v7.0.3  Converts CSS selectors to XPath expressions
symfony/debug-bundle               v5.4.26 v7.0.3  Provides a tight integration of the Symfony VarDumper component and the ServerLogCommand from MonologBridge into the Symfony full-stack framework
symfony/dependency-injection       v5.4.34 v7.0.4  Allows you to standardize and centralize the way objects are constructed in your application
symfony/deprecation-contracts      v3.4.0  v3.4.0  A generic function and convention to trigger deprecation notices
symfony/doctrine-bridge            v5.4.34 v7.0.5  Provides integration for Doctrine with various Symfony components
symfony/dom-crawler                v5.4.32 v7.0.4  Eases DOM navigation for HTML and XML documents
symfony/dotenv                     v5.4.34 v7.0.4  Registers environment variables from a .env file
symfony/error-handler              v5.4.29 v7.0.4  Provides tools to manage errors and ease debugging PHP code
symfony/event-dispatcher           v5.4.34 v7.0.3  Provides tools that allow your application components to communicate with each other by dispatching events and listening to them
symfony/event-dispatcher-contracts v3.4.0  v3.4.0  Generic abstractions related to dispatching event
symfony/expression-language        v5.4.21 v7.0.3  Provides an engine that can compile and evaluate expressions
symfony/filesystem                 v5.4.25 v7.0.3  Provides basic utilities for the filesystem
symfony/finder                     v5.4.27 v7.0.0  Finds files and directories via an intuitive fluent interface
symfony/flex                       v2.4.3  v2.4.5  Composer plugin for Symfony
symfony/form                       v5.4.33 v7.0.4  Allows to easily create, process and reuse HTML forms
symfony/framework-bundle           v5.4.34 v7.0.4  Provides a tight integration between Symfony components and the Symfony full-stack framework
symfony/http-client                v5.4.34 v7.0.5  Provides powerful methods to fetch HTTP resources synchronously or asynchronously
symfony/http-client-contracts      v2.5.2  v3.4.0  Generic abstractions related to HTTP clients
symfony/http-foundation            v5.4.34 v7.0.4  Defines an object-oriented layer for the HTTP specification
symfony/http-kernel                v5.4.34 v7.0.5  Provides a structured process for converting a Request into a Response
symfony/intl                       v5.4.30 v7.0.3  Provides a PHP replacement layer for the C intl extension that includes additional data from the ICU library
symfony/mailer                     v5.4.34 v7.0.4  Helps sending emails
symfony/maker-bundle               v1.50.0 v1.56.0 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.
symfony/mime                       v5.4.26 v7.0.3  Allows manipulating MIME messages
symfony/monolog-bridge             v5.4.31 v7.0.3  Provides integration for Monolog with various Symfony components
symfony/monolog-bundle             v3.10.0 v3.10.0 Symfony MonologBundle
symfony/options-resolver           v5.4.21 v7.0.0  Provides an improved replacement for the array_replace PHP function
symfony/password-hasher            v5.4.31 v7.0.4  Provides password hashing utilities
symfony/phpunit-bridge             v5.4.34 v7.0.4  Provides utilities for PHPUnit, especially user deprecation notices management
symfony/polyfill-intl-grapheme     v1.28.0 v1.29.0 Symfony polyfill for intl's grapheme_* functions
symfony/polyfill-intl-icu          v1.28.0 v1.29.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn          v1.28.0 v1.29.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-intl-normalizer   v1.28.0 v1.29.0 Symfony polyfill for intl's Normalizer class and related functions
symfony/polyfill-mbstring          v1.28.0 v1.29.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php72             v1.28.0 v1.29.0 Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions
symfony/polyfill-php73             v1.28.0 v1.29.0 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/polyfill-php80             v1.28.0 v1.29.0 Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions
symfony/polyfill-php81             v1.28.0 v1.29.0 Symfony polyfill backporting some PHP 8.1+ features to lower PHP versions
symfony/process                    v5.4.34 v7.0.4  Executes commands in sub-processes
symfony/property-access            v5.4.26 v7.0.4  Provides functions to read and write from/to an object or array using a simple string notation
symfony/property-info              v5.4.24 v7.0.3  Extracts information about PHP class' properties using metadata of popular sources
symfony/routing                    v5.4.34 v7.0.5  Maps an HTTP request to a set of configuration variables
symfony/runtime                    v5.4.26 v7.0.3  Enables decoupling PHP applications from global state
symfony/security-acl               v3.3.3  v3.3.3  Symfony Security Component - ACL (Access Control List)
symfony/security-bundle            v5.4.34 v7.0.5  Provides a tight integration of the Security component into the Symfony full-stack framework
symfony/security-core              v5.4.30 v7.0.3  Symfony Security Component - Core Library
symfony/security-csrf              v5.4.27 v7.0.3  Symfony Security Component - CSRF Library
symfony/security-guard             v5.4.27 v5.4.35 Symfony Security Component - Guard
symfony/security-http              v5.4.31 v7.0.4  Symfony Security Component - HTTP Integration
symfony/serializer                 v5.4.34 v7.0.4  Handles serializing and deserializing data structures, including object graphs, into array structures or other formats like XML and JSON.
symfony/service-contracts          v2.5.2  v3.4.1  Generic abstractions related to writing services
symfony/stopwatch                  v5.4.21 v7.0.3  Provides a way to profile code
symfony/string                     v5.4.34 v7.0.4  Provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters in a unified way
symfony/templating                 v5.4.21 v6.4.3  Provides all the tools needed to build any kind of template system
symfony/translation                v5.4.31 v7.0.4  Provides tools to internationalize your application
symfony/translation-contracts      v2.5.2  v3.4.1  Generic abstractions related to translation
symfony/twig-bridge                v5.4.34 v7.0.4  Provides integration for Twig with various Symfony components
symfony/twig-bundle                v5.4.31 v7.0.4  Provides a tight integration of Twig into the Symfony full-stack framework
symfony/validator                  v5.4.34 v7.0.5  Provides tools to validate values
symfony/var-dumper                 v5.4.29 v7.0.4  Provides mechanisms for walking through any arbitrary PHP variable
symfony/var-exporter               v6.4.2  v7.0.4  Allows exporting any serializable PHP data structure to plain PHP code
symfony/web-link                   v5.4.21 v7.0.3  Manages links between resources
symfony/web-profiler-bundle        v5.4.34 v7.0.4  Provides a development tool that gives detailed information about the execution of any request
symfony/webpack-encore-bundle      v2.1.1  v2.1.1  Integration with your Symfony app & Webpack Encore!
symfony/yaml                       v5.4.31 v7.0.3  Loads and dumps YAML files

PHP version

$ php -v
# PHP 8.2.2

Subject

Minimal repository with the bug

Steps to reproduce

  1. Add
#[ORM\ManyToOne]
    private ?Municipality $municipality = null;

to App\Entity\SonataPagePage
2. Create entity App\Entity\Municipality with

#[ORM\ManyToOne]
   private ?SonataMediaGallery $headerImages = null;
  1. Add
#[ORM\ManyToOne]
    private ?Municipality $municipality = null;

to App\Entity\SonataMediaGallery

  1. Create SonataPageAdmin Admin extension according documentation with method:
public function configureFormFields(FormMapper $form): void
    {
        if ($this->authorizationChecker->isGranted('ROLE_SUPER_ADMIN') === true) {
            $form
                ->with('advanced', ['collapsed' => true])
                ->add('municipality', null, [
                    'required' => false,
                    'label' => 'Obec',
                ])
                ->end();
        }
    }
  1. create simillar admin extension for SonataMediaGallery
  2. Create MunicipalityAdmin with method
protected function configureFormFields(FormMapper $form): void
    {
        $form
            ->add('headerImages', ModelListType::class, [], ['link_parameters' => ['context' => 'municipality_header_images']])
    }
  1. Create page, choose municipality, save, try to create snapshot

Expected results

Succesful save

Actual results

exception
Symfony\Component\Serializer\Exception\CircularReferenceException:
A circular reference has been detected when serializing the object of class "Proxies\__CG__\App\Entity\SonataMediaGallery" (configured limit: 1).

  at vendor/symfony/serializer/Normalizer/AbstractNormalizer.php:207
  at Symfony\Component\Serializer\Normalizer\AbstractNormalizer->handleCircularReference()
     (vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php:157)
  at Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->normalize()
     (vendor/symfony/serializer/Serializer.php:161)
  at Symfony\Component\Serializer\Serializer->normalize()
     (vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php:224)
  at Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->normalize()
     (vendor/symfony/serializer/Serializer.php:161)
  at Symfony\Component\Serializer\Serializer->normalize()
     (vendor/symfony/serializer/Serializer.php:179)
  at Symfony\Component\Serializer\Serializer->normalize()
     (vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php:224)
  at Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->normalize()
     (vendor/symfony/serializer/Serializer.php:161)
  at Symfony\Component\Serializer\Serializer->normalize()
     (vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php:224)
  at Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->normalize()
     (vendor/symfony/serializer/Serializer.php:161)
  at Symfony\Component\Serializer\Serializer->normalize()
     (vendor/symfony/serializer/Normalizer/AbstractObjectNormalizer.php:224)
  at Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer->normalize()
     (vendor/symfony/serializer/Serializer.php:161)
  at Symfony\Component\Serializer\Serializer->normalize()
     (vendor/sonata-project/page-bundle/src/Entity/Transformer.php:100)
  at Sonata\PageBundle\Entity\Transformer->create()
     (vendor/sonata-project/page-bundle/src/Admin/SnapshotAdmin.php:76)
  at Sonata\PageBundle\Admin\SnapshotAdmin->prePersist()
     (vendor/sonata-project/admin-bundle/src/Admin/AbstractAdmin.php:404)
  at Sonata\AdminBundle\Admin\AbstractAdmin->create()
     (vendor/sonata-project/admin-bundle/src/Controller/CRUDController.php:592)
  at Sonata\AdminBundle\Controller\CRUDController->createAction()
     (vendor/symfony/http-kernel/HttpKernel.php:163)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw()
     (vendor/symfony/http-kernel/HttpKernel.php:75)
  at Symfony\Component\HttpKernel\HttpKernel->handle()
     (vendor/symfony/http-kernel/Kernel.php:202)
  at Symfony\Component\HttpKernel\Kernel->handle()
     (vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php:35)
  at Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner->run()
     (vendor/autoload_runtime.php:35)
  at require_once('/var/www/clients/client1/web5/private/vendor/autoload_runtime.php')
     (/var/www/clients/client1/web5/web/index.php:5)       

cc @Hanmac (#1522)

Seems like one issue with the normalizer is the fact that it normalize every field and not only the one needed.
Maybe we need to specify a group ?

@mesiarm as first solution, you could mark your property with @Ignore

@VincentLanglet we need to think if the properties should be a white (Group) or black list (Ignore)

@mesiarm as first solution, you could mark your property with @Ignore

@VincentLanglet we need to think if the properties should be a white (Group) or black list (Ignore)

Yeah @Ignore resolved probem, thank you