Skip to content

Commit 7cebc72

Browse files
authored
Accept (primary) key ID using the non-canonical long form (#857)
* Accept EntityKey when using the non-canonical long-form * Fix: deprecated "Implicitly marking parameter as nullable" (PHP 8.4) * Update Tests and add new snapshots
1 parent 0707b34 commit 7cebc72

18 files changed

+313
-28
lines changed

src/EntitySet.php

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -548,35 +548,36 @@ public static function pipe(
548548
);
549549
}
550550

551-
// Test for alternative key syntax
552-
$alternateKey = $lexer->with(function (Lexer $lexer) {
551+
// Test for non-canonical long form with specified key property name
552+
$longFormKey = $lexer->with(function (Lexer $lexer) {
553553
return $lexer->identifier();
554554
});
555555

556-
if ($alternateKey) {
557-
if ($lexer->maybeChar('=')) {
558-
// Test for referenced value syntax
559-
if ($lexer->maybeChar('@')) {
560-
$referencedKey = $lexer->identifier();
561-
$referencedValue = $transaction->getParameterAlias($referencedKey);
562-
$lexer = new Lexer($referencedValue);
563-
}
556+
if ($longFormKey && $lexer->maybeChar('=')) {
557+
// Test for referenced value syntax
558+
if ($lexer->maybeChar('@')) {
559+
$referencedKey = $lexer->identifier();
560+
$referencedValue = $transaction->getParameterAlias($referencedKey);
561+
$lexer = new Lexer($referencedValue);
562+
}
564563

565-
$keyProperty = $entitySet->getType()->getProperty($alternateKey);
564+
$requestedKeyProperty = $entitySet->getType()->getProperty($longFormKey);
566565

567-
if ($keyProperty instanceof DeclaredProperty && !$keyProperty->isAlternativeKey()) {
568-
throw new BadRequestException(
569-
'property_not_alternative_key',
570-
sprintf(
571-
'The requested property (%s) is not configured as an alternative key',
572-
$alternateKey
573-
)
574-
);
575-
}
576-
} else {
577-
// Captured value was not an alternative key, reset the lexer
578-
$lexer = new Lexer($id);
566+
if ($requestedKeyProperty instanceof DeclaredProperty
567+
&& !($requestedKeyProperty->isAlternativeKey() || $keyProperty->getName() === $requestedKeyProperty->getName())) {
568+
throw new BadRequestException(
569+
'property_not_alternative_key',
570+
sprintf(
571+
'The requested property (%s) is not configured as key or alternative key',
572+
$longFormKey
573+
)
574+
);
579575
}
576+
$keyProperty = $requestedKeyProperty;
577+
578+
} else {
579+
// Captured value was not an alternative key, reset the lexer
580+
$lexer = new Lexer($id);
580581
}
581582

582583
if (null === $keyProperty) {
@@ -1261,4 +1262,4 @@ public function toArray(ComplexValue $complexValue): array
12611262

12621263
return $result;
12631264
}
1264-
}
1265+
}

tests/Drivers/WithCSVDriver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ protected function setUpDriver(): void
1919
{
2020
$this->entityId = 0;
2121
$this->missingEntityId = 99;
22+
$this->entitySetKey = 'offset';
2223

2324
/** @var FilesystemAdapter $disk */
2425
$disk = Storage::disk('testing');
@@ -46,4 +47,4 @@ protected function setUpDriver(): void
4647
Lodata::add($entitySet);
4748
$this->updateETag();
4849
}
49-
}
50+
}

tests/Drivers/WithFilesystemDriver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ protected function setUpDriver(): void
1717
$this->entitySet = 'disk';
1818
$this->entityId = 'a1.txt';
1919
$this->missingEntityId = 'qq.txt';
20+
$this->entitySetKey = 'path';
2021

2122
/** @var FilesystemAdapter $disk */
2223
$disk = Storage::disk('testing');
@@ -44,4 +45,4 @@ protected function withModifiedPropertySourceName()
4445
$passengerSet->getType()->getProperties()->reKey();
4546
$passengerSet->setPropertySourceName($ageProperty, 'timestamp');
4647
}
47-
}
48+
}

tests/Drivers/WithRedisDriver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ protected function setUpDriver(): void
1616
{
1717
$this->entityId = 'alpha';
1818
$this->missingEntityId = 'missing';
19+
$this->entitySetKey = 'key';
1920

2021
foreach ($this->getSeed() as $key => $record) {
2122
// @phpstan-ignore-next-line
@@ -54,4 +55,4 @@ protected function assertRedisRecord($key): void
5455
// @phpstan-ignore-next-line
5556
$this->assertMatchesObjectSnapshot(unserialize(Redis::get($key)));
5657
}
57-
}
58+
}

tests/Entity/Entity.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ public function test_read_key_as_segment()
3232
);
3333
}
3434

35+
public function test_read_key_as_long_form()
36+
{
37+
$this->assertJsonResponseSnapshot(
38+
(new Request)
39+
->path($this->entitySetPath.'('.$this->entitySetKey.'='.$this->escapedEntityId.')')
40+
);
41+
}
42+
3543
public function test_read_alternative_key()
3644
{
3745
$this->assertJsonResponseSnapshot(

tests/Helpers/UseDatabaseAssertions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,4 @@ protected static function getQueryCount(): int
157157
{
158158
return count(self::getQueriesExecuted());
159159
}
160-
}
160+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"@context": "http://localhost/odata/$metadata#passengers/$entity",
3+
"offset": 0,
4+
"name": "Alpha",
5+
"age": 4,
6+
"dob": "2000-01-01T04:04:04+00:00",
7+
"chips": true,
8+
"dq": "2000-01-01",
9+
"in_role": "P1DT0S",
10+
"open_time": "05:05:05.000000",
11+
"flight_id": 1,
12+
"colour": "Green",
13+
"sock_colours": "Green,Blue",
14+
"emails": [
15+
16+
17+
]
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"@context": "http://localhost/odata/$metadata#Passengers/$entity",
3+
"id": 1,
4+
"flight_id": 1,
5+
"name": "Alpha",
6+
"dob": "2000-01-01T04:04:04+00:00",
7+
"age": 4,
8+
"chips": true,
9+
"dq": "2000-01-01",
10+
"in_role": "P1DT0S",
11+
"open_time": "05:05:05.000000",
12+
"colour": "Green",
13+
"sock_colours": "Green,Blue",
14+
"emails": [
15+
16+
17+
]
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"@context": "http://localhost/odata/$metadata#disk/$entity",
3+
"type": "text/plain",
4+
"path": "a1.txt",
5+
"timestamp": "2020-01-01T01:01:01+00:00",
6+
"size": 5
7+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"@context": "http://localhost/odata/$metadata#passengers/$entity",
3+
"id": "alpha",
4+
"name": "Alpha",
5+
"age": 4,
6+
"dob": "2000-01-01T04:04:04+00:00",
7+
"chips": true,
8+
"dq": "2000-01-01",
9+
"in_role": "P1DT0S",
10+
"open_time": "05:05:05.000000",
11+
"flight_id": 1,
12+
"colour": "Green",
13+
"sock_colours": "Green,Blue",
14+
"emails": [
15+
16+
17+
]
18+
}

0 commit comments

Comments
 (0)