Closed ruudk closed 1 year ago
@goetas Now that that PR is merged, is it fixed? I didn't test yet.
it should be. can you verify? you need this release of the bundle https://github.com/schmittjoh/JMSSerializerBundle/releases/tag/5.2.0
I have the latest version of both packages. The problem is still there.
I think the issue is related to DoctrineObjectConstructor.php:72 not supporting enums yet.
I verified that \JMS\Serializer\Handler\EnumHandler::serializeEnum
is called upon serialization. But in deserialization, it never calls \JMS\Serializer\Handler\EnumHandler::deserializeEnum
On serialization, it calls \JMS\SerializerBundle\Debug\TraceableHandlerRegistry::getHandler
with $typeName = 'enum'
.
On deserialization, it calls the getHandler
again but this time, the $typeName = 'FQCN\ToTheEnum'
Did you maybe explicitly specify the type in the Metadata? It would be great if you could reproduce the issue? The doctrine constructor should never be used for enums
I don't use #[Type]
attributes, it's all inferred from typed properties. Maybe that's the issue?
enum SocialPlatform : string
{
case Spotify = 'spotify';
case Instagram = 'instagram';
}
class MyEvent {
public function __construct(
private int $userId,
private SocialPlatform $socialPlatform,
) {
}
}
I now understand why the typeName = 'enum'
on serialization, because of https://github.com/schmittjoh/serializer/blob/master/src/EventDispatcher/Subscriber/EnumSubscriber.php
Shouldn't the same be done for deserialization?
When I manually type it does work properly:
class MyEvent {
public function __construct(
private int $userId,
#[Type("enum<'SocialPlatform', 'value'>")]
private SocialPlatform $socialPlatform,
) {
}
}
I think that the $typeName
of an enum should always become enum
with additionally the fqcn
. Then deserialization becomes easier, as it's not confused with normal objects.
You are right, I totally forgot about the de serializeation listener ππ
It can happen. Do you have time to add it? Or do you want me to try it?
I will not have time this morning, probably in the evening. If you want to try it will be certainly helpful
I'm having a hard time understanding where the problem is. Because when I check the JsonSerializationTest I see that it already provides a lot of tests for enums. And that BaseSerializationTest::testEnum also tests deserialization.
That just passes fine.
So I suspect there is something wrong with JMSSerializerBundle. As that is what I'm using.
Maybe the problem is that the Subscriber only changes the typeName to enum
on serializer.pre_serialize
. But that this type should be stored in the metadata (and cached). Because when I set a breakpoint in TraceableMetadataFactory::getMetadataForClass
I see that when it loads MyEvent
, the $socialPlatform
has PropertyMetadata
with $type
set to [name: 'SocialPlatform', params: []]
.
The problem is this: https://github.com/schmittjoh/serializer/blob/0fa5e4f332fdce524aef5e5d4572d98d30168437/src/Metadata/Driver/EnumPropertiesDriver.php#L42-L45
It seems that the inner driver already provides the type ([name: 'SocialPlatform', params: []]
).
The driver that causes this is the TypedPropertiesDriver
.
Caused by https://github.com/schmittjoh/serializer/blob/0fa5e4f332fdce524aef5e5d4572d98d30168437/src/Metadata/Driver/TypedPropertiesDriver.php#L120-L121 because class_exists(SocialPlatform)
returns true
π
@ruudk the fix is available in https://github.com/schmittjoh/JMSSerializerBundle/pull/919 could you please test it? cc @simPod
@scyzoryck I think it addresses https://github.com/schmittjoh/serializer/pull/1463 as well, what do you think?
Just tested it, still not working. https://github.com/schmittjoh/JMSSerializerBundle/pull/919#issuecomment-1415216916
Don't understand why this is closed as I tested the PR and said it was not working.
I'm sorry, I missed that, π
Tried it again today, and it seems to be working fine. Thank you! π
I'm super grateful for the work on enums in https://github.com/schmittjoh/serializer/pull/1448. I'm just testing it out with
jms/serializer v3.20.0
together withjms/serializer-bundle dev-enable-enum
and noticed something wrong.Steps required to reproduce the problem
Expected Result
No error, deserialized back to an object.
Actual Result