Skip to content

Commit 208629e

Browse files
committed
API: Some changes to tag API endpoints
- Updated tag values endpoint to use query param instead of path argument, so a better range of values can be provided (including those with slashes). - Updated image gallery example request to align with docs use changes.
1 parent 346dc27 commit 208629e

File tree

5 files changed

+22
-8
lines changed

5 files changed

+22
-8
lines changed

app/Activity/Controllers/TagApiController.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use BookStack\Activity\TagRepo;
88
use BookStack\Http\ApiController;
99
use Illuminate\Http\JsonResponse;
10+
use Illuminate\Http\Request;
1011

1112
/**
1213
* Endpoints to query data about tags in the system.
@@ -21,6 +22,15 @@ public function __construct(
2122
) {
2223
}
2324

25+
protected function rules(): array
26+
{
27+
return [
28+
'listValues' => [
29+
'name' => ['required', 'string'],
30+
],
31+
];
32+
}
33+
2434
/**
2535
* Get a list of tag names used in the system.
2636
* Only the name field can be used in filters.
@@ -38,18 +48,21 @@ public function listNames(): JsonResponse
3848
}
3949

4050
/**
41-
* Get a list of tag values, which have been set for the given tag name.
51+
* Get a list of tag values, which have been set for the given tag name,
52+
* which must be provided as a query parameter on the request.
4253
* Only the value field can be used in filters.
4354
*/
44-
public function listValues(string $name): JsonResponse
55+
public function listValues(Request $request): JsonResponse
4556
{
46-
$tagQuery = $this->tagRepo
47-
->queryWithTotalsForApi($name);
57+
$data = $this->validate($request, $this->rules()['listValues']);
58+
$name = $data['name'];
59+
60+
$tagQuery = $this->tagRepo->queryWithTotalsForApi($name);
4861

4962
return $this->apiListingResponse($tagQuery, [
5063
'name', 'value', 'usages', 'page_count', 'chapter_count', 'book_count', 'shelf_count',
5164
], [], [
52-
'name', 'value',
65+
'value',
5366
]);
5467
}
5568
}
File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GET /api/tags/values-for-name?name=Category

routes/api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
Route::get('system', [SystemApiController::class, 'read']);
112112

113113
Route::get('tags/names', [TagApiController::class, 'listNames']);
114-
Route::get('tags/name/{name}/values', [TagApiController::class, 'listValues']);
114+
Route::get('tags/values-for-name', [TagApiController::class, 'listValues']);
115115

116116
Route::get('users', [UserApiController::class, 'list']);
117117
Route::post('users', [UserApiController::class, 'create']);

tests/Api/TagsApiTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function test_list_values_returns_values_for_set_tag()
7676
$booksToTag->each(fn (Book $book) => $book->tags()->save(new Tag(['name' => 'MyValueApiTag', 'value' => 'tag-book' . $book->id])));
7777
$chaptersToTag->each(fn (Chapter $chapter) => $chapter->tags()->save(new Tag(['name' => 'MyValueApiTag', 'value' => 'tag-chapter' . $chapter->id])));
7878

79-
$resp = $this->actingAsApiEditor()->getJson('api/tags/name/MyValueApiTag/values');
79+
$resp = $this->actingAsApiEditor()->getJson('api/tags/values-for-name?name=MyValueApiTag');
8080

8181
$resp->assertStatus(200);
8282
$resp->assertJson(['total' => 18]);
@@ -101,7 +101,7 @@ public function test_list_values_is_limited_by_permission_visibility(): void
101101
$this->permissions->disableEntityInheritedPermissions($pagesToTag[3]);
102102
$this->permissions->disableEntityInheritedPermissions($pagesToTag[6]);
103103

104-
$resp = $this->actingAsApiEditor()->getJson('api/tags/name/MyGreatApiTag/values');
104+
$resp = $this->actingAsApiEditor()->getJson('api/tags/values-for-name?name=MyGreatApiTag');
105105
$resp->assertStatus(200);
106106
$resp->assertJson(['total' => 8]);
107107
$resp->assertJsonMissing(['value' => 'cat' . $pagesToTag[3]->id]);

0 commit comments

Comments
 (0)