Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dsbaars/56be85286ebf4f89e4d4 to your computer and use it in GitHub Desktop.
Save dsbaars/56be85286ebf4f89e4d4 to your computer and use it in GitHub Desktop.
JMSSerializerBundle groups and Hateoas
Hateoas\Representation\CollectionRepresentation:
exclusion_policy: all
exclude: false
expose: true
properties:
rel:
exclude: false
expose: true
groups: [ Default, Hateoas ]
resources:
exclude: false
expose: true
groups: [ Default, Hateoas ]
Hateoas\Representation\PaginatedRepresentation:
custom_accessor_order: [page, limit, pages, total]
relations:
- rel: self
href:
route: expr(object.getRoute())
parameters:
parameters:
page: expr(object.getPage())
_format: json
absolute: expr(object.isAbsolute())
exclusion:
groups: [Hateoas]
- rel: first
href:
route: expr(object.getRoute())
parameters:
page: 1
_format: json
absolute: expr(object.isAbsolute())
exclusion:
groups: [Hateoas]
- rel: last
href:
route: expr(object.getRoute())
parameters:
page: expr(object.getPages())
_format: json
absolute: expr(object.isAbsolute())
exclusion:
groups: [Hateoas]
exclude_if: expr(object.getPages() === null)
- rel: next
href:
route: expr(object.getRoute())
parameters:
page: expr(object.getPage()+1)
_format: json
absolute: expr(object.isAbsolute())
exclusion:
groups: [Hateoas]
exclude_if: expr(object.getPages() !== null && (object.getPage() + 1) > object.getPages())
- rel: previous
href:
route: expr(object.getRoute())
parameters:
page: expr(object.getPage()-1)
_format: json
absolute: expr(object.isAbsolute())
exclusion:
groups: [Hateoas]
exclude_if: expr((object.getPage() - 1) < 1)
properties:
page:
expose: true
groups: [Hateoas]
limit:
expose: true
groups: [Hateoas]
pages:
expose: true
groups: [Hateoas]
total:
expose: true
groups: [Hateoas]
Hateoas\Representation\RouteAwareRepresentation:
properties:
inline:
expose: true
groups: [ Default, Hateoas ]
serialized_name: _embedded
@bruno-ds
Copy link

Hello,

thank you for this gist, you missed :

Hateoas\Representation\AbstractSegmentedRepresentation:
    properties:
        limit:
            expose: true
            groups: [Hateoas]
        total:
            expose: true
            groups: [Hateoas]

@smuralidharan
Copy link

Hi I did the same but i am getting empty response.

$serializer = HateoasBuilder::create()
->setUrlGenerator(null, $queryStringUrlGenerator)
->addMetadataDir(DIR . '/../Resources/config/serializer/Hateoas', 'Hateoas\Representation')->build()
$collection = new CollectionRepresentation($users);
$json = $serializer->serialize($collection, 'json', SerializationContext::create()->setGroups(['users']));

Metadata FileName:
Representation.CollectionRepresentation.yml

Hateoas\Representation\CollectionRepresentation:
exclusion_policy: none
properties:
id:
type: integer
groups: ['users']

Any suggestions please?

@igormukhingmailcom
Copy link

Right CollectionRepresentation.yml looks like this for me:

Hateoas\Representation\CollectionRepresentation:
    exclusion_policy: all
    expose: true
    virtual_properties:
        getResources:
            serialized_name: items
            expose: true
            groups: [ Default, Hateoas ]

@igormukhingmailcom
Copy link

Current PaginatedRepresentation.yml make an issue and any API endpoint throw error when route have any parameter.

For example: Some mandatory parameters are missing (\"productCode\") to generate a URL for route \"sylius_admin_api_product_variant_index\"."

Fixed PaginatedRepresentation.yml should be like this (add page parameters rather than replace them):

Hateoas\Representation\PaginatedRepresentation:
    exclusion_policy: ALL
    expose: false
    custom_accessor_order: [page, limit, pages, total]
    relations:
      - rel: self
        href:
          route: expr(object.getRoute())
          parameters: "expr(object.getParameters() + {page: object.getPage()})"
          absolute: expr(object.isAbsolute())
        exclusion:
          groups: [ Default, Hateoas ]
      - rel: first
        href:
          route: expr(object.getRoute())
          parameters: "expr(object.getParameters() + {page: 1})"
          absolute: expr(object.isAbsolute())
        exclusion:
          groups: [ Default, Hateoas ]
      - rel: last
        href:
          route: expr(object.getRoute())
          parameters: "expr(object.getParameters() + {page: object.getPages()})"
          absolute: expr(object.isAbsolute())
        exclusion:
          groups: [ Default, Hateoas ]
          exclude_if: expr(object.getPages() === null)
      - rel: next
        href:
          route: expr(object.getRoute())
          parameters: "expr(object.getParameters() + {page: object.getPage()+1})"
          absolute: expr(object.isAbsolute())
        exclusion:
          groups: [ Default, Hateoas ]
          exclude_if: expr(object.getPages() !== null && (object.getPage() + 1) > object.getPages())
      - rel: previous
        href:
          route: expr(object.getRoute())
          parameters: "expr(object.getParameters() + {page: object.getPage()-1})"
          absolute: expr(object.isAbsolute())
        exclusion:
          groups: [ Default, Hateoas ]
          exclude_if: expr((object.getPage() - 1) < 1)
    properties:
        page:
            expose: true
            groups: [ Default, Hateoas ]
        limit:
            expose: true
            groups: [ Default, Hateoas ]
        pages:
            expose: true
            groups: [ Default, Hateoas ]
        total:
            expose: true
            groups: [ Default, Hateoas ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment