Skip to content

DATAMONGO-2315 - Fix $date parameter binding for string based queries. #772

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2315-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2315-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2315-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2315-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ class DateTimeFormatter {
static {
FormatterImpl dateTimeHelper;
try {
dateTimeHelper = loadDateTimeFormatter("org.bson.json.DateTimeFormatter$Java8DateTimeFormatter");
dateTimeHelper = loadDateTimeFormatter(
"org.springframework.data.mongodb.util.json.DateTimeFormatter$Java8DateTimeFormatter");
} catch (LinkageError e) {
// this is expected if running on a release prior to Java 8: fallback to JAXB.
dateTimeHelper = loadDateTimeFormatter("org.bson.json.DateTimeFormatter$JaxbDateTimeFormatter");
dateTimeHelper = loadDateTimeFormatter(
"org.springframework.data.mongodb.util.json.DateTimeFormatter$JaxbDateTimeFormatter");
}

FORMATTER_IMPL = dateTimeHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
Expand All @@ -35,22 +36,13 @@
import org.bson.BsonWriter;
import org.bson.Document;
import org.bson.Transformer;
import org.bson.codecs.BsonTypeClassMap;
import org.bson.codecs.BsonTypeCodecMap;
import org.bson.codecs.BsonValueCodecProvider;
import org.bson.codecs.Codec;
import org.bson.codecs.CollectibleCodec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.DocumentCodecProvider;
import org.bson.codecs.EncoderContext;
import org.bson.codecs.IdGenerator;
import org.bson.codecs.ObjectIdGenerator;
import org.bson.codecs.ValueCodecProvider;
import org.bson.codecs.*;
import org.bson.codecs.configuration.CodecRegistry;

import org.springframework.data.spel.EvaluationContextProvider;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.lang.Nullable;
import org.springframework.util.NumberUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
Expand Down Expand Up @@ -278,6 +270,17 @@ private Object readValue(final BsonReader reader, final DecoderContext decoderCo
if (bindingReader.currentValue != null) {

Object value = bindingReader.currentValue;

if (ObjectUtils.nullSafeEquals(BsonType.DATE_TIME, bindingReader.getCurrentBsonType())
&& !(value instanceof Date)) {

if (value instanceof Number) {
value = new Date(NumberUtils.convertNumberToTargetClass((Number) value, Long.class));
} else if (value instanceof String) {
value = new Date(DateTimeFormatter.parse((String) value));
}
}

bindingReader.setState(State.TYPE);
bindingReader.currentValue = null;
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ private BindableValue bindableValueFor(JsonToken token) {
matched = true;
String group = matcher.group();
int index = computeParameterIndex(group);
computedValue = computedValue.replace(group, ObjectUtils.nullSafeToString(getBindableValueForIndex(index)));
computedValue = computedValue.replace(group, nullSaveToString(getBindableValueForIndex(index)));
}

if (!matched) {
Expand All @@ -406,7 +406,7 @@ private BindableValue bindableValueFor(JsonToken token) {
String binding = regexMatcher.group();
String expression = binding.substring(3, binding.length() - 1);

computedValue = computedValue.replace(binding, ObjectUtils.nullSafeToString(evaluateExpression(expression)));
computedValue = computedValue.replace(binding, nullSaveToString(evaluateExpression(expression)));
}
}

Expand All @@ -416,6 +416,15 @@ private BindableValue bindableValueFor(JsonToken token) {
return bindableValue;
}

private static String nullSaveToString(Object value) {

if (value instanceof Date) {
return DateTimeFormatter.format(((Date) value).getTime());
}

return ObjectUtils.nullSafeToString(value);
}

private static int computeParameterIndex(String parameter) {
return NumberUtils.parseNumber(parameter.replace("?", ""), Integer.class);
}
Expand Down Expand Up @@ -1244,12 +1253,20 @@ private long visitDateTimeExtendedJson() {
} else {
if (valueToken.getType() == JsonTokenType.INT32 || valueToken.getType() == JsonTokenType.INT64) {
value = valueToken.getValue(Long.class);
} else if (valueToken.getType() == JsonTokenType.STRING) {
String dateTimeString = valueToken.getValue(String.class);
try {
value = DateTimeFormatter.parse(dateTimeString);
} catch (IllegalArgumentException e) {
throw new JsonParseException("Failed to parse string as a date", e);
} else if (valueToken.getType() == JsonTokenType.STRING
|| valueToken.getType() == JsonTokenType.UNQUOTED_STRING) {

Object dt = bindableValueFor(valueToken).getValue();
if (dt instanceof Date) {
value = ((Date) dt).getTime();
} else if (dt instanceof Number) {
value = NumberUtils.convertNumberToTargetClass((Number) dt, Long.class);
} else {
try {
value = DateTimeFormatter.parse(dt.toString());
} catch (IllegalArgumentException e) {
throw new JsonParseException(String.format("Failed to parse string '%s' as a date", dt), e);
}
}
} else {
throw new JsonParseException("JSON reader expected an integer or string but found '%s'.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.bson.Document;
Expand Down Expand Up @@ -158,6 +159,42 @@ public void bindSpEL() {
assertThat(target).isEqualTo(new Document("arg0", 100.01D));
}

@Test // DATAMONGO-2315
public void bindDateAsDate() {

Date date = new Date();
Document target = parse("{ 'end_date' : { $gte : { $date : ?0 } } }", date);

assertThat(target).isEqualTo(Document.parse("{ 'end_date' : { $gte : { $date : " + date.getTime() + " } } } "));
}

@Test // DATAMONGO-2315
public void bindQuotedDateAsDate() {

Date date = new Date();
Document target = parse("{ 'end_date' : { $gte : { $date : '?0' } } }", date);

assertThat(target).isEqualTo(Document.parse("{ 'end_date' : { $gte : { $date : " + date.getTime() + " } } } "));
}

@Test // DATAMONGO-2315
public void bindStringAsDate() {

Date date = new Date();
Document target = parse("{ 'end_date' : { $gte : { $date : ?0 } } }", "2019-07-04T12:19:23.000Z");

assertThat(target).isEqualTo(Document.parse("{ 'end_date' : { $gte : { $date : '2019-07-04T12:19:23.000Z' } } } "));
}

@Test // DATAMONGO-2315
public void bindNumberAsDate() {

Long time = new Date().getTime();
Document target = parse("{ 'end_date' : { $gte : { $date : ?0 } } }", time);

assertThat(target).isEqualTo(Document.parse("{ 'end_date' : { $gte : { $date : " + time + " } } } "));
}

private static Document parse(String json, Object... args) {

ParameterBindingJsonReader reader = new ParameterBindingJsonReader(json, args);
Expand Down