-
-
Notifications
You must be signed in to change notification settings - Fork 586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error serializing union types between class and boolean value types #1568
Comments
gregtyler
changed the title
Error serialising union types between class and boolean value types
Error serializing union types between class and boolean value types
Nov 11, 2024
Hi! It looks like it will work :) I can help with reviewing the PR. |
gregtyler
added a commit
to gregtyler/serializer
that referenced
this issue
Nov 11, 2024
Allows serialization of value types (i.e. `true` and `false`, as opposed to `bool`), including in union types (e.g. `string|false`). Otherwise, tries to create class with FQCN "true" and, for union types, filters out value types as non-primitive. Fixes schmittjoh#1568
Thanks, I've created #1569. In doing so I realised that it also doesn't work for non-union value types, so I've generalised the PR slightly to acknowledge that. For example, the following: class TestClass {
public false $falsableProp = false;
}
$serializer = JMS\Serializer\SerializerBuilder::create()->build();
$jsonContent = $serializer->serialize(new TestClass(), 'json'); Currently fails with:
|
gregtyler
added a commit
to gregtyler/serializer
that referenced
this issue
Nov 12, 2024
Allows serialization of value types (i.e. `true` and `false`, as opposed to `bool`), including in union types (e.g. `string|false`). Otherwise, tries to create class with FQCN "true" and, for union types, filters out value types as non-primitive. Fixes schmittjoh#1568
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As of 3.31.0, if you try to serialize a method that is a union type between a class a boolean value type (e.g.
MyClass|false
), JMS Serializer raises an exception.This is because the value type is filtered out by
TypedPropertiesDriver->shouldTypeHint()
(becausetrue
andfalse
aren't in the allowlist) so the only acceptable value becomes "MyClass".Steps required to reproduce the problem
Expected Result
Actual Result
Exception thrown:
Workarounds
Changing the typehint to
MyClass|bool
fixes this, though means you're opening your type wider than needed.Fix
I believe this can be fixed with two changes:
'true'
and'false'
to the TypedPropertiesDriver whitelist'true'
and'false'
as cases to the graph navigator boolean acceptor (without, the navigator treats value types as objects)I'm happy to raise a PR for this, but I'm not very familiar with the nuances of the codebase so I'm not confident this won't cause other issues.
The text was updated successfully, but these errors were encountered: