Skip to content

Commit 2bb4a2c

Browse files
committed
Added countable and paginatable (works for now but should be re-visited)
1 parent 3be10f0 commit 2bb4a2c

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Endpoint/Concerns/BuildsOpenApiPaths.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Tobyz\JsonApiServer\Endpoint\Index;
88
use Tobyz\JsonApiServer\JsonApi;
99
use Tobyz\JsonApiServer\Resource\Collection;
10-
use Tobyz\JsonApiServer\Resource\Listable;
1110
use Tobyz\JsonApiServer\Resource\Resource;
1211
use Tobyz\JsonApiServer\Schema\Field\Field;
1312
use Tobyz\JsonApiServer\Schema\Field\Relationship;
@@ -21,6 +20,8 @@ private function buildOpenApiContent(
2120
bool $multiple = false,
2221
bool $included = true,
2322
bool $links = false,
23+
bool $countable = false,
24+
bool $paginatable = false,
2425
): array {
2526
$item = count($resources) === 1 ? $resources[0] : ['oneOf' => $resources];
2627

@@ -33,6 +34,7 @@ private function buildOpenApiContent(
3334
'links' => $links ? $this->buildLinksObject($name) : [],
3435
'data' => $multiple ? ['type' => 'array', 'items' => $item] : $item,
3536
'included' => $included ? ['type' => 'array'] : [],
37+
'meta' => $this->buildMetaObject($countable, $paginatable),
3638
]),
3739
],
3840
],
@@ -79,6 +81,27 @@ private function buildLinksObject(string $name): array
7981
];
8082
}
8183

84+
private function buildMetaObject(bool $countable, bool $paginatable): array
85+
{
86+
if (!($countable || $paginatable)) {
87+
return [];
88+
}
89+
90+
return [
91+
'type' => 'object',
92+
'properties' => [
93+
'page' => [
94+
'type' => 'object',
95+
'properties' => array_filter([
96+
'total' => $countable ? ['type' => 'integer'] : [],
97+
'limit' => $paginatable ? ['type' => 'integer'] : [],
98+
'offset' => $paginatable ? ['type' => 'integer'] : [],
99+
]),
100+
],
101+
],
102+
];
103+
}
104+
82105
/**
83106
* @throws ReflectionException
84107
*/

src/Endpoint/Index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ public function getOpenApiPaths(Collection $collection): array
199199
),
200200
multiple: true,
201201
links: true,
202+
countable: true,
203+
paginatable: true,
202204
),
203205
],
204206
'400' => $this->buildBadRequestErrorResponse(),

0 commit comments

Comments
 (0)