API Error: getPostsAction returns empty
delineas opened this issue · 23 comments
Hi!
If we call this API request:
/api/news/posts.json
We get empty result
In function getPostsAction(ParamFetcherInterface $paramFetcher) if we change the return from
return $pager;
to
return $pager->getResults();
This change resolves the problem. But, Is it correct?
Same problem here... I think ideally the API should be returning not only the results, but also the current page, the number of results and the link to fetch the next and previous page.
There is maybe a listener for that, but the Pager is not serializable so only an empty object is returned in JSON.
I searched and searched and searched… from news to core to datagrid back to news back to core back to datagrid again… and found this. This is the thing that does not do its job. Can you have a look into that?
I think i should work by configuring jms serializer properly. Can you show us your configuration?
I do not have any configuration for jms serializer (it is not needed for basic usage). What should I put un configuration ?
IMO you shouldn't have to do anything, the datagrid bundle should do it for you automatically.
But first let's try to make it work on your project :
jms_serializer:
metadata:
directories:
sonata_datagrid:
namespace_prefix: "Sonata\\DatagridBundle"
path: "@SonataDatagridBundle/Resources/config/serializer"
It is not working, even with this correct configuration and after enabling the DatagridBundle in the AppKernel
jms_serializer:
metadata:
directories:
sonata_datagrid:
namespace_prefix: "Sonata\\DatagridBundle"
path: "@SonataDatagridBundle/Resources/config/serializer"
Can you check if the directory of the xml file appears in this variable?
The function is not even called (I still have the configuration).
This one is not called either.
But by following another path, I found out the only drivers called are on JMSDiExtraBundle. This ConfiguredControllerInjectionsDriver is called and then driver calls the AnnotationDriver. But those two does not found any metadata.
That's another bundle that has to do with dependency injection. Let's move up then, until we find something that is executed. You said you registered the bundle. If you did so, then this should definitely be executed.
Yes this code is executed, and the DatagridBundle is in the $directories
variable and is correctly passed to the jms_serializer.metadata.file_locator
service.
Ok well that's a start… now let's try to figure out what is supposed to call the file locator and when.
Oh I remember now… the listener that should trigger all this is a fos rest listener, that should look for this annotation
Did you register the fos rest bundle?
Yes it is registered. The annotation looks to be working because the controller is returning a Response
. The first thing I though of is that maybe the serializerGroups are not used correctly.
The groups match those that appear in the XML metadata. Maybe just check if the jms serializer is called at all … maybe there is some priority problem and the symfony serializer is in use?
No symfony's serializer is correctly disabled.
Now I think the problem might come from FOSRestBundle. After upgrading FOSRestBundle to ^2.0, and updating all the annotations on Sonata's bundles (because there are a lot of BC on FOSRestBundle 2.0), the API is working correctly. So now I have to find a version of FOSRestBundle that does not require me to update Sonata's bundles.
Ok so I was right, the problem came from how FOSRestBundle was using the SeralizerBundle. I should have started by a composer update
because the latest stable version of FOSRestBundle (1.7.7) is working like a charm.
Thank you very much @greg0ire for your help and I am very sorry to have bothered you with this problem.
Glad you found a solution :) I'm closing this then.