Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept API requests with lowercase GUID Keys #834

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
Loading