From a388df1fef7bdd84f506e92218c1120c6a4362cc Mon Sep 17 00:00:00 2001 From: Yvo Niedrich Date: Fri, 12 Jul 2024 15:04:28 +0200 Subject: [PATCH] Accept API requests with lowercase GUID Keys (#834) --- src/PathSegment/Key.php | 2 +- src/Primitive.php | 18 ++++++++++++++++++ src/Type/Guid.php | 13 +++++++++++++ tests/Queries/GuidTest.php | 7 ++++++- .../Queries/GuidTest__test_filter_guid__5.json | 4 ++++ 5 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 tests/__snapshots__/Queries/GuidTest__test_filter_guid__5.json diff --git a/src/PathSegment/Key.php b/src/PathSegment/Key.php index 15b806fcd..f0d795a62 100644 --- a/src/PathSegment/Key.php +++ b/src/PathSegment/Key.php @@ -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(); } diff --git a/src/Primitive.php b/src/Primitive.php index cd5c21014..51cab017a 100644 --- a/src/Primitive.php +++ b/src/Primitive.php @@ -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, diff --git a/src/Type/Guid.php b/src/Type/Guid.php index d1fd6f2a8..1590e55c6 100644 --- a/src/Type/Guid.php +++ b/src/Type/Guid.php @@ -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) { diff --git a/tests/Queries/GuidTest.php b/tests/Queries/GuidTest.php index cda3d4113..da2ab7896 100644 --- a/tests/Queries/GuidTest.php +++ b/tests/Queries/GuidTest.php @@ -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)') + ); } -} \ No newline at end of file +} diff --git a/tests/__snapshots__/Queries/GuidTest__test_filter_guid__5.json b/tests/__snapshots__/Queries/GuidTest__test_filter_guid__5.json new file mode 100644 index 000000000..5f793c7d3 --- /dev/null +++ b/tests/__snapshots__/Queries/GuidTest__test_filter_guid__5.json @@ -0,0 +1,4 @@ +{ + "@context": "http://localhost/odata/$metadata#examples/$entity", + "id": "00000000-0000-0000-0000-0012EA25EEFB" +}