From b8ddc75741f16f354729e9d7bf23d76f4f8d66b9 Mon Sep 17 00:00:00 2001 From: Norbert Orzechowicz Date: Sun, 27 Sep 2020 11:09:50 +0200 Subject: [PATCH] Fixed fail positive json detection (#210) --- src/Matcher/Pattern/Assert/Json.php | 8 +++++++- tests/Matcher/JsonMatcherTest.php | 6 +++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Matcher/Pattern/Assert/Json.php b/src/Matcher/Pattern/Assert/Json.php index afd6b3ad..652db0df 100644 --- a/src/Matcher/Pattern/Assert/Json.php +++ b/src/Matcher/Pattern/Assert/Json.php @@ -16,7 +16,13 @@ public static function isValid($value) : bool return false; } - if (null === \json_decode($value) && JSON_ERROR_NONE !== \json_last_error()) { + $result = \json_decode($value); + + if (\is_float($result) && \is_infinite($result)) { + return false; + } + + if (null === $result && JSON_ERROR_NONE !== \json_last_error()) { return false; } diff --git a/tests/Matcher/JsonMatcherTest.php b/tests/Matcher/JsonMatcherTest.php index 42ebbc3c..fce3376a 100644 --- a/tests/Matcher/JsonMatcherTest.php +++ b/tests/Matcher/JsonMatcherTest.php @@ -244,7 +244,11 @@ public static function positiveMatches() [ '[{"name": "Norbert"},{"name":"MichaƂ"},{"name":"Bob"},{"name":"Martin"}]', '"@array@.repeat({\"name\": \"@string@\"})"' - ] + ], + [ + '{"something": "5e61188283825"}', + '{"something": "@string@"}', + ], ]; }