Comments (13)
Investigated some further: $resourceKey holds the value you can supply as third parameter when calling collection/item methods. While serializing the $resourceKey will be the supplied value if $data contains the root otherwise it will be an empty string.
This indeed changed in v0.20.
If you adjust the above code to below it will serialize as expected:
return empty($resourceKey) ? $data : [$resourceKey => $data]
from fractal.
Thanks for your patience while I put this together. Here's a pretty basic PHP repro that has distilled the framework code out: https://github.com/dwightwatson/fractal-issue
The tests pass on the 0.19
branch, but they fail on the 0.20
branch.
You can see the only changes in the branches apart from a junit report I accidently committed are:
- Bumping the Fratcal version with Composer
- Adhering to the require typehints on the seralizer and transformer attributes
Hope this helps, please let me know if there is more I can do to help here.
from fractal.
Looks good to me - thanks so much getting to the bottom of this and shipping a fix! 🤟
from fractal.
I have exactly the same 'root' serializer. Seems the $resourceKey is now always an empty string, while until v0.19.2 it was FALSE or NULL.
from fractal.
@dwightwatson since the RootSerializer is custom and extending the DataArraySerializer, can you provide more information such as how you are calling so I can try to recreate this on my machine?
cc/ @chris-rs
from fractal.
I'll have re-confirm because it's been a long time since I wrote this code - but I believe the intention was that the root level item in the response was nested under a data
key, but every other includes is nested directly under it's name - not under another data
key.
You can see in my example above you used to be able to get data.subject.id
. Without my custom serializer it would have been data.subject.data.id
which was what I was trying to avoid.
Let me know if that makes sense - alternatively perhaps I can put together a sample repo that demonstrates the break?
from fractal.
@matthewtrask the serializer works like @dwightwatson describes. I also implemented this years ago. The trick is that we like to add the 'data' value in front of root items and strip it in nested structures.
I think that the problem is here that the code was refactored with typed parameters.
Until v0.19:
public function collection($resourceKey, $data)
$resourceKey can be FALSE or the supplied key value and NULL if empty, which is in my humble opinion an unwanted flexibility in PHP. Since a while type hinting can be used which prohibits this.
Since v0.20:
public function collection(string $resourceKey, array $data): array
$resourceKey is now forced to hold string values which is a good thing! But this breaks with old implementations that expect it to be FALSE, NULL or the supplied key value. The 'good' thing about the v0.19 implementation is that you could detect root items from nested items in $data. $resourceKey only held the key value (or NULL) if $data contains the root, otherwise it is FALSE.
Since v0.20 we only get the string $value (supplied key or NULL) and have no way to detect root items anymore.
Hope this makes sense.
from fractal.
alright, let me try to write a test to see if the fix I have in mind will work. Will probably be later today for me after work.
from fractal.
@dwightwatson "alternatively perhaps I can put together a sample repo that demonstrates the break?" do you mind doing this? Even just something simple would be super helpful. im trying to write some breaking tests but not quite getting it to break the way you are describing. Im probably just missing something super obvious but the sample repo would be a huge help if possible.
from fractal.
@dwightwatson this is fantastic, thank you so much for this and thank you both for your patience as I look into this,
from fractal.
@dwightwatson @chris-rs alright I have a solution. Not super happy with it, but it works. I will write a test in the main library today and cut a 0.20.1 release for yall to have. In fact I think I can just take your test from the example @dwightwatson if that works for you.
from fractal.
Yeah of course, happy for you to use that code however you need.
from fractal.
@dwightwatson @chris-rs thank you both for your help and patience. here is the new release that should fix the issue!
https://github.com/thephpleague/fractal/releases/tag/0.20.1
from fractal.
Related Issues (20)
- How to remove the 'self' link from a relationship links object when using the JsonApiSerializer HOT 2
- [JsonApiSerializer] A way to return requested realtionship without attributes and links HOT 2
- Filters on relationships HOT 4
- How to transform when Parent Child Relation in same Table HOT 1
- Sparse Fieldsets without calculations HOT 2
- Modernize like it's 1.0 HOT 3
- What a current state of this project? PHP8 support plans? HOT 3
- Scope class toArray() method takes too much time HOT 4
- Possible incorrect pagination output from JsonApiSerializer HOT 2
- Support 7.4
- How to create custom ArraySerializer with the result of meta pagination array inside the collection array HOT 2
- Please use SEMVER on this package! HOT 7
- Breaking change between 0.19.2 and 0.20.0 HOT 1
- Update version 0.19.2 -> 0.19.3
- How to set `includes` for specific `includeItem` in the code for transformer?
- How to leave `data` resource key only for root transformer? HOT 1
- [Suggestion] Use a container to fetch transformers HOT 1
- How to make output of nested array
- Scope.php::fireTransformer is unable to handle `null` $transformer
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 fractal.