-
-
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
Question: How to handle de-/serializing unions (using native type hints)? #1502
Comments
Hello, You can set up Type Annotation to state what kind of type it should deserialize too however. |
Hi, thanks for the quick reply!
I don't quite understand how I'm supposed to declare that union using type annotations other than what I've tried. Can you please elaborate on this? Background: I'm working on an API client for a 3rd party vendor API I don't have any control over. ExampleRequest: GET /api/foo Response: {
"bar": "bar is collapsed, you can expand it using ?expand=bar"
} Request: GET /api/foo?expand=bar Response: {
"bar": [
{ "id": 1, "message": "Now suddenly" },
{ "id": 2, "message": "bar is an array" },
{ "id": 3, "message": "of objects" },
]
} Unfortunately I can't solve this by having just a second method on my client since there's multiple fields that can be "expanded" that way. To be precise we're talking about 6 fields. |
Hello! Looking at the code it looks like as of 3.26 version this package has limited support.
class ClassWithUnionType
{
#[\JMS\Serializer\Annotation\Type('expandable<ComplexType>')]
public string|array $propertyUnderTest;
}
|
Hi! Sorry for the late reply. Currently I'm declaring the expandable types as $type = [
'name' => 'expandable',
'params' => [
[
'name' => 'array',
'params' => [
[
'name' => 'ComplexType',
'params' => [],
],
],
],
],
]; Now what I'd like to do is (pseudo code): class ExpandableHandler
{
// ...
public function deserializeExpandableFromJson(JsonDeserializationVisitor $visitor, $expandableData, array $type, Context $context)
{
// since in the collapsed state, $expandableData is guaranteed to be a string, we can return it without further processing
if ($this->isCollapsed($expandableData)) {
return $expandableData;
}
// delegate back with an unwrapped type
return $this->whatMethodToCallHere($visitor, $expandableData, $type['params'][0], $context);
// ^
// Basically "unwrap" the expandable type here
}
} Aside: The reason I'm declaring the type with an explicit array is, that I've found out that certain expandable fields won't expand into an array of complex types but a singular complex type instead. |
Nevermind, I've figured it out looking at the Thanks for the support guys! |
cc: @dgafka
According to #1330 we should have some capabilities of handling union types.
UnionHandler
the only/best way to get this working?I could imagine that especially deserialization is tricky since some pattern matching would have to be done in order to infer the actual desired type. Even then ambiguities will remain an issue if complex type definitions overlap.
I thought about implementing a custom handler for this, supporting only two arguments as a union and restricting the first argument to a primitive type to mitigate those pattern matching issues.
Would you be interested in a contribution to this project for such a restricted union support?
Steps required to reproduce the problem
Expected Result
Actual Result
Aside:
GitHub Discussions may be a perfect fit for those kind of questions. Is there any chance we get those enabled for this repo?
The text was updated successfully, but these errors were encountered: