JMS Serializer ignores mappings for Knp Paginator

I have a problem with excluding some Paginator KNP properties using JMS Serializer.

Firstly, it is included in composer.json

... "jms/serializer-bundle": "~0.13", "knplabs/knp-paginator-bundle": "2.4.*@dev", ... 

I paginate the CrmContacts policy and the exclusion policy for this object. I also added a yml file for KNP Paginator as follows:

config.yml

 jms_serializer: metadata: directories: KNPPB: namespace_prefix: 'Knp\\Bundle\\PaginatorBundle' path: %kernel.root_dir%/Resources/serializer/Knp 

inside the application / Resources / Serializer / Knp folder I created Pagination.SlidingPagination.yml:

 Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination: exclusion_policy: ALL properties: items: expose: true access_type: public_method accessor: getter: getItems type: array serialized_name: payload currentPageNumber: expose: true serialized_name: page numItemsPerPage: expose: true serialized_name: items totalCount: expose: true serialized_name: totalItems 

and this is the logic for returning serialized data:

 public function getContactsAction(Request $request) { $limit = $request->query->getInt('l', 10); $page = $request->query->getInt('p', 1); $serializer = $this->get('jms_serializer'); $contacts = $this->getDoctrine() ->getManager() ->getRepository('AcmeContactsBundle:CrmContact') ->getContacts(); $paginator = $this->get('knp_paginator'); $pagination = $paginator->paginate( $contacts, $page, $limit ); return new Response( $serializer->serialize( $pagination, 'json', SerializationContext::create()->setGroups(['Default']) ), Response::HTTP_OK, [ 'Content-Type' => 'application/json', ] ); } 

Unfortunately, I get all the properties from Knp Paginator in response:

 { "currentPageNumber": 1, "numItemsPerPage": 10, "items": [ { "id": 1, ... }, { "id": 2, ... }, { "id": 3, ... } ], "totalCount": 3, "paginatorOptions": { "pageParameterName": "page", "sortFieldParameterName": "sort", "sortDirectionParameterName": "direction", "filterFieldParameterName": "filterField", "filterValueParameterName": "filterValue", "distinct": true }, "customParameters": [], "route": "acmeContactsGetContacts", "params": [], "pageRange": 5, "template": "KnpPaginatorBundle:Pagination:sliding.html.twig", "sortableTemplate": "KnpPaginatorBundle:Pagination:sortable_link.html.twig", "filtrationTemplate": "KnpPaginatorBundle:Pagination:filtration.html.twig" } 
+7
jmsserializerbundle knppaginator
source share
2 answers

The properties you want to map belong to Knp \ Component \ Pager \ Pagination \ AbstractPagination.

You also want to hide the rest of the properties, so you have to tweak both classes.

I just tried the following and it works for me.


application / Config / config.yml

 jms_serializer: metadata: directories: KnpPaginatorBundle: namespace_prefix: Knp\Bundle\PaginatorBundle path: %kernel.root_dir%/config/serializer/KnpPaginatorBundle KnpPager: namespace_prefix: Knp\Component\Pager path: %kernel.root_dir%/config/serializer/KnpPager 

application / Config / serializer / KnpPager / Pagination.AbstractPagination.yml

 Knp\Component\Pager\Pagination\AbstractPagination: exclusion_policy: ALL properties: items: expose: true access_type: public_method accessor: getter: getItems type: array serialized_name: payload currentPageNumber: expose: true serialized_name: page numItemsPerPage: expose: true serialized_name: items totalCount: expose: true serialized_name: totalItems 

application / Config / serializer / KnpPaginatorBundle / Pagination.SlidingPagination.yml

 Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination: exclusion_policy: ALL 

Remember to clear the cache before testing.

Hope this helps you.

+16
source share

Instead of serializing the entire pagination object, try serializing only data and elements, for example:

 $result = array( 'data' => $pagination->getItems(), 'meta' => $pagination->getPaginationData()); return new Response( $serializer->serialize( $result, 'json', SerializationContext::create()->setGroups(['Default']) ), Response::HTTP_OK, ['Content-Type' => 'application/json',] ); 
+3
source share

All Articles