Skip to content

Commit 9501def

Browse files
committed
3126 SF
1 parent 368761e commit 9501def

File tree

6 files changed

+86
-41
lines changed

6 files changed

+86
-41
lines changed

server/libs/core/commons/commons-util/src/main/java/com/bytechef/commons/util/JsonUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
2424
import com.fasterxml.jackson.databind.JsonNode;
2525
import com.fasterxml.jackson.databind.ObjectMapper;
26+
import com.fasterxml.jackson.databind.node.ObjectNode;
2627
import com.fasterxml.jackson.databind.type.TypeFactory;
2728
import com.jayway.jsonpath.Configuration;
2829
import com.jayway.jsonpath.DocumentContext;
@@ -48,6 +49,10 @@ public class JsonUtils {
4849
private static Configuration configuration;
4950
private static ObjectMapper objectMapper;
5051

52+
public static ObjectNode createObjectNode() {
53+
return objectMapper.createObjectNode();
54+
}
55+
5156
public static Object read(InputStream inputStream) {
5257
try {
5358
return objectMapper.readValue(inputStream, Object.class);

server/libs/platform/platform-workflow/platform-workflow-validator/src/main/java/com/bytechef/platform/workflow/validator/ArrayPropertyValidator.java

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
package com.bytechef.platform.workflow.validator;
1818

19+
import com.bytechef.commons.util.CollectionUtils;
1920
import com.bytechef.commons.util.StringUtils;
2021
import com.bytechef.platform.workflow.validator.model.PropertyInfo;
2122
import com.fasterxml.jackson.databind.JsonNode;
22-
import com.fasterxml.jackson.databind.ObjectMapper;
2323
import com.fasterxml.jackson.databind.node.ObjectNode;
2424
import java.util.ArrayList;
2525
import java.util.Iterator;
@@ -65,11 +65,13 @@ static void validateFromPropertyInfo(
6565

6666
if (isWrappedDefinition(nestedProperties)) {
6767
validateWrappedArray(valueJsonNode, nestedProperties, propertyPath, errors, warnings);
68+
6869
return;
6970
}
7071

7172
if (!valueJsonNode.isEmpty()) {
7273
JsonNode firstElement = valueJsonNode.get(0);
74+
7375
if (firstElement.isObject()) {
7476
validateObjectArray(valueJsonNode, nestedProperties, propertyPath, errors, warnings);
7577
} else {
@@ -86,6 +88,7 @@ private static void validateUnionTypeArray(
8688

8789
for (int i = 0; i < arrayJsonNode.size(); i++) {
8890
JsonNode valueJsonNode = arrayJsonNode.get(i);
91+
8992
boolean matchesAnyType = allowedTypes.stream()
9093
.anyMatch(typeInfo -> TypeValidator.isTypeValid(valueJsonNode, typeInfo.type()));
9194

@@ -104,15 +107,15 @@ private static void validateObjectArray(
104107

105108
if (isUnionTypeObjectArray(elementProperties)) {
106109
validateUnionTypeObjectArray(arrayJsonNode, elementProperties, propertyPath, errors, warnings);
110+
107111
return;
108112
}
109113

110114
JsonNode rootParametersJsonNode = createRootParametersJsonNode(arrayJsonNode, propertyPath);
111115

112116
for (int i = 0; i < arrayJsonNode.size(); i++) {
113117
validateObjectArrayElement(
114-
arrayJsonNode.get(i), elementProperties, propertyPath, i,
115-
rootParametersJsonNode, errors, warnings);
118+
arrayJsonNode.get(i), elementProperties, propertyPath, i, rootParametersJsonNode, errors, warnings);
116119
}
117120
}
118121

@@ -129,8 +132,10 @@ private static void validateUnionTypeObjectArray(
129132

130133
if (!elementJsonNode.isObject()) {
131134
String actualType = JsonUtils.getJsonNodeType(elementJsonNode);
135+
132136
StringUtils.appendWithNewline(
133137
ValidationErrorUtils.typeError(elementPath, "object", actualType), errors);
138+
134139
continue;
135140
}
136141

@@ -141,16 +146,16 @@ private static void validateUnionTypeObjectArray(
141146
}
142147

143148
private static void validateObjectArrayElement(
144-
JsonNode elementJsonNode, List<PropertyInfo> elementProperties,
145-
String propertyPath, int index, JsonNode rootParametersJsonNode,
146-
StringBuilder errors, StringBuilder warnings) {
149+
JsonNode elementJsonNode, List<PropertyInfo> elementProperties, String propertyPath, int index,
150+
JsonNode rootParametersJsonNode, StringBuilder errors, StringBuilder warnings) {
147151

148152
String elementPath = propertyPath + "[" + index + "]";
149153

150154
if (!elementJsonNode.isObject()) {
151155
String actualType = JsonUtils.getJsonNodeType(elementJsonNode);
152-
StringUtils.appendWithNewline(
153-
ValidationErrorUtils.typeError(elementPath, "object", actualType), errors);
156+
157+
StringUtils.appendWithNewline(ValidationErrorUtils.typeError(elementPath, "object", actualType), errors);
158+
154159
return;
155160
}
156161

@@ -179,8 +184,9 @@ private static void validateExtraPropertiesInArrayElement(
179184
StringUtils.appendWithNewline(
180185
"Property '" + propertyPath + "[index]." + fieldName + "' is not defined in task definition",
181186
warnings);
182-
} else if (matchingProperty.displayCondition() != null && !matchingProperty.displayCondition()
183-
.isEmpty()) {
187+
} else if (matchingProperty.displayCondition() != null &&
188+
!org.apache.commons.lang3.StringUtils.isEmpty(matchingProperty.displayCondition())) {
189+
184190
checkDisplayConditionForExtraProperty(
185191
matchingProperty, index, propertyPath, fieldName, rootParametersJsonNode, warnings);
186192
}
@@ -197,8 +203,7 @@ private static void checkDisplayConditionForExtraProperty(
197203

198204
if (!result.shouldShow()) {
199205
StringUtils.appendWithNewline(
200-
"Property '" + propertyPath + "[" + index + "]." + fieldName +
201-
"' is not defined in task definition",
206+
"Property '" + propertyPath + "[" + index + "]." + fieldName + "' is not defined in task definition",
202207
warnings);
203208
}
204209
}
@@ -220,6 +225,7 @@ private static void validatePropertyInArrayElement(
220225
StringUtils.appendWithNewline("Missing required property: " + fieldPath, errors);
221226
} else if (elementJsonNode.has(fieldName)) {
222227
JsonNode valueJsonNode = elementJsonNode.get(fieldName);
228+
223229
if (!valueJsonNode.isTextual() || !TypeValidator.isDataPillExpression(valueJsonNode.asText())) {
224230
TypeValidator.validateType(valueJsonNode, propertyInfo.type(), fieldPath, errors);
225231
}
@@ -232,6 +238,7 @@ private static boolean matchesAnyUnionType(
232238

233239
for (PropertyInfo unionType : unionTypes) {
234240
List<PropertyInfo> schemaProperties = unionType.nestedProperties();
241+
235242
if (schemaProperties == null || schemaProperties.isEmpty()) {
236243
continue;
237244
}
@@ -250,6 +257,7 @@ private static boolean matchesAnyUnionType(
250257
if (!currentWarnings.isEmpty()) {
251258
warnings.append(currentWarnings);
252259
}
260+
253261
return true;
254262
}
255263
}
@@ -303,16 +311,15 @@ private static boolean isWrappedDefinition(List<PropertyInfo> nestedProperties)
303311

304312
private static boolean isUnionTypeObjectArray(List<PropertyInfo> elementProperties) {
305313
return elementProperties.stream()
306-
.allMatch(prop -> "OBJECT".equalsIgnoreCase(prop.type()) &&
307-
prop.nestedProperties() != null &&
308-
!prop.nestedProperties()
309-
.isEmpty());
314+
.allMatch(prop -> "OBJECT".equalsIgnoreCase(prop.type()) && prop.nestedProperties() != null &&
315+
!CollectionUtils.isEmpty(prop.nestedProperties()));
310316
}
311317

312318
private static JsonNode createRootParametersJsonNode(JsonNode arrayValue, String propertyPath) {
313-
ObjectMapper mapper = new ObjectMapper();
314-
ObjectNode rootNode = mapper.createObjectNode();
319+
ObjectNode rootNode = com.bytechef.commons.util.JsonUtils.createObjectNode();
320+
315321
rootNode.set(propertyPath, arrayValue);
322+
316323
return rootNode;
317324
}
318325

@@ -327,6 +334,7 @@ private static List<PropertyInfo> simplifyDisplayConditionsForUnionType(
327334

328335
if (displayCondition != null && !displayCondition.isEmpty()) {
329336
String simplifiedCondition = simplifyConditionForUnionType(displayCondition, baseArrayName);
337+
330338
simplified.add(new PropertyInfo(
331339
prop.name(), prop.type(), prop.description(), prop.required(),
332340
prop.expressionEnabled(), simplifiedCondition, prop.nestedProperties()));
@@ -357,29 +365,35 @@ private static void addUnionTypeError(
357365
String actualType = JsonUtils.getJsonNodeType(valueJsonNode);
358366

359367
StringBuilder expectedTypes = new StringBuilder();
368+
360369
for (int j = 0; j < allowedTypes.size(); j++) {
361370
if (j > 0) {
362371
expectedTypes.append(" or ");
363372
}
364-
expectedTypes.append(allowedTypes.get(j)
365-
.type()
366-
.toLowerCase());
373+
374+
PropertyInfo propertyInfo = allowedTypes.get(j);
375+
376+
expectedTypes.append(org.apache.commons.lang3.StringUtils.lowerCase(propertyInfo.type()));
367377
}
368378

369379
StringUtils.appendWithNewline(
370380
ValidationErrorUtils.arrayElementError(elementValue, propertyName, expectedTypes.toString(), actualType),
371381
errors);
372382
}
373383

374-
private static void
375-
addUnionTypeObjectError(String elementPath, List<PropertyInfo> unionTypes, StringBuilder errors) {
384+
private static void addUnionTypeObjectError(
385+
String elementPath, List<PropertyInfo> unionTypes, StringBuilder errors) {
386+
376387
StringBuilder typeNames = new StringBuilder();
388+
377389
for (int j = 0; j < unionTypes.size(); j++) {
378390
if (j > 0) {
379391
typeNames.append(", ");
380392
}
381-
typeNames.append(unionTypes.get(j)
382-
.name());
393+
394+
PropertyInfo propertyInfo = unionTypes.get(j);
395+
396+
typeNames.append(propertyInfo.name());
383397
}
384398

385399
StringUtils.appendWithNewline(

server/libs/platform/platform-workflow/platform-workflow-validator/src/main/java/com/bytechef/platform/workflow/validator/DisplayConditionEvaluator.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,26 @@ static DisplayConditionResult evaluateForArrayElement(
6666

6767
try {
6868
boolean shouldShow = WorkflowUtils.extractAndEvaluateCondition(resolvedCondition, rootParametersJsonNode);
69+
6970
return DisplayConditionResult.of(shouldShow, false, null);
7071
} catch (Exception e) {
7172
if (logger.isTraceEnabled()) {
7273
logger.trace(e.getMessage());
7374
}
75+
7476
return DisplayConditionResult.hidden();
7577
}
7678
}
7779

78-
private static DisplayConditionResult handleEvaluationException(Exception e) {
80+
private static DisplayConditionResult handleEvaluationException(Exception exception) {
81+
String message = exception.getMessage();
7982

80-
String message = e.getMessage();
8183
if (message != null && message.startsWith("Invalid logic for display condition:")) {
8284
return DisplayConditionResult.malformed(message);
8385
}
8486

8587
if (logger.isTraceEnabled()) {
86-
logger.trace(e.getMessage());
88+
logger.trace(exception.getMessage());
8789
}
8890

8991
return DisplayConditionResult.hidden();
@@ -97,8 +99,10 @@ private static String replaceIndexPlaceholder(String condition, int index) {
9799
* Result of display condition evaluation.
98100
*/
99101
static class DisplayConditionResult {
102+
100103
private final boolean shouldShow;
101104
private final boolean isMalformed;
105+
@Nullable
102106
private final String malformedMessage;
103107

104108
@SuppressFBWarnings("NP")
@@ -132,6 +136,7 @@ boolean isMalformed() {
132136
return isMalformed;
133137
}
134138

139+
@Nullable
135140
String getMalformedMessage() {
136141
return malformedMessage;
137142
}

0 commit comments

Comments
 (0)