This is an automated email from the ASF dual-hosted git repository.

gongchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git


The following commit(s) were added to refs/heads/master by this push:
     new 9be7295084 [fix]bugfix:Prevent Long.parseLong() error when search 
param is a float (#3483)
9be7295084 is described below

commit 9be7295084fa5b4bed5f9d96ad920b1eedb1a2c5
Author: starryCoder <[email protected]>
AuthorDate: Sat Sep 20 00:24:51 2025 +0800

    [fix]bugfix:Prevent Long.parseLong() error when search param is a float 
(#3483)
    
    Signed-off-by: Sarthak Arora <[email protected]>
    Signed-off-by: Cyanty <[email protected]>
    Signed-off-by: tomsun28 <[email protected]>
    Signed-off-by: Wenqi Luo <[email protected]>
    Signed-off-by: yuluo-yx <[email protected]>
    Signed-off-by: lynx009 <[email protected]>
    Signed-off-by: Tom <[email protected]>
    Co-authored-by: aias00 <[email protected]>
    Co-authored-by: tomsun28 <[email protected]>
    Co-authored-by: Calvin <[email protected]>
    Co-authored-by: 淞筱 <[email protected]>
    Co-authored-by: shown <[email protected]>
    Co-authored-by: kangli <[email protected]>
    Co-authored-by: Logic <[email protected]>
    Co-authored-by: Sarthak Arora <[email protected]>
    Co-authored-by: Jast <[email protected]>
    Co-authored-by: DeleiGuo <[email protected]>
    Co-authored-by: Cyanty <[email protected]>
    Co-authored-by: Copilot <[email protected]>
    Co-authored-by: Sherlock Yin <[email protected]>
    Co-authored-by: Duansg <[email protected]>
    Co-authored-by: liutianyou <[email protected]>
    Co-authored-by: VampireAchao <[email protected]>
    Co-authored-by: Wenqi Luo <[email protected]>
    Co-authored-by: Yang Chen <[email protected]>
    Co-authored-by: tuzuy <[email protected]>
    Co-authored-by: tuzy <[email protected]>
    Co-authored-by: Kerwin Bryant <[email protected]>
    Co-authored-by: liutianyou <[email protected]>
    Co-authored-by: carl pinto <[email protected]>
    Co-authored-by: TJxiaobao <[email protected]>
    Co-authored-by: cxhello <[email protected]>
---
 .../manager/service/impl/MonitorServiceImpl.java        |  8 ++++----
 .../hertzbeat/manager/service/MonitorServiceTest.java   | 17 ++++++++++++++---
 2 files changed, 18 insertions(+), 7 deletions(-)

diff --git 
a/hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/service/impl/MonitorServiceImpl.java
 
b/hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/service/impl/MonitorServiceImpl.java
index d76b4537ec..26ada75da8 100644
--- 
a/hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/service/impl/MonitorServiceImpl.java
+++ 
b/hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/service/impl/MonitorServiceImpl.java
@@ -19,6 +19,7 @@ package org.apache.hertzbeat.manager.service.impl;
 
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.google.common.collect.Sets;
+import com.google.common.primitives.Longs;
 import com.usthe.sureness.subject.SubjectSum;
 import com.usthe.sureness.util.SurenessContextHolder;
 import jakarta.persistence.criteria.CriteriaBuilder;
@@ -46,7 +47,6 @@ import org.apache.hertzbeat.common.entity.manager.ParamDefine;
 import org.apache.hertzbeat.common.entity.message.CollectRep;
 import org.apache.hertzbeat.common.support.event.MonitorDeletedEvent;
 import org.apache.hertzbeat.common.util.AesUtil;
-import org.apache.hertzbeat.common.util.CommonUtil;
 import org.apache.hertzbeat.common.util.FileUtil;
 import org.apache.hertzbeat.common.util.IntervalExpressionUtil;
 import org.apache.hertzbeat.common.util.IpDomainUtil;
@@ -662,9 +662,9 @@ public class MonitorServiceImpl implements MonitorService {
             if (StringUtils.hasText(search)) {
                 Predicate predicateHost = 
criteriaBuilder.like(root.get("host"), "%" + search + "%");
                 Predicate predicateName = 
criteriaBuilder.like(criteriaBuilder.lower(root.get("name")), "%" + 
search.toLowerCase() + "%");
-                if (CommonUtil.isNumeric(search)){
-                    Predicate predicateId = 
criteriaBuilder.equal(root.get("id"), Long.parseLong(search));
-                    orList.add(predicateId);
+                Long id = Longs.tryParse(search);
+                if (id != null) {
+                    orList.add(criteriaBuilder.equal(root.get("id"), id));
                 }
                 orList.add(predicateHost);
                 orList.add(predicateName);
diff --git 
a/hertzbeat-manager/src/test/java/org/apache/hertzbeat/manager/service/MonitorServiceTest.java
 
b/hertzbeat-manager/src/test/java/org/apache/hertzbeat/manager/service/MonitorServiceTest.java
index 7db847a451..8bad80d915 100644
--- 
a/hertzbeat-manager/src/test/java/org/apache/hertzbeat/manager/service/MonitorServiceTest.java
+++ 
b/hertzbeat-manager/src/test/java/org/apache/hertzbeat/manager/service/MonitorServiceTest.java
@@ -22,7 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.mockito.Mockito.any;
-import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.reset;
 import static org.mockito.Mockito.when;
 import java.util.ArrayList;
@@ -31,6 +31,9 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Optional;
 import java.util.Set;
+import jakarta.persistence.criteria.CriteriaBuilder;
+import jakarta.persistence.criteria.CriteriaQuery;
+import jakarta.persistence.criteria.Root;
 import org.apache.hertzbeat.alert.dao.AlertDefineBindDao;
 import org.apache.hertzbeat.common.constants.CommonConstants;
 import org.apache.hertzbeat.common.entity.job.Job;
@@ -651,8 +654,16 @@ class MonitorServiceTest {
 
     @Test
     void getMonitors() {
-        
doReturn(Page.empty()).when(monitorDao).findAll(any(Specification.class), 
any(PageRequest.class));
-        assertNotNull(monitorService.getMonitors(null, null, null, null, 
"gmtCreate", "desc", 1, 1, null));
+        when(monitorDao.findAll(any(Specification.class), 
any(PageRequest.class))).thenAnswer((invocation)->{
+            Specification<Monitor> spec = invocation.getArgument(0);
+            CriteriaBuilder cb = mock(CriteriaBuilder.class);
+            CriteriaQuery<?> query = mock(CriteriaQuery.class);
+            Root<Monitor> root = mock(Root.class);
+            spec.toPredicate(root, query, cb);
+            return Page.empty();
+        });
+        assertNotNull(monitorService.getMonitors(null, null, "9.111", null, 
"gmtCreate", "desc", 1, 1, null));
+        assertNotNull(monitorService.getMonitors(null, null, "9", null, 
"gmtCreate", "desc", 1, 1, null));
     }
 
     @Test


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to