diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index 3210cf7e3e..292324419f 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -839,6 +839,10 @@ App::post('/v1/database/collections/:collectionId/attributes/enum') $size = ($length > $size) ? $length : $size; } + if (!is_null($default) && !in_array($default, $elements)) { + throw new Exception('Default value not found in elements', 400); + } + $attribute = createAttribute($collectionId, new Document([ 'key' => $key, 'type' => Database::VAR_STRING, diff --git a/app/init.php b/app/init.php index 40df8eaf75..9495bc7b8c 100644 --- a/app/init.php +++ b/app/init.php @@ -311,7 +311,7 @@ Structure::addFormat(APP_DATABASE_ATTRIBUTE_EMAIL, function() { Structure::addFormat(APP_DATABASE_ATTRIBUTE_ENUM, function($attribute) { $elements = $attribute['formatOptions']['elements']; - return new WhiteList($elements); + return new WhiteList($elements, true); }, Database::VAR_STRING); Structure::addFormat(APP_DATABASE_ATTRIBUTE_IP, function() { diff --git a/tests/e2e/Services/Database/DatabaseBase.php b/tests/e2e/Services/Database/DatabaseBase.php index b0fc22b49c..97d70e2521 100644 --- a/tests/e2e/Services/Database/DatabaseBase.php +++ b/tests/e2e/Services/Database/DatabaseBase.php @@ -1419,6 +1419,36 @@ trait DatabaseBase 'array' => true, ]); + $defaultRequired = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/integer', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'attributeId' => 'defaultRequired', + 'required' => true, + 'default' => 12 + ]); + + $enumDefault = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/enum', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'attributeId' => 'enumDefault', + 'elements' => ['north', 'west'], + 'default' => 'south' + ]); + + $enumDefaultStrict = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/enum', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'attributeId' => 'enumDefault', + 'elements' => ['north', 'west'], + 'default' => 'NORTH' + ]); + $this->assertEquals(201, $email['headers']['status-code']); $this->assertEquals(201, $ip['headers']['status-code']); $this->assertEquals(201, $url['headers']['status-code']); @@ -1427,8 +1457,12 @@ trait DatabaseBase $this->assertEquals(201, $probability['headers']['status-code']); $this->assertEquals(201, $upperBound['headers']['status-code']); $this->assertEquals(201, $lowerBound['headers']['status-code']); + $this->assertEquals(201, $enum['headers']['status-code']); $this->assertEquals(400, $invalidRange['headers']['status-code']); $this->assertEquals(400, $defaultArray['headers']['status-code']); + $this->assertEquals(400, $defaultRequired['headers']['status-code']); + $this->assertEquals(400, $enumDefault['headers']['status-code']); + $this->assertEquals(400, $enumDefaultStrict['headers']['status-code']); $this->assertEquals('Minimum value must be lesser than maximum value', $invalidRange['body']['message']); $this->assertEquals('Cannot set default value for array attributes', $defaultArray['body']['message']);