Skip to content
2 changes: 1 addition & 1 deletion src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -787,9 +787,9 @@ public function createDocument(string $collection, Document $document): Document
$stmtPermissions->execute();
}
} catch (Throwable $e) {
$this->getPDO()->rollBack();
switch ($e->getCode()) {
case 23505:
$this->getPDO()->rollBack();
throw new Duplicate('Duplicated document: ' . $e->getMessage());
default:
throw $e;
Expand Down
34 changes: 34 additions & 0 deletions tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,40 @@ public function testCreateDocumentDefaults(): void
static::getDatabase()->deleteCollection('defaults');
}

public function testInvalidUtfCharacters(): void
{
// This test is a reminder we can not insert non UTF chars to UTF field
$collection = 'invalid_characters';

static::getDatabase()->createCollection($collection);
static::getDatabase()->createAttribute($collection, 'str', Database::VAR_STRING, 128, true);

// Test insert of null
$str = "\x00"; // Use double quotes!
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createDocument($collection, new Document([
'str' => $str
])));

// Test insert of null
$str = "\u0000"; // Use double quotes!
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createDocument($collection, new Document([
'str' => $str
])));

// Test insert of null Use double quotes
$str = "\000"; // Use double quotes!
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createDocument($collection, new Document([
'str' => $str,
])));

// Suggestion for fix: cleanup non-utf chars
$str = "\xE2\x94"; // Use double quotes!
$str = mb_convert_encoding($str, 'UTF-8', 'UTF-8');
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createDocument($collection, new Document([
'str' => $str
])));
}

/**
* @throws AuthorizationException|LimitException|DuplicateException|StructureException|Exception|Throwable
*/
Expand Down