diff --git a/src/UriTemplate.php b/src/UriTemplate.php index 659168fe..248d2272 100644 --- a/src/UriTemplate.php +++ b/src/UriTemplate.php @@ -402,7 +402,9 @@ private function expandExpression(array $foundExpression): string $parts = []; /** @var array{name:string, modifier:string, position:string} $variable */ foreach ($expression['variables'] as $variable) { - $parts[] = $this->expandVariable($variable, $expression['operator'], $joiner, $useQuery); + if (isset($this->variables[$variable['name']])) { + $parts[] = $this->expandVariable($variable, $expression['operator'], $joiner, $useQuery); + } } $nullFilter = static function ($value): bool { diff --git a/tests/UriTemplateTest.php b/tests/UriTemplateTest.php index 7b9c3607..45140f62 100644 --- a/tests/UriTemplateTest.php +++ b/tests/UriTemplateTest.php @@ -224,6 +224,7 @@ public function templateExpansionProvider(): iterable ['{;x,y,empty}', ';x=1024;y=768;empty'], ['{?x,y}', '?x=1024&y=768'], ['{?x,y,empty}', '?x=1024&y=768&empty='], + ['{?x,y,undef}', '?x=1024&y=768'], ['?fixed=yes{&x}', '?fixed=yes&x=1024'], ['{&x,y,empty}', '&x=1024&y=768&empty='], ],