Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

MetadataMap does not honor optional route parameters #5

@ghost

Description

In our expressive application we use one route definition for a whole endpoint. HAL's metadata map does not recognize optional parameters in this setup and complains about URIs it can't create for paginated list views.

Code to reproduce the issue

// PipelineAndRoutesDelegator.php
$app->route(
    '/book[/:id]',
    [
        BookAction::class
    ],
    [
        'GET', 'POST', 'PUT', 'DELETE', 'PATCH'
    ],
    'book'
);

// ConfigProvider.php
MetadataMap::class => [
    [
        '__class__' => RouteBasedResourceMetadata::class,
        'resource_class' => Book::class,
        'route' => 'book',
        'extractor' => ClassMethods::class,
    ],
    [
        '__class__' => RouteBasedCollectionMetadata::class,
        'collection_class' => BookCollection::class,
        'collection_relation' => 'book',
        'route' => 'book',
    ],
],

Expected results

{
  "_total_items": 245,
  "_page": 1,
  "_page_count": 25,
  "_links": {
    "self": {
      "href": "http://localhost:8080/book?page=1"
    }
  },

Actual results

{
  "code": 0,
  "message": "Missing parameter \"id\"",
  "trace": "#0 /…/vendor/zendframework/zend-router/src/Http/Segment.php(328): Zend\\Router\\Http\\Segment->buildPath(Array, Array, true, true, Array)\n#1
…
  {main}"
}

I temporarily resolved this by adding

'route_params' => [
    'id' => 0,
]

to the defintition of the collection. But then the generated link looks like this. It's functional, but not pretty:

"_links": {
  "self": {
    "href": "http://localhost:8080/book/0?page=1"
  }
},

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions