Skip to content

Commit e4b5b88

Browse files
committed
Fix BSON types and query option name
1 parent 73ba412 commit e4b5b88

File tree

9 files changed

+53
-31
lines changed

9 files changed

+53
-31
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,4 @@ jobs:
155155
env:
156156
DOCTRINE_MONGODB_SERVER: ${{ steps.setup-mongodb.outputs.cluster-uri }}
157157
USE_LAZY_GHOST_OBJECTS: ${{ matrix.proxy == 'lazy-ghost' && '1' || '0' }}"
158+
CRYPT_SHARED_LIB_PATH: ${{ steps.setup-mongodb.outputs.crypt-shared-lib-path }}

lib/Doctrine/ODM/MongoDB/Mapping/Annotations/Encrypt.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class Encrypt implements Annotation
2828
/**
2929
* @param EncryptQuery|null $queryType Set the query type for the field, null if not queryable.
3030
* @param int<1, 4>|null $sparsity
31-
* @param positive-int|null $prevision
31+
* @param positive-int|null $precision
3232
* @param positive-int|null $trimFactor
3333
* @param positive-int|null $contention
3434
*/
@@ -37,7 +37,7 @@ public function __construct(
3737
int|float|Int64|Decimal128|UTCDateTime|DateTimeInterface|null $min = null,
3838
int|float|Int64|Decimal128|UTCDateTime|DateTimeInterface|null $max = null,
3939
public ?int $sparsity = null,
40-
public ?int $prevision = null,
40+
public ?int $precision = null,
4141
public ?int $trimFactor = null,
4242
public ?int $contention = null,
4343
) {

lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
* order?: int|string,
108108
* background?: bool,
109109
* enumType?: class-string<BackedEnum>,
110-
* encrypt?: array{queryType?: ?string, min?: mixed, max?: mixed, sparsity?: int<1, 4>, prevision?: int, trimFactor?: int, contention?: int}
110+
* encrypt?: array{queryType?: ?string, min?: mixed, max?: mixed, sparsity?: int<1, 4>, precision?: int, trimFactor?: int, contention?: int}
111111
* }
112112
* @phpstan-type FieldMapping array{
113113
* type: string,
@@ -154,7 +154,7 @@
154154
* alsoLoadFields?: list<string>,
155155
* enumType?: class-string<BackedEnum>,
156156
* storeEmptyArray?: bool,
157-
* encrypt?: array{queryType?: ?string, min?: mixed, max?: mixed, sparsity?: int<1, 4>, prevision?: int, trimFactor?: int, contention?: int},
157+
* encrypt?: array{queryType?: ?string, min?: mixed, max?: mixed, sparsity?: int<1, 4>, precision?: int, trimFactor?: int, contention?: int},
158158
* }
159159
* @phpstan-type AssociationFieldMapping array{
160160
* type?: string,

lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,13 @@ public function loadMetadataForClass($className, \Doctrine\Persistence\Mapping\C
320320
$mapping['encrypt'][$encryptKey] = match ($encryptKey) {
321321
'queryType' => (string) $encryptValue,
322322
'min', 'max' => match ($mapping['type']) {
323-
Type::INT => (int) $encryptValue,
323+
Type::INT, Type::INTEGER => (int) $encryptValue,
324324
Type::FLOAT => (float) $encryptValue,
325325
Type::DECIMAL128 => new Decimal128((string) $encryptValue),
326326
Type::DATE, Type::DATE_IMMUTABLE => new UTCDateTime(new DateTimeImmutable((string) $encryptValue)),
327327
default => null, // Invalid
328328
},
329-
'sparsity', 'prevision', 'trimFactor', 'contention' => (int) $encryptValue,
329+
'sparsity', 'precision', 'trimFactor', 'contention' => (int) $encryptValue,
330330
};
331331
}
332332
}

lib/Doctrine/ODM/MongoDB/Utility/EncryptedFieldsMapGenerator.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
99
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactoryInterface;
1010
use Doctrine\ODM\MongoDB\Mapping\MappingException;
11+
use Doctrine\ODM\MongoDB\Types\Type;
1112
use Generator;
13+
use LogicException;
1214

1315
use function array_filter;
1416
use function assert;
1517
use function iterator_to_array;
18+
use function sprintf;
1619

1720
final class EncryptedFieldsMapGenerator
1821
{
@@ -108,11 +111,15 @@ private function createEncryptedFieldsMapForClass(
108111
$field = [
109112
'path' => $path . $mapping['name'],
110113
'bsonType' => match ($mapping['type']) {
111-
'one' => 'object',
112-
'many' => 'array',
113-
default => $mapping['type'],
114+
ClassMetadata::ONE => 'object',
115+
ClassMetadata::MANY => 'array',
116+
Type::STRING => 'string',
117+
Type::INT, Type::INTEGER => 'int',
118+
Type::FLOAT => 'double',
119+
Type::DECIMAL128 => 'decimal',
120+
Type::DATE, Type::DATE_IMMUTABLE => 'date',
121+
default => throw new LogicException(sprintf('Type "%s" is not supported in encrypted fields map.', $mapping['type'])),
114122
},
115-
// @todo allow setting a keyId in #[Encrypt] attribute
116123
'keyId' => null, // Generate the key automatically
117124
];
118125

phpstan-baseline.neon

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,12 +1668,6 @@ parameters:
16681668
count: 3
16691669
path: tests/Doctrine/ODM/MongoDB/Tests/Functional/QueryableEncryptionTest.php
16701670

1671-
-
1672-
message: '#^Parameter \#1 \$options of method Doctrine\\ODM\\MongoDB\\Configuration\:\:setAutoEncryption\(\) expects array\{keyVaultNamespace\: string, kmsProviders\: array\<string, array\<string, string\>\>, tlsOptions\?\: array\{kmip\: array\{tlsCAFile\: string, tlsCertificateKeyFile\: string\}\}\}, array\{keyVaultNamespace\: non\-falsy\-string, kmsProviders\: array\{local\: array\{key\: MongoDB\\BSON\\Binary\}\}\} given\.$#'
1673-
identifier: argument.type
1674-
count: 1
1675-
path: tests/Doctrine/ODM/MongoDB/Tests/Functional/QueryableEncryptionTest.php
1676-
16771671
-
16781672
message: '#^Parameter \#1 \$value of function count expects array\|Countable, Iterator\<int, string\> given\.$#'
16791673
identifier: argument.type
@@ -2005,43 +1999,43 @@ parameters:
20051999
path: tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest.php
20062000

20072001
-
2008-
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:165\:\:__construct\(\) has parameter \$classNames with no value type specified in iterable type array\.$#'
2002+
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:168\:\:__construct\(\) has parameter \$classNames with no value type specified in iterable type array\.$#'
20092003
identifier: missingType.iterableValue
20102004
count: 1
20112005
path: tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest.php
20122006

20132007
-
2014-
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:165\:\:doLoadMetadata\(\) has parameter \$class with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
2008+
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:168\:\:doLoadMetadata\(\) has parameter \$class with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
20152009
identifier: missingType.generics
20162010
count: 1
20172011
path: tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest.php
20182012

20192013
-
2020-
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:165\:\:doLoadMetadata\(\) has parameter \$parent with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
2014+
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:168\:\:doLoadMetadata\(\) has parameter \$parent with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
20212015
identifier: missingType.generics
20222016
count: 1
20232017
path: tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest.php
20242018

20252019
-
2026-
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:165\:\:getAllMetadata\(\) return type with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata does not specify its types\: T$#'
2020+
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:168\:\:getAllMetadata\(\) return type with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata does not specify its types\: T$#'
20272021
identifier: missingType.generics
20282022
count: 1
20292023
path: tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest.php
20302024

20312025
-
2032-
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:165\:\:initializeReflection\(\) has parameter \$class with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
2026+
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:168\:\:initializeReflection\(\) has parameter \$class with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
20332027
identifier: missingType.generics
20342028
count: 1
20352029
path: tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest.php
20362030

20372031
-
2038-
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:165\:\:isEntity\(\) has parameter \$class with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
2032+
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:168\:\:isEntity\(\) has parameter \$class with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
20392033
identifier: missingType.generics
20402034
count: 1
20412035
path: tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest.php
20422036

20432037
-
2044-
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:165\:\:wakeupReflection\(\) has parameter \$class with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
2038+
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:168\:\:wakeupReflection\(\) has parameter \$class with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
20452039
identifier: missingType.generics
20462040
count: 1
20472041
path: tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest.php

tests/Doctrine/ODM/MongoDB/Tests/Functional/QueryableEncryptionTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use MongoDB\Model\BSONDocument;
1515

1616
use function count;
17+
use function getenv;
1718
use function iterator_to_array;
1819
use function random_bytes;
1920

@@ -99,6 +100,15 @@ protected static function createTestDocumentManager(): DocumentManager
99100
'key' => new Binary(random_bytes(96)),
100101
]);
101102

103+
$autoEncryptionOptions = [];
104+
105+
$cryptSharedLibPath = getenv('CRYPT_SHARED_LIB_PATH');
106+
if ($cryptSharedLibPath) {
107+
$autoEncryptionOptions['extraOptions']['cryptSharedLibPath'] = $cryptSharedLibPath;
108+
}
109+
110+
$config->setAutoEncryption($autoEncryptionOptions);
111+
102112
$client = new Client(self::getUri(), [], $config->getDriverOptions());
103113

104114
return DocumentManager::create($client, $config);

tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,27 @@ public function testVariousRangeTypes(): void
9090
],
9191
[
9292
'path' => 'floatField',
93-
'bsonType' => 'float',
93+
'bsonType' => 'double',
9494
'keyId' => null,
95-
'queries' => ['queryType' => 'range', 'min' => 5.5, 'max' => 10.5],
95+
'queries' => ['queryType' => 'range', 'min' => 5.5, 'max' => 10.5, 'precision' => 1],
9696
],
9797
[
9898
'path' => 'decimalField',
99-
'bsonType' => 'decimal128',
99+
'bsonType' => 'decimal',
100100
'keyId' => null,
101-
'queries' => ['queryType' => 'range', 'min' => new Decimal128('0.1'), 'max' => new Decimal128('0.2')],
101+
'queries' => ['queryType' => 'range', 'min' => new Decimal128('0.1'), 'max' => new Decimal128('0.2'), 'precision' => 2],
102102
],
103103
[
104104
'path' => 'dateField',
105-
'bsonType' => 'date_immutable',
105+
'bsonType' => 'date',
106106
'keyId' => null,
107107
'queries' => [
108108
'queryType' => 'range',
109109
'min' => new UTCDateTime(new DateTimeImmutable('2000-01-01 00:00:00')),
110110
'max' => new UTCDateTime(new DateTimeImmutable('2100-01-01 00:00:00')),
111+
'sparsity' => 1,
112+
'trimFactor' => 3,
113+
'contention' => 4,
111114
],
112115
],
113116
];

tests/Documents/Encryption/RangeTypes.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,21 @@ class RangeTypes
2929
public int $intField;
3030

3131
#[Field(type: Type::FLOAT)]
32-
#[Encrypt(EncryptQuery::Range, min: 5.5, max: 10.5)]
32+
#[Encrypt(EncryptQuery::Range, min: 5.5, max: 10.5, precision: 1)]
3333
public float $floatField;
3434

3535
#[Field(type: Type::DECIMAL128)]
36-
#[Encrypt(EncryptQuery::Range, min: new Decimal128('0.1'), max: new Decimal128('0.2'))]
36+
#[Encrypt(EncryptQuery::Range, min: new Decimal128('0.1'), max: new Decimal128('0.2'), precision: 2)]
3737
public Decimal128 $decimalField;
3838

3939
#[Field(type: Type::DATE_IMMUTABLE)]
40-
#[Encrypt(EncryptQuery::Range, min: new DateTimeImmutable('2000-01-01 00:00:00'), max: new DateTimeImmutable('2100-01-01 00:00:00'))]
40+
#[Encrypt(
41+
queryType: EncryptQuery::Range,
42+
min: new DateTimeImmutable('2000-01-01 00:00:00'),
43+
max: new DateTimeImmutable('2100-01-01 00:00:00'),
44+
sparsity: 1,
45+
trimFactor: 3,
46+
contention: 4,
47+
)]
4148
public DateTimeImmutable $dateField;
4249
}

0 commit comments

Comments
 (0)