Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • 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
@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