Skip to content

Commit 53285c4

Browse files
authored
Merge pull request #185 from jr3cermak/master
1st attempt at adding exclude= (issue #136) try#2
2 parents 7edb0a2 + 97a593c commit 53285c4

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

api.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,28 @@ protected function addRelationColumns($columns,$select) {
15271527
return $columns;
15281528
}
15291529

1530+
protected function excludeFields($fields,$exclude) {
1531+
if ($fields) {
1532+
$columns = explode(',',$exclude);
1533+
foreach($columns as $column) {
1534+
$table = "";
1535+
if (strpos($column,'.')) {
1536+
$key = explode('.',$column);
1537+
$table = $key[0];
1538+
$column = $key[1];
1539+
} else {
1540+
if (count($fields)==1) {
1541+
$table = array_keys($fields)[0];
1542+
}
1543+
}
1544+
if (array_key_exists($table,$fields) and array_key_exists($column,$fields[$table])) {
1545+
unset($fields[$table][$column]);
1546+
}
1547+
}
1548+
}
1549+
return $fields;
1550+
}
1551+
15301552
protected function findFields($tables,$columns,$database) {
15311553
$fields = array();
15321554
foreach ($tables as $i=>$table) {
@@ -1609,6 +1631,7 @@ protected function getParameters($settings) {
16091631
$table = $this->parseRequestParameter($request, 'a-zA-Z0-9\-_');
16101632
$key = $this->parseRequestParameter($request, 'a-zA-Z0-9\-_,'); // auto-increment or uuid
16111633
$action = $this->mapMethodToAction($method,$key);
1634+
$exclude = $this->parseGetParameter($get, 'exclude', 'a-zA-Z0-9\-_,.');
16121635
$include = $this->parseGetParameter($get, 'include', 'a-zA-Z0-9\-_,');
16131636
$page = $this->parseGetParameter($get, 'page', '0-9,');
16141637
$filters = $this->parseGetParameterArray($get, 'filter', false);
@@ -1628,6 +1651,7 @@ protected function getParameters($settings) {
16281651
list($tables,$collect,$select) = $this->findRelations($tables,$database,$auto_include);
16291652
$columns = $this->addRelationColumns($columns,$select);
16301653
$fields = $this->findFields($tables,$columns,$database);
1654+
if (isset($exclude)) $fields = $this->excludeFields($fields,$exclude);
16311655

16321656
// permissions
16331657
if ($table_authorizer) $this->applyTableAuthorizer($table_authorizer,$action,$database,$tables);
@@ -2084,6 +2108,13 @@ protected function swagger($settings) {
20842108
if ($action['name']=='list') {
20852109
echo '"parameters":[';
20862110
echo '{';
2111+
echo '"name":"exclude",';
2112+
echo '"in":"query",';
2113+
echo '"description":"One or more related entities (comma separated).",';
2114+
echo '"required":false,';
2115+
echo '"type":"string"';
2116+
echo '},';
2117+
echo '{';
20872118
echo '"name":"include",';
20882119
echo '"in":"query",';
20892120
echo '"description":"One or more related entities (comma separated).",';

tests/tests.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,20 @@ public function testListWithPaginateLastPage()
357357
$test->expect('{"posts":{"columns":["id","user_id","category_id","content"],"records":[[14,1,1,"#10"]],"results":11}}');
358358
}
359359

360+
public function testListExampleFromReadmeFullRecord()
361+
{
362+
$test = new API($this);
363+
$test->get('/posts?filter=id,eq,1');
364+
$test->expect('{"posts":{"columns":["id","user_id","category_id","content"],"records":[[1,1,1,"blog started"]]}}');
365+
}
366+
367+
public function testListExampleFromReadmeWithExclude()
368+
{
369+
$test = new API($this);
370+
$test->get('/posts?exclude=id&filter=id,eq,1');
371+
$test->expect('{"posts":{"columns":["user_id","category_id","content"],"records":[[1,1,"blog started"]]}}');
372+
}
373+
360374
public function testListExampleFromReadme()
361375
{
362376
$test = new API($this);
@@ -371,6 +385,13 @@ public function testListExampleFromReadmeWithTransform()
371385
$test->expect('{"posts":[{"id":1,"post_tags":[{"id":1,"post_id":1,"tag_id":1,"tags":[{"id":1,"name":"funny"}]},{"id":2,"post_id":1,"tag_id":2,"tags":[{"id":2,"name":"important"}]}],"comments":[{"id":1,"post_id":1,"message":"great"},{"id":2,"post_id":1,"message":"fantastic"}],"user_id":1,"category_id":1,"categories":[{"id":1,"name":"announcement","icon":null}],"content":"blog started"}]}');
372386
}
373387

388+
public function testListExampleFromReadmeWithTransformWithExclude()
389+
{
390+
$test = new API($this);
391+
$test->get('/posts?include=categories,tags,comments&exclude=comments.message&filter=id,eq,1&transform=1');
392+
$test->expect('{"posts":[{"id":1,"post_tags":[{"id":1,"post_id":1,"tag_id":1,"tags":[{"id":1,"name":"funny"}]},{"id":2,"post_id":1,"tag_id":2,"tags":[{"id":2,"name":"important"}]}],"comments":[{"id":1,"post_id":1},{"id":2,"post_id":1}],"user_id":1,"category_id":1,"categories":[{"id":1,"name":"announcement","icon":null}],"content":"blog started"}]}');
393+
}
394+
374395
public function testEditCategoryWithBinaryContent()
375396
{
376397
$binary = base64_encode("\0abc\0\n\r\b\0");
@@ -429,7 +450,7 @@ public function testOptionsRequest()
429450
{
430451
$test = new API($this);
431452
$test->options('/posts/2');
432-
$test->expect('["Access-Control-Allow-Headers: Content-Type","Access-Control-Allow-Methods: OPTIONS, GET, PUT, POST, DELETE, PATCH","Access-Control-Allow-Credentials: true","Access-Control-Max-Age: 1728000"]',false);
453+
$test->expect('["Access-Control-Allow-Headers: Content-Type, X-XSRF-Token","Access-Control-Allow-Methods: OPTIONS, GET, PUT, POST, DELETE, PATCH","Access-Control-Allow-Credentials: true","Access-Control-Max-Age: 1728000"]',false);
433454
}
434455

435456
public function testHidingPasswordColumn()

0 commit comments

Comments
 (0)