Skip to content

Commit

Permalink
Accept API requests with lowercase GUID Keys (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
yvo-niedrich authored Jul 12, 2024
1 parent d587545 commit a388df1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/PathSegment/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static function pipe(
$keyValue->setProperty($keyProperty);
$keyValue->setValue($keyProperty->getType()->instance($currentSegment));

if ((string) $keyValue->getPrimitiveValue() !== $currentSegment) {
if (!$keyValue->getPrimitive()->matches($currentSegment)) {
throw new PathNotHandledException();
}

Expand Down
18 changes: 18 additions & 0 deletions src/Primitive.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ public function equals(Primitive $value): bool
return $value instanceof $this && $value->get() === $this->get();
}

/**
* Return whether the provided value is a representation of this one
* @param mixed $value
* @return bool
*/
public function matches($value): bool
{
if ($value instanceof self) {
$value = $value->get();
}

if (is_scalar($value) || is_null($value) || $value instanceof \Stringable) {
return (string) $this->get() === (string) $value;
}

return false;
}

public static function pipe(
Transaction $transaction,
string $currentSegment,
Expand Down
13 changes: 13 additions & 0 deletions src/Type/Guid.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ class Guid extends Primitive
/** @var ?string $value */
protected $value;

public function matches($value): bool
{
if ($value instanceof self) {
$value = $value->get();
}

if (is_scalar($value) || is_null($value) || $value instanceof \Stringable) {
return (string) $this->get() === strtoupper((string) $value);
}

return false;
}

public function toUrl(): string
{
if (null === $this->value) {
Expand Down
7 changes: 6 additions & 1 deletion tests/Queries/GuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,10 @@ public function test_filter_guid()
(new Request)
->path('/examples(00000000-0000-0000-0000-0012EA25EEFB)')
);

$this->assertJsonResponseSnapshot(
(new Request)
->path('/examples(00000000-0000-0000-0000-0012ea25eefb)')
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"@context": "http://localhost/odata/$metadata#examples/$entity",
"id": "00000000-0000-0000-0000-0012EA25EEFB"
}

0 comments on commit a388df1

Please sign in to comment.