Skip to content

Fix expression defining entire query in annotated repository methods #3907

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 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
8 changes: 4 additions & 4 deletions 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>3.4.0-SNAPSHOT</version>
<version>3.3.0-GH-3871-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data.build</groupId>
<artifactId>spring-data-parent</artifactId>
<version>2.7.0-SNAPSHOT</version>
<version>2.6.0-SNAPSHOT</version>
</parent>

<modules>
Expand All @@ -26,8 +26,8 @@
<properties>
<project.type>multi</project.type>
<dist.id>spring-data-mongodb</dist.id>
<springdata.commons>2.7.0-SNAPSHOT</springdata.commons>
<mongo>4.4.0</mongo>
<springdata.commons>2.6.0-SNAPSHOT</springdata.commons>
<mongo>4.4.0-beta1</mongo>
<mongo.reactivestreams>${mongo}</mongo.reactivestreams>
<jmh.version>1.19</jmh.version>
</properties>
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>3.4.0-SNAPSHOT</version>
<version>3.3.0-GH-3871-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>3.4.0-SNAPSHOT</version>
<version>3.3.0-GH-3871-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
9 changes: 8 additions & 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>3.4.0-SNAPSHOT</version>
<version>3.3.0-GH-3871-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -237,6 +237,13 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>${slf4j}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public ParameterBindingContext(ValueProvider valueProvider, ExpressionParser exp
Supplier<EvaluationContext> evaluationContext) {

this(valueProvider, new SpELExpressionEvaluator() {
@Override
@Override @SuppressWarnings("unchecked")
public <T> T evaluate(String expressionString) {
return (T) expressionParser.parseExpression(expressionString).getValue(evaluationContext.get(), Object.class);
}
Expand Down Expand Up @@ -91,7 +91,7 @@ public static ParameterBindingContext forExpressions(ValueProvider valueProvider
ExpressionParser expressionParser, Function<ExpressionDependencies, EvaluationContext> contextFunction) {

return new ParameterBindingContext(valueProvider, new SpELExpressionEvaluator() {
@Override
@Override @SuppressWarnings("unchecked")
public <T> T evaluate(String expressionString) {

Expression expression = expressionParser.parseExpression(expressionString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
*/
package org.springframework.data.mongodb.util.json;

import static java.util.Arrays.*;
import static org.bson.assertions.Assertions.*;
import static org.bson.codecs.configuration.CodecRegistries.*;
import static java.util.Arrays.asList;
import static org.bson.assertions.Assertions.notNull;
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand All @@ -31,13 +30,24 @@
import org.bson.BsonBinarySubType;
import org.bson.BsonDocument;
import org.bson.BsonDocumentWriter;
import org.bson.BsonInvalidOperationException;
import org.bson.BsonReader;
import org.bson.BsonType;
import org.bson.BsonValue;
import org.bson.BsonWriter;
import org.bson.Document;
import org.bson.Transformer;
import org.bson.codecs.*;
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.configuration.CodecRegistry;
import org.bson.json.JsonParseException;
import org.springframework.data.mapping.model.SpELExpressionEvaluator;
Expand All @@ -61,6 +71,7 @@
* @author Ross Lawley
* @author Ralph Schaer
* @author Christoph Strobl
* @author Rocco Lagrotteria
* @since 2.2
*/
public class ParameterBindingDocumentCodec implements CollectibleCodec<Document> {
Expand Down Expand Up @@ -172,7 +183,7 @@ public Document decode(@Nullable String json, Object[] values) {

public Document decode(@Nullable String json, ParameterBindingContext bindingContext) {

if (StringUtils.isEmpty(json)) {
if (!StringUtils.hasText(json)) {
return new Document();
}

Expand All @@ -193,7 +204,7 @@ public Document decode(@Nullable String json, ParameterBindingContext bindingCon
public ExpressionDependencies captureExpressionDependencies(@Nullable String json, ValueProvider valueProvider,
ExpressionParser expressionParser) {

if (StringUtils.isEmpty(json)) {
if (!StringUtils.hasText(json)) {
return ExpressionDependencies.none();
}

Expand All @@ -217,19 +228,26 @@ public Document decode(final BsonReader reader, final DecoderContext decoderCont
if (bindingReader.currentValue instanceof org.bson.Document) {
return (Document) bindingReader.currentValue;
}
if(ObjectUtils.nullSafeEquals(bindingReader.currentValue, ParameterBindingJsonReader.PLACEHOLDER)) {
return new Document();
}
}

Document document = new Document();
reader.readStartDocument();

try {

reader.readStartDocument();

while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
String fieldName = reader.readName();
Object value = readValue(reader, decoderContext);
document.put(fieldName, value);
}
} catch (JsonParseException e) {

reader.readEndDocument();

} catch (JsonParseException | BsonInvalidOperationException e) {
try {

Object value = readValue(reader, decoderContext);
Expand All @@ -244,8 +262,6 @@ public Document decode(final BsonReader reader, final DecoderContext decoderCont
}
}

reader.readEndDocument();

return document;
}

Expand Down Expand Up @@ -377,8 +393,6 @@ private List<Object> readList(final BsonReader reader, final DecoderContext deco
*/
static class DependencyCapturingExpressionEvaluator implements SpELExpressionEvaluator {

private static final Object PLACEHOLDER = new Object();

private final ExpressionParser expressionParser;
private final List<ExpressionDependencies> dependencies = new ArrayList<>();

Expand All @@ -387,11 +401,11 @@ static class DependencyCapturingExpressionEvaluator implements SpELExpressionEva
}

@Nullable
@Override
@Override @SuppressWarnings("unchecked")
public <T> T evaluate(String expression) {

dependencies.add(ExpressionDependencies.discover(expressionParser.parseExpression(expression)));
return (T) PLACEHOLDER;
return (T) ParameterBindingJsonReader.PLACEHOLDER;
}

ExpressionDependencies getCapturedDependencies() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.springframework.data.mongodb.util.json;

import static java.lang.String.*;
import static java.lang.String.format;

import java.text.DateFormat;
import java.text.ParsePosition;
Expand All @@ -29,7 +29,18 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.bson.*;
import org.bson.AbstractBsonReader;
import org.bson.BsonBinary;
import org.bson.BsonBinarySubType;
import org.bson.BsonContextType;
import org.bson.BsonDbPointer;
import org.bson.BsonInvalidOperationException;
import org.bson.BsonReaderMark;
import org.bson.BsonRegularExpression;
import org.bson.BsonTimestamp;
import org.bson.BsonType;
import org.bson.BsonUndefined;
import org.bson.Document;
import org.bson.internal.Base64;
import org.bson.json.JsonParseException;
import org.bson.types.Decimal128;
Expand Down Expand Up @@ -57,11 +68,14 @@
* @author Florian Buecklers
* @author Brendon Puntin
* @author Christoph Strobl
* @author Rocco Lagrotteria
* @since 2.2
*/
public class ParameterBindingJsonReader extends AbstractBsonReader {

static final Object PLACEHOLDER = new Object();

private static final Pattern PARAMETER_ONLY_BINDING_PATTERN = Pattern.compile("^\\?(\\d+)$");
private static final Pattern ENTIRE_QUERY_BINDING_PATTERN = Pattern.compile("^\\?(\\d+)$|^[\\?:]#\\{.*\\}$");
private static final Pattern PARAMETER_BINDING_PATTERN = Pattern.compile("\\?(\\d+)");
private static final Pattern EXPRESSION_BINDING_PATTERN = Pattern.compile("[\\?:]#\\{.*\\}");

Expand All @@ -70,7 +84,6 @@ public class ParameterBindingJsonReader extends AbstractBsonReader {
private final JsonScanner scanner;
private JsonToken pushedToken;
Object currentValue;
private Mark mark;

/**
* Constructs a new instance with the given JSON string.
Expand Down Expand Up @@ -106,15 +119,8 @@ public ParameterBindingJsonReader(String json, ValueProvider accessor, SpelExpre
public ParameterBindingJsonReader(String json, ValueProvider accessor, SpelExpressionParser spelExpressionParser,
Supplier<EvaluationContext> evaluationContext) {

this.scanner = new JsonScanner(json);
setContext(new Context(null, BsonContextType.TOP_LEVEL));

this.bindingContext = new ParameterBindingContext(accessor, spelExpressionParser, evaluationContext);
this(json, new ParameterBindingContext(accessor, spelExpressionParser, evaluationContext));

Matcher matcher = PARAMETER_ONLY_BINDING_PATTERN.matcher(json);
if (matcher.find()) {
currentValue = bindableValueFor(new JsonToken(JsonTokenType.UNQUOTED_STRING, json)).getValue();
}
}

public ParameterBindingJsonReader(String json, ParameterBindingContext bindingContext) {
Expand All @@ -124,10 +130,20 @@ public ParameterBindingJsonReader(String json, ParameterBindingContext bindingCo

this.bindingContext = bindingContext;

Matcher matcher = PARAMETER_ONLY_BINDING_PATTERN.matcher(json);
Matcher matcher = ENTIRE_QUERY_BINDING_PATTERN.matcher(json);
if (matcher.find()) {
currentValue = bindableValueFor(new JsonToken(JsonTokenType.UNQUOTED_STRING, json)).getValue();
BindableValue bindingResult = bindableValueFor(new JsonToken(JsonTokenType.UNQUOTED_STRING, json));
try {
if (bindingResult.getType().equals(BsonType.DOCUMENT)) {
currentValue = Document.parse(bindingResult.getValue().toString());
}

} catch (JsonParseException jsonParseException) {
throw new IllegalArgumentException(
String.format("Resulting value of expression '%s' is not a valid json query", json), jsonParseException);
}
}

}

// Spring Data Customization END
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2558,7 +2558,6 @@ public T next() {
return iterator.next();
}

@Override
public int available() {
return 1;
}
Expand Down
Loading