Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Incubator-kie-issues#1345] It shouldn't be possible to add\subtract a DateTime with a number #6040

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.math.MathContext;
import java.time.Duration;
import java.time.LocalDate;
import java.time.Period;
import java.time.chrono.ChronoPeriod;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAmount;
Expand Down Expand Up @@ -90,10 +89,6 @@ private Object add(Object left, Object right, EvaluationContext ctx) {
if (right instanceof TemporalAmount temporalAmount) {
return temporal.plus(temporalAmount);
}
if (right instanceof BigDecimal bigDecimal) {
Period toAdd = Period.ofDays(bigDecimal.intValue());
return temporal.plus(toAdd);
}
} else if (left instanceof TemporalAmount temporalAmount) {
if (right instanceof Temporal temporal) {
return temporal.plus(temporalAmount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ protected void instanceTest(String expression, Object result, FEELEvent.Severity
}

private static Collection<Object[]> data() {
List<Integer> nullObject = new ArrayList<>();
gitgabrio marked this conversation as resolved.
Show resolved Hide resolved
nullObject.add(null);
nullObject.add(null);
nullObject.add(null);
final Object[][] cases = new Object[][] {
//normal:
{"for x in [1, 2, 3] return x+1", Stream.of(1, 2, 3 ).map(x -> BigDecimal.valueOf(x + 1 ) ).collect(Collectors.toList() ), null},
{"for x in @\"2021-01-01\"..@\"2021-01-03\" return x+1", Stream.of("2021-01-02", "2021-01-03", "2021-01-04" ).map(LocalDate::parse).collect(Collectors.toList() ), null},
{"for x in @\"2021-01-01\"..@\"2021-01-03\" return x + @\"P1D\"", Stream.of("2021-01-02", "2021-01-03", "2021-01-04" ).map(LocalDate::parse).collect(Collectors.toList() ), null},
{"for x in @\"2021-01-01\"..@\"2021-01-03\" return x+1", nullObject, FEELEvent.Severity.ERROR},
yesamer marked this conversation as resolved.
Show resolved Hide resolved

//extended:
{"for x in 1..3 return x+1", Stream.of(1, 2, 3).map(x -> BigDecimal.valueOf(x + 1)).collect(Collectors.toList()), null},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ protected void instanceTest(String expression, Object result, FEELEvent.Severity
private static Collection<Object[]> data() {
final Object[][] cases = new Object[][] {
// date/time/duration function invocations
{ "@\"2021-01-01\" + 10", null , FEELEvent.Severity.ERROR},
{ "@\"2021-01-01T10:10:10\" + 10", null , FEELEvent.Severity.ERROR},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bncriju Can you please add here those additional test cases:

  • number + Date
  • number + DateTime
  • number - Date
  • number - DateTime
  • Date - number
  • DateTime - number

They should all return null with an error.

Just for the sake of completeness :)
Thank you

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bncriju Don't forget this please :)

{ "date(\"2016-07-29\")", DateTimeFormatter.ISO_DATE.parse( "2016-07-29", LocalDate::from ) , null},
{ "@\"2016-07-29\"", DateTimeFormatter.ISO_DATE.parse( "2016-07-29", LocalDate::from ) , null},
{ "date(\"-0105-07-29\")", DateTimeFormatter.ISO_DATE.parse( "-0105-07-29", LocalDate::from ) , null}, // 105 BC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<dmn:extensionElements/>
<dmn:variable id="_4608E42A-977F-4786-AD2A-DE62804DECBA" name="forloopdates" typeRef="tDates"/>
<dmn:literalExpression id="_3BC54643-D567-4148-8931-7A5892E5863F">
<dmn:text>for x in @"2021-01-01"..@"2021-01-03" return x+1</dmn:text>
<dmn:text>for x in @"2021-01-01"..@"2021-01-03" return x + duration("P1D")</dmn:text>
</dmn:literalExpression>
</dmn:decision>
<dmndi:DMNDI>
Expand Down
Loading