From 8ffeac436157a29c9a98467b15cac34c8450f91d Mon Sep 17 00:00:00 2001 From: Jason Froehlich Date: Wed, 4 Dec 2024 16:30:09 -0500 Subject: [PATCH 1/2] Implemented Rules for #142 SimpleEvaluationcontext Signed-off-by: Jason Froehlich --- ...g-framework-5.x-to-6.0-core-container.yaml | 26 ++++++++++++++++++ .../konveyor/spel/DynamicQueryService.java | 23 ++++++++++++++++ .../konveyor/spel/SecondaryQueryService.java | 27 +++++++++++++++++++ ...mework-5.x-to-6.0-core-container.test.yaml | 7 +++++ 4 files changed, 83 insertions(+) create mode 100644 default/generated/spring-framework/tests/data/core-container/src/main/java/org/konveyor/spel/DynamicQueryService.java create mode 100644 default/generated/spring-framework/tests/data/core-container/src/main/java/org/konveyor/spel/SecondaryQueryService.java diff --git a/default/generated/spring-framework/spring-framework-5.x-to-6.0-core-container.yaml b/default/generated/spring-framework/spring-framework-5.x-to-6.0-core-container.yaml index de4cea3..884a7af 100644 --- a/default/generated/spring-framework/spring-framework-5.x-to-6.0-core-container.yaml +++ b/default/generated/spring-framework/spring-framework-5.x-to-6.0-core-container.yaml @@ -41,3 +41,29 @@ - title: 'Spring 6.0 migration guide' url: https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-6.0-Release-Notes#core-container +- ruleID: spring-framework-5.x-to-6.0-core-container-00020 + category: potential + effort: 1 + labels: + - konveyor.io/source=spring5 + - konveyor.io/target=spring6+ + when: + or: + - java.referenced: + location: FIELD + pattern: org.springframework.expression.spel.support.SimpleEvaluationContext + - java.referenced: + location: CONSTRUCTOR_CALL + pattern: org.springframework.expression.spel.support.SimpleEvaluationContext + description: Replace SimpleEvaluationContext with StandardEvaluationContext when working with array operations. + message: | + `SimpleEvaluationContext` now disallows creating arrays inside expressions which aligns with how regular constructor resolution works. + + Code that relies on creating arrays within expressions using `SimpleEvaluationContext` might malfunction or produce unexpected results. + + It is recommended to switch to `org.springframework.expression.spel.standard.StandardEvaluationContext` as it offers the full SpEL functionality, + including array creation within expressions as well as improved performance. + links: + - title: 'Spring 6.0 migration guide' + url: https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-6.0-Release-Notes#core-container + diff --git a/default/generated/spring-framework/tests/data/core-container/src/main/java/org/konveyor/spel/DynamicQueryService.java b/default/generated/spring-framework/tests/data/core-container/src/main/java/org/konveyor/spel/DynamicQueryService.java new file mode 100644 index 0000000..835f179 --- /dev/null +++ b/default/generated/spring-framework/tests/data/core-container/src/main/java/org/konveyor/spel/DynamicQueryService.java @@ -0,0 +1,23 @@ +package java.org.konveyor.spel; + +import org.springframework.expression.Expression; +import org.springframework.expression.ExpressionParser; +import org.springframework.expression.spel.standard.SpelExpressionParser; +import org.springframework.expression.spel.support.SimpleEvaluationContext; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class DynamicQueryService { + + public String generateSql(List ids) { + ExpressionParser parser = new SpelExpressionParser(); + SimpleEvaluationContext context = new SimpleEvaluationContext(); + + context.setRootObject(ids); + Expression expression = parser.parseExpression("'IN (' + #root.join(\',\') + ')'"); + String inClause = (String) expression.getValue(context); + return "SELECT * FROM users WHERE user_id " + inClause; + } +} diff --git a/default/generated/spring-framework/tests/data/core-container/src/main/java/org/konveyor/spel/SecondaryQueryService.java b/default/generated/spring-framework/tests/data/core-container/src/main/java/org/konveyor/spel/SecondaryQueryService.java new file mode 100644 index 0000000..9e300f8 --- /dev/null +++ b/default/generated/spring-framework/tests/data/core-container/src/main/java/org/konveyor/spel/SecondaryQueryService.java @@ -0,0 +1,27 @@ +package java.org.konveyor.spel; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.expression.Expression; +import org.springframework.expression.ExpressionParser; +import org.springframework.expression.spel.standard.SpelExpressionParser; +import org.springframework.expression.spel.support.*; +import org.springframework.stereotype.Service; + +import java.util.List; + + +@Service +public class SecondaryQueryService { + + @Autowired + private SimpleEvaluationContext evaluationContext; + + public String generateSql(List ids) { + ExpressionParser parser = new SpelExpressionParser(); + + evaluationContext.setRootObject(ids); + Expression expression = parser.parseExpression("'IN (' + #root.join(\',\') + ')'"); + String inClause = (String) expression.getValue(evaluationContext); + return "SELECT * FROM users WHERE user_id " + inClause; + } +} diff --git a/default/generated/spring-framework/tests/spring-framework-5.x-to-6.0-core-container.test.yaml b/default/generated/spring-framework/tests/spring-framework-5.x-to-6.0-core-container.test.yaml index 54ad96e..29d3f22 100644 --- a/default/generated/spring-framework/tests/spring-framework-5.x-to-6.0-core-container.test.yaml +++ b/default/generated/spring-framework/tests/spring-framework-5.x-to-6.0-core-container.test.yaml @@ -17,3 +17,10 @@ tests: mode: "source-only" hasIncidents: exactly: 2 +- ruleID: spring-framework-5.x-to-6.0-core-container-00020 + testCases: + - name: tc-2 + analysisParams: + mode: "source-only" + hasIncidents: + exactly: 2 From 47c9d44ed28def303d212303357889e2bb1b7496 Mon Sep 17 00:00:00 2001 From: Jason Froehlich Date: Wed, 4 Dec 2024 16:50:10 -0500 Subject: [PATCH 2/2] Fixed yaml errors --- .../spring-framework-5.x-to-6.0-core-container.test.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/default/generated/spring-framework/tests/spring-framework-5.x-to-6.0-core-container.test.yaml b/default/generated/spring-framework/tests/spring-framework-5.x-to-6.0-core-container.test.yaml index 29d3f22..6c82032 100644 --- a/default/generated/spring-framework/tests/spring-framework-5.x-to-6.0-core-container.test.yaml +++ b/default/generated/spring-framework/tests/spring-framework-5.x-to-6.0-core-container.test.yaml @@ -19,8 +19,8 @@ tests: exactly: 2 - ruleID: spring-framework-5.x-to-6.0-core-container-00020 testCases: - - name: tc-2 + - name: tc-2 analysisParams: mode: "source-only" - hasIncidents: - exactly: 2 + hasIncidents: + exactly: 2