Skip to content

Commit 0c34840

Browse files
committed
Prevented asDate from working on decimal numbers. CTR
1 parent 52f36f5 commit 0c34840

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ This release also includes changes from <<release-3-6-8, 3.6.8>>.
3030
* Deprecated public constructor for `SeedStrategy` in favor of builder pattern to be consistent with other strategies.
3131
* Allowed specification of a customized Spark app name.
3232
* Added getter method to `CoinStep` for its probability field.
33+
* Prevented decimal values from being parsed by `asDate()`.
3334
* Prevented specification of `Cardinality` to `option()` when not used in conjunction with `mergeV()`.
3435
* Exposed a mechanism for providers to customize the assertion of error messages in feature tests.
3536
* Attempted to detect JDK version for Gremlin Console to avoid problems with Java 17 if `neo4j-gremlin` is used.

gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/AsDateStep.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.tinkerpop.gremlin.structure.util.StringFactory;
2828
import org.apache.tinkerpop.gremlin.util.DatetimeHelper;
2929

30+
import java.math.BigInteger;
3031
import java.time.format.DateTimeParseException;
3132
import java.util.Collections;
3233
import java.util.Date;
@@ -51,9 +52,17 @@ protected Date map(final Traverser.Admin<S> traverser) {
5152
throw new IllegalArgumentException("Can't parse null as Date.");
5253
if (object instanceof Date)
5354
return (Date) object;
54-
if (object instanceof Number)
55+
if (object instanceof Byte || object instanceof Short || object instanceof Integer || object instanceof Long)
5556
// numbers handled as milliseconds since January 1, 1970, 00:00:00 GMT.
5657
return new Date(((Number) object).longValue());
58+
if (object instanceof BigInteger) {
59+
try {
60+
return new Date(((BigInteger) object).longValueExact());
61+
} catch (ArithmeticException ae) {
62+
throw new IllegalArgumentException("Can't parse " + object + " as Date.");
63+
}
64+
}
65+
5766
if (object instanceof String) {
5867
try {
5968
return DatetimeHelper.parse((String) object);

gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/AsDateStepTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.tinkerpop.gremlin.process.traversal.step.StepTest;
2424
import org.junit.Test;
2525

26+
import java.math.BigInteger;
2627
import java.time.Instant;
2728
import java.time.ZonedDateTime;
2829
import java.util.Arrays;
@@ -48,8 +49,8 @@ public void shouldParseDate() {
4849
final Date testDate = new Date(testInstant.getEpochSecond() * 1000);
4950

5051
assertEquals(new Date(1), __.__(1).asDate().next());
51-
assertEquals(new Date(2), __.__(2.0).asDate().next());
5252
assertEquals(new Date(3), __.__(3L).asDate().next());
53+
assertEquals(new Date(6), __.__(new BigInteger("6")).asDate().next());
5354
assertEquals(testDate, __.__(testDate.getTime()).asDate().next());
5455

5556
assertEquals(testDate, __.__("2023-08-02T00:00:00Z").asDate().next());
@@ -71,6 +72,16 @@ public void shouldThrowExceptionWhenUUIDInput() {
7172
__.__(UUID.randomUUID()).asDate().next();
7273
}
7374

75+
@Test(expected = IllegalArgumentException.class)
76+
public void shouldThrowExceptionWhenDecimalInput() {
77+
__.__(2.2d).asDate().next();
78+
}
79+
80+
@Test(expected = IllegalArgumentException.class)
81+
public void shouldThrowExceptionWhenBigIntegerOutOfLongInput() {
82+
__.__(new BigInteger("1000000000000000000000")).asDate().next();
83+
}
84+
7485
@Test(expected = IllegalArgumentException.class)
7586
public void shouldThrowExceptionWhenNullInput() {
7687
__.__(null).asDate().next();

gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/test/features/map/AsDate.feature

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ Feature: Step - asDate()
6464
g.inject(xx1).asDate()
6565
"""
6666
When iterated to list
67-
Then the result should be unordered
68-
| result |
69-
| dt[2023-09-06T16:28:29Z] |
67+
Then the traversal will raise an error with message containing text of "Can't parse"
7068

7169
@GraphComputerVerificationInjectionNotSupported
7270
Scenario: g_injectX1_2X_asDate

0 commit comments

Comments
 (0)