Skip to content
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

Open
gregtyler opened this issue Nov 11, 2024 · 2 comments · May be fixed by #1569
Open

Error serializing union types between class and boolean value types #1568

gregtyler opened this issue Nov 11, 2024 · 2 comments · May be fixed by #1569

Comments

@gregtyler
Copy link
Contributor

gregtyler commented Nov 11, 2024

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

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() (because true and false aren't in the allowlist) so the only acceptable value becomes "MyClass".

Steps required to reproduce the problem

class SubClass {
}

class TestClass {
    public SubClass|false $falsableProp = false;
}

$serializer = JMS\Serializer\SerializerBuilder::create()->build();
$jsonContent = $serializer->serialize(new TestClass(), 'json');
echo $jsonContent;

Expected Result

{"falsable_prop":false}

Actual Result

Exception thrown:

JMS\Serializer\Exception\InvalidArgumentException: Value at TestClass is expected to be an object of class SubClass but is of type boolean

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:

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.

@gregtyler 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
@scyzoryck
Copy link
Collaborator

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
@gregtyler gregtyler linked a pull request Nov 11, 2024 that will close this issue
@gregtyler
Copy link
Contributor Author

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:

ReflectionException: Class "false" does not exist

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
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants