From a619c26aff08dc7343bcc797c49f2dc1997eb436 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Fri, 27 Aug 2021 13:12:16 -0400 Subject: [PATCH] Use database filter to encode default value as JSON string to preseve type --- app/config/collections2.php | 2 +- app/controllers/api/database.php | 15 ++------------- app/init.php | 14 +++++++++++++- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/app/config/collections2.php b/app/config/collections2.php index 8079bdd7c..a01133fdd 100644 --- a/app/config/collections2.php +++ b/app/config/collections2.php @@ -174,7 +174,7 @@ $collections = [ 'required' => false, 'default' => null, 'array' => false, - 'filters' => [], + 'filters' => ['defaultValue'], ], [ '$id' => 'signed', diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index 080ddb954..c01d38fa6 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -55,8 +55,6 @@ function createAttribute($collectionId, $attribute, $response, $dbForInternal, $ $formatOptions = $attribute->getAttribute('formatOptions', []); $filters = $attribute->getAttribute('filters', []); // filters are hidden from the endpoint $default = $attribute->getAttribute('default', null); - // $default = (empty($default)) ? null : (int)$default; - $defaultEncoded = (\is_null($default)) ? '' : json_encode(['value'=>$default]); $collection = $dbForInternal->getDocument('collections', $collectionId); @@ -85,7 +83,7 @@ function createAttribute($collectionId, $attribute, $response, $dbForInternal, $ 'size' => $size, 'required' => $required, 'signed' => $signed, - 'default' => $defaultEncoded, + 'default' => $default, 'array' => $array, 'format' => $format, 'formatOptions' => $formatOptions, @@ -97,9 +95,6 @@ function createAttribute($collectionId, $attribute, $response, $dbForInternal, $ $dbForInternal->purgeDocument('collections', $collectionId); - // Only attributes table needs $default encoded as a string, reset before response - $attribute->setAttribute('default', $default); - // Pass clone of $attribute object to workers // so we can later modify Document to fit response model $clone = clone $attribute; @@ -805,13 +800,7 @@ App::get('/v1/database/collections/:collectionId/attributes') throw new Exception('Collection not found', 404); } - $attributes = $collection->getAttributes(); - - $attributes = array_map(function ($attribute) use ($collection) { - return new Document([\array_merge($attribute, [ - 'collectionId' => $collection->getId(), - ])]); - }, $attributes); + $attributes = $collection->getAttribute('attributes', []); $response->dynamic(new Document([ 'sum' => \count($attributes), diff --git a/app/init.php b/app/init.php index f6b82bba3..8f93c717d 100644 --- a/app/init.php +++ b/app/init.php @@ -145,7 +145,7 @@ if(!empty($user) || !empty($pass)) { } /** - * DB Filters + * Old DB Filters */ DatabaseOld::addFilter('json', function($value) { @@ -181,6 +181,18 @@ DatabaseOld::addFilter('encrypt', } ); +/** + * New DB Filters + */ +Database::addFilter('defaultValue', + function($value) { + return \json_encode(['value' => $value]); + }, + function($value) { + return \json_decode($value, true)['value']; + } +); + Database::addFilter('subQueryAttributes', function($value) { return null;