Skip to content

Commit

Permalink
fix the textarea value judgment error (#931)
Browse files Browse the repository at this point in the history
  fix issue 930

  update value format verification

---------

Co-authored-by: Carpe-Wang <wangcarpe@126.com>
  • Loading branch information
Carpe-Wang and Carpe-Wang authored May 6, 2023
1 parent 34c0cc9 commit bfb1e10
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.googlecode.aviator.Expression;
import com.googlecode.aviator.exception.CompileExpressionErrorException;
import com.googlecode.aviator.exception.ExpressionRuntimeException;
import com.googlecode.aviator.exception.ExpressionSyntaxErrorException;
import org.dromara.hertzbeat.alert.AlerterProperties;
import org.dromara.hertzbeat.alert.AlerterWorkerPool;
import org.dromara.hertzbeat.common.queue.CommonDataQueue;
Expand Down Expand Up @@ -163,10 +164,12 @@ private void calculate(CollectRep.MetricsData metricsData) {
try {
Expression expression = AviatorEvaluator.compile(expr, true);
match = (Boolean) expression.execute(fieldValueMap);
} catch (CompileExpressionErrorException compileException) {
} catch (CompileExpressionErrorException | ExpressionSyntaxErrorException compileException) {
log.error("Alert Define Rule: {} Compile Error: {}.", expr, compileException.getMessage());
} catch (ExpressionRuntimeException expressionRuntimeException) {
log.error("Alert Define Rule: {} Run Error: {}.", expr, expressionRuntimeException.getMessage());
} catch (Exception e) {
log.error("Alert Define Rule: {} Run Error: {}.", e, e.getMessage());
}

if (match != null && match) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

Expand All @@ -39,7 +40,8 @@ public class JsonUtil {

static {
OBJECT_MAPPER
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
}

public static String toJson(Object source) {
Expand Down Expand Up @@ -77,6 +79,10 @@ public static <T> T fromJson(String jsonStr, TypeReference<T> type) {
return null;
}
}

public static <T> T fromJson(String jsonStr) {
return fromJson(jsonStr, new TypeReference<>() {});
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -330,18 +330,17 @@ public void validate(MonitorDto monitorDto, Boolean isModify) throws IllegalArgu
param.setType(CommonConstants.PARAM_TYPE_NUMBER);
break;
case "textarea":
if (StringUtils.hasText(param.getValue())) {
Short textareaLimit = paramDefine.getLimit();
if (textareaLimit != null && param.getValue().length() > textareaLimit) {
throw new IllegalArgumentException("Params field " + field + " type "
+ paramDefine.getType() + " over limit " + param.getValue());
}
break;
case "text":
Short limit = paramDefine.getLimit();
if (limit != null) {
if (param.getValue().length() > limit) {
throw new IllegalArgumentException("Params field " + field + " type "
+ paramDefine.getType() + " over limit " + limit);
}
Short textLimit = paramDefine.getLimit();
if (textLimit != null && param.getValue().length() > textLimit) {
throw new IllegalArgumentException("Params field " + field + " type "
+ paramDefine.getType() + " over limit " + textLimit);
}
break;
case "host":
Expand All @@ -364,9 +363,7 @@ public void validate(MonitorDto monitorDto, Boolean isModify) throws IllegalArgu
case "boolean":
// boolean check
String booleanValue = param.getValue();
try {
Boolean.parseBoolean(booleanValue);
} catch (Exception e) {
if (!"true".equalsIgnoreCase(booleanValue) && !"false".equalsIgnoreCase(booleanValue)) {
throw new IllegalArgumentException("Params field " + field + " value "
+ booleanValue + " is invalid boolean value.");
}
Expand Down Expand Up @@ -405,9 +402,7 @@ public void validate(MonitorDto monitorDto, Boolean isModify) throws IllegalArgu
}
break;
case "key-value":
try {
JsonUtil.toJson(param.getValue());
} catch (Exception e) {
if (JsonUtil.fromJson(param.getValue()) == null) {
throw new IllegalArgumentException("Params field " + field + " value "
+ param.getValue() + " is invalid key-value value");
}
Expand Down

0 comments on commit bfb1e10

Please sign in to comment.