Skip to content

Commit 9d67af7

Browse files
committed
[JsonType] Fixed issue #5320
1 parent 0cdb94f commit 9d67af7

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/Codeception/Util/JsonType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected function typeComparison($data, $jsonType)
130130
continue;
131131
}
132132

133-
$regexMatcher = '/:regex\((.*?)\)(?:\|(boolean|integer|double|float|string|array|object|resource|resource \(closed\)|null|unknown type)|$)/';
133+
$regexMatcher = '/:regex\(((.).*?\2)\)/';
134134
$regexes = [];
135135

136136
// Match the string ':regex(' and any characters until a regex delimiter (matches 99.999% use cases) followed by character ')'

tests/unit/Codeception/Util/JsonTypeTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,27 @@ public function testMatchesArrayReturnedByFetchBoth()
216216
'b' => 'integer',
217217
]));
218218
}
219+
220+
public function testRegexFilterWithPrefixedAlternatives()
221+
{
222+
$jsonType = new JsonType(['test' => null]);
223+
$this->assertTrue($jsonType->matches(['test' => 'null|string:regex(~^(\d\d@):\d\d$~)']));
224+
$this->assertNotTrue($jsonType->matches(['test' => 'integer|string:regex(~^(\d\d@):\d\d$~)']));
225+
226+
$jsonType = new JsonType(['test' => 12345]);
227+
$this->assertTrue($jsonType->matches(['test' => 'integer|null|string:regex(~^(\d\d@):\d\d$~)']));
228+
}
229+
230+
public function testRegexFilterWithPostfixedAlternatives()
231+
{
232+
$jsonType = new JsonType(['test' => null]);
233+
// currently produces a false positive
234+
$this->assertNotTrue($jsonType->matches(['test' => 'string:regex(~^(\d\d@):\d\d$~)|integer']));
235+
// currently produces a false negative
236+
$this->assertTrue($jsonType->matches(['test' => 'string:regex(~^(\d\d@):\d\d$~)|null']));
237+
238+
$jsonType = new JsonType(['test' => 12345]);
239+
// currently produces a false negative
240+
$this->assertTrue($jsonType->matches(['test' => 'string:regex(~^(\d\d@):\d\d$~)|integer']));
241+
}
219242
}

0 commit comments

Comments
 (0)