Answer the question
In order to leave comments, you need to log in
For what reasons can the serializer not find the mapping?
"symfony/serializer": "^5.2",
serializer:
enabled: true
enable_annotations: true // пробовал и через аннотации, так же пустой массив
mapping:
paths: [ '%kernel.project_dir%/src/Infrastructure/Serializer/' ]
<?xml version="1.0" encoding="UTF-8" ?>
<serializer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://symfony.com/schema/dic/serializer-mapping"
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping
https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
>
<class name="App\Domain\AuthToken">
это поле лежит в абстрактном классе родителе 4 уровня выше
<attribute name="id">
<group>all</group>
</attribute>
это поле принадлежит AuthToken
<attribute name="isDeviceRemembered">
<group>all</group>
<group>test</group>
</attribute>
это поле лежит в абстрактном классе родителе 1 уровень выше
<attribute name="user">
<group>all</group>
<group>test</group>
</attribute>
</class>
</serializer>
Infrastructure/Serializer/AuthToken.xml
Infrastructure/Serializer/Domain/AuthToken.xml
$attributes = $this->extractAttributes($object, $format, $context); // === []
/**
* @return Generator|iterable|BundleInterface[]
*/
public function registerBundles() {
$contents = require $this->getProjectDir() . '/config/bundles.php';
foreach ($contents as $class => $envs) {
if (isset($envs['all']) || isset($envs[$this->environment])) {
yield new $class();
}
}
}
<?php
return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
Nelmio\CorsBundle\NelmioCorsBundle::class => ['all' => true],
Nelmio\ApiDocBundle\NelmioApiDocBundle::class => ['all' => true],
Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
Snc\RedisBundle\SncRedisBundle::class => ['all' => true],
DAMA\DoctrineTestBundle\DAMADoctrineTestBundle::class => ['test' => true],
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
Exercise\HTMLPurifierBundle\ExerciseHTMLPurifierBundle::class => ['all' => true],
];
Symfony\Component\Serializer\Mapping\ClassMetadata {#21134
+name: "App\Domain\AuthToken"
+attributesMetadata: array:25 [
"isDeviceRemembered" => Symfony\Component\Serializer\Mapping\AttributeMetadata {#26006
+name: "isDeviceRemembered"
+groups: []
+maxDepth: null
+serializedName: null
+ignore: false
}
use Symfony\Component\Serializer\SerializerInterface;
...
protected SerializerInterface $serializer;
....
$this->serializer->serialize($data, 'json', ['groups' => ['all', 'test']]), // === []
namespace App\Domain\Entity;
use App\Domain\ValueObject\TokenIsDeviceRemembered;
use App\Domain\AbstractUserToken;
use Symfony\Component\Serializer\Annotation\Groups;
class AuthToken extends AbstractUserToken implements UserInterface {
/**
* @Groups({"all", "test"})
*/
public TokenIsDeviceRemembered $isDeviceRemembered;
final class TokenIsDeviceRemembered extends AbstractVO {
/**
* @Groups({"all", "test"})
*/
public bool $isDeviceRemembered;
abstract class AbstractVO implements IValueObject, JsonSerializable {
Infrastructure/Serializer/App/Domain/AuthToken.xml
.... // аналогичное с точкой вместо слеша /
Infrastructure/Serializer/App.Domain.AuthToken.xml
Infrastructure/Serializer/App.Domain.AuthToken.Serializer.xml
Infrastructure/Serializer/App.Domain.AuthToken.Serializer.orm.xml
Infrastructure/Serializer/Domain.AuthToken.Serializer.xml
Infrastructure/Serializer/Domain.AuthToken.Serializer.orm.xml
Answer the question
In order to leave comments, you need to log in
1 - after each update of the mapping, you must manually demolish the cache folder. Perhaps there are commands that can update the cache
composer dump-autoload; php bin/console cache:clear
didn't work. array:1 [
0 => "all" // контекст
]
$serializedData"=== []
// Serializer/Token.xml
<attribute name="isValid">
<group>all</group>
<group>non_sensitive</group>
</attribute>
// Token.php
public function isValid(): bool {
return false === $this->isExpired();
}
<attribute name="isDeviceRemembered">
<group>all</group>
<group>non_sensitive</group>
</attribute>
public function isDeviceRemembered(): TokenIsDeviceRemembered {
return $this->isDeviceRemembered;
}
use JsonSerializable;
final class TokenIsDeviceRemembered implements IValueObject, JsonSerializable {
/**
* @throws JsonException
*/
/**
* @throws JsonException
*/
final public function jsonSerialize(): string {
if (true === is_object($this->value())) {
return json_encode($this->value(), JSON_THROW_ON_ERROR);
}
if (true === is_bool($this->value())) {
return $this->value() ? 'true' : 'false';
}
return $this->value();
}
<attribute name="isDeviceRemembered.isDeviceRemembered">
<group>all</group>
<group>non_sensitive</group>
</attribute>
public bool $isDeviceRemembered;
public function isDeviceRemembered(): bool {
return $this->isDeviceRemembered;
}
<attribute name="isDeviceRemembered">
<group>all</group>
<group>non_sensitive</group>
</attribute>
<attribute name="isDeviceRemembered.isDeviceRemembered">
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question