From bba1e4376e93c1d449f271a484860bc32f70ed1d Mon Sep 17 00:00:00 2001 From: Filip Michalski Date: Mon, 30 Sep 2024 14:37:22 +0200 Subject: [PATCH] Extrend MandatoryParameterValidator to treat string expression with only quotes as empty value --- .../engine/api/definition/ParameterValidator.scala | 11 ++++++++++- .../api/definition/ParameterValidatorSpec.scala | 3 ++- .../graph/node-modal/editors/expression/Formatter.ts | 5 +---- .../nussknacker/ui/api/ManagementResourcesSpec.scala | 2 +- .../nussknacker/ui/integration/BaseFlowTest.scala | 2 +- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/components-api/src/main/scala/pl/touk/nussknacker/engine/api/definition/ParameterValidator.scala b/components-api/src/main/scala/pl/touk/nussknacker/engine/api/definition/ParameterValidator.scala index bea444f22c6..534fdb35cd9 100644 --- a/components-api/src/main/scala/pl/touk/nussknacker/engine/api/definition/ParameterValidator.scala +++ b/components-api/src/main/scala/pl/touk/nussknacker/engine/api/definition/ParameterValidator.scala @@ -43,7 +43,8 @@ case object MandatoryParameterValidator extends ParameterValidator { override def isValid(paramName: ParameterName, expression: Expression, value: Option[Any], label: Option[String])( implicit nodeId: NodeId ): Validated[PartSubGraphCompilationError, Unit] = - if (!expression.expression.isBlank) valid(()) else invalid(error(paramName, nodeId.id)) + if (!expression.expression.isBlank && !isEmptyStringOrQuotesOnly(value, expression.expression)) valid(()) + else invalid(error(paramName, nodeId.id)) private def error(paramName: ParameterName, nodeId: String): EmptyMandatoryParameter = EmptyMandatoryParameter( message = "This field is mandatory and can not be empty", @@ -52,6 +53,14 @@ case object MandatoryParameterValidator extends ParameterValidator { nodeId = nodeId ) + private def isEmptyStringOrQuotesOnly(value: Option[Any], expression: String): Boolean = { + val exprValue = expression.trim + value match { + case Some(_: String) => exprValue.isBlank || exprValue.equals("''") || exprValue.equals("""""""") + case _ => false + } + } + } case object NotNullParameterValidator extends ParameterValidator { diff --git a/components-api/src/test/scala/pl/touk/nussknacker/engine/api/definition/ParameterValidatorSpec.scala b/components-api/src/test/scala/pl/touk/nussknacker/engine/api/definition/ParameterValidatorSpec.scala index c1596a506ba..3d740952370 100644 --- a/components-api/src/test/scala/pl/touk/nussknacker/engine/api/definition/ParameterValidatorSpec.scala +++ b/components-api/src/test/scala/pl/touk/nussknacker/engine/api/definition/ParameterValidatorSpec.scala @@ -19,10 +19,11 @@ class ParameterValidatorSpec extends AnyFunSuite with TableDrivenPropertyChecks (" ", Some(null), false), ("\t", Some(null), false), (" \n ", Some(null), false), + ("''", Some(""), false), + ("""""""", Some(""), false), ("null", Some(null), true), ("#input.foo['bar']", None, true), ("true", Some(true), true), - ("''", Some(""), true), ("'foo'", Some("foo"), true), ("1", Some(1), true), ("1 + 1", Some(2), true), diff --git a/designer/client/src/components/graph/node-modal/editors/expression/Formatter.ts b/designer/client/src/components/graph/node-modal/editors/expression/Formatter.ts index 82eee8bfee6..49a0f484e6a 100644 --- a/designer/client/src/components/graph/node-modal/editors/expression/Formatter.ts +++ b/designer/client/src/components/graph/node-modal/editors/expression/Formatter.ts @@ -29,10 +29,7 @@ const valueStartsWithQuotationMark = (value) => startsWith(value, '"') || starts const quotationMark = (value) => (valueStartsWithQuotationMark(value) ? valueQuotationMark(value) : defaultQuotationMark); export const stringSpelFormatter: Formatter = { - encode: (value) => { - if (value === "") return value; - else return quotationMark(value) + value + quotationMark(value); - }, + encode: (value) => quotationMark(value) + value + quotationMark(value), decode: (value) => value.substring(1, value.length - 1), }; diff --git a/designer/server/src/test/scala/pl/touk/nussknacker/ui/api/ManagementResourcesSpec.scala b/designer/server/src/test/scala/pl/touk/nussknacker/ui/api/ManagementResourcesSpec.scala index 988b54345e8..f3022fa7506 100644 --- a/designer/server/src/test/scala/pl/touk/nussknacker/ui/api/ManagementResourcesSpec.scala +++ b/designer/server/src/test/scala/pl/touk/nussknacker/ui/api/ManagementResourcesSpec.scala @@ -377,7 +377,7 @@ class ManagementResourcesSpec "end", "kafka-string", TopicParamName.value -> "'end.topic'".spel, - SinkValueParamName.value -> "''".spel + SinkValueParamName.value -> "'sth'".spel ) val testDataContent = """{"sourceId":"startProcess","record":"ala"} diff --git a/designer/server/src/test/scala/pl/touk/nussknacker/ui/integration/BaseFlowTest.scala b/designer/server/src/test/scala/pl/touk/nussknacker/ui/integration/BaseFlowTest.scala index 523509542c3..41d9f1b1c0d 100644 --- a/designer/server/src/test/scala/pl/touk/nussknacker/ui/integration/BaseFlowTest.scala +++ b/designer/server/src/test/scala/pl/touk/nussknacker/ui/integration/BaseFlowTest.scala @@ -366,7 +366,7 @@ class BaseFlowTest .streaming(processId) .source("source", "csv-source") .enricher("enricher", "out", "complexReturnObjectService") - .emptySink("end", "sendSms", "Value" -> "''".spel) + .emptySink("end", "sendSms", "Value" -> "'welcome'".spel) saveProcess(process)