Comments (5)
Hello!
Looking at the code it looks like as of 3.26 version this package has limited support.
- It does not work with
Type
annotation - works only with typed properties - It does not work with deserialisation.
To solve your problem I would suggest:
- Create some fake type like:
class ClassWithUnionType
{
#[\JMS\Serializer\Annotation\Type('expandable<ComplexType>')]
public string|array $propertyUnderTest;
}
- Register custom handler for your type that will check if your data is a string or array of objects. Inside you will get access to DeserializationVisitorInterface, and type, so you will be able to deserialise it to array of objects or to string.
from serializer.
Hello,
Serialization should work in this scenario, as JMS will know what type of reference is serialized object holding.
However for the deserialization JMS can't know which type it should pick for deserialization, therefore it will fail.
You can set up Type Annotation to state what kind of type it should deserialize too however.
from serializer.
Hi, thanks for the quick reply!
You can set up Type Annotation to state what kind of type it should deserialize too however.
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.
They have implemented a mechanism, that allows you to "expand" certain fields in the response. If those fields are not expanded, they have a string value. If they are expanded they contain an array of complex types.
Example
Request:
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.
That would add up to 2^6=64
possible combinations of whether to expand certain fields or not which is unfeasible bloat and complexity in the client implementation.
from serializer.
Hi!
Sorry for the late reply.
What I don't understand is how I can delegate to other handlers (or whatever the correct terminology is here).
Currently I'm declaring the expandable types as #[Type('expandable<array<ComplexType>>')]
which is giving me the type argument in my handler fn with the following shape (as expected):
$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.
from serializer.
Nevermind, I've figured it out looking at the EnumHandler.php
Thanks for the support guys!
from serializer.
Related Issues (20)
- Type Error in XmlDeserializationVisitor.php when switching to PHP 8 HOT 3
- The serializer cannot be installed in a Laravel version 10 project. HOT 2
- Update doctrine/instantiator to v2.x for PHP 8.1 HOT 5
- JMS serializer implements Symfony serializer HOT 4
- "Can't use incorrect type integer for collection" on PHPDoc-typed integer, boolean or double HOT 1
- stdClass Serialization Does Not Use Property Naming Strategy HOT 3
- Serialization of Symfony\Component\Mime\Address HOT 4
- Undefined property: JsonDeserializationVisitor::$navigator sence version 3.23 HOT 2
- The dependency on doctrine/annotations should become an optional one HOT 2
- Minor Issue from Triggering Autoload Before Expanding Class Names HOT 2
- ORM 3.0 Compat HOT 8
- Adding tideways spans inside JMS serializer HOT 5
- Serialization of objects with missing attribute errors HOT 2
- AccessType attribute constructor expects array HOT 3
- Feature request: Allow literal discriminator values HOT 2
- Use another field than "id" to find Doctrine entity HOT 3
- Change to optional Doctrine/Annotations package introduces a breaking change HOT 10
- Enum deserializer
- Missing fields are replaced with null HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from serializer.