Skip to content

Commit 5f199cf

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1465 - Fix String quotation in DefaultScriptOperations.execute().
This change prevents Strings from being quoted prior to sending them as args of a script. Original pull request: #383.
1 parent eb26b78 commit 5f199cf

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultScriptOperations.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -98,7 +98,7 @@ public Object execute(final ExecutableMongoScript script, final Object... args)
9898

9999
@Override
100100
public Object doInDB(DB db) throws MongoException, DataAccessException {
101-
return db.eval(script.getCode(), convertScriptArgs(args));
101+
return db.eval(script.getCode(), convertScriptArgs(false, args));
102102
}
103103
});
104104
}
@@ -116,7 +116,7 @@ public Object call(final String scriptName, final Object... args) {
116116

117117
@Override
118118
public Object doInDB(DB db) throws MongoException, DataAccessException {
119-
return db.eval(String.format("%s(%s)", scriptName, convertAndJoinScriptArgs(args)));
119+
return db.eval(String.format("%s(%s)", scriptName, convertAndJoinScriptArgs(true, args)));
120120
}
121121
});
122122
}
@@ -155,7 +155,7 @@ public Set<String> getScriptNames() {
155155
return scriptNames;
156156
}
157157

158-
private Object[] convertScriptArgs(Object... args) {
158+
private Object[] convertScriptArgs(boolean quote, Object... args) {
159159

160160
if (ObjectUtils.isEmpty(args)) {
161161
return args;
@@ -164,15 +164,15 @@ private Object[] convertScriptArgs(Object... args) {
164164
List<Object> convertedValues = new ArrayList<Object>(args.length);
165165

166166
for (Object arg : args) {
167-
convertedValues.add(arg instanceof String ? String.format("'%s'", arg) : this.mongoOperations.getConverter()
168-
.convertToMongoType(arg));
167+
convertedValues.add(arg instanceof String && quote ? String.format("'%s'", arg)
168+
: this.mongoOperations.getConverter().convertToMongoType(arg));
169169
}
170170

171171
return convertedValues.toArray();
172172
}
173173

174-
private String convertAndJoinScriptArgs(Object... args) {
175-
return ObjectUtils.isEmpty(args) ? "" : StringUtils.arrayToCommaDelimitedString(convertScriptArgs(args));
174+
private String convertAndJoinScriptArgs(boolean quote, Object... args) {
175+
return ObjectUtils.isEmpty(args) ? "" : StringUtils.arrayToCommaDelimitedString(convertScriptArgs(quote, args));
176176
}
177177

178178
/**

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultScriptOperationsTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -190,4 +190,12 @@ public void scriptNamesShouldContainNameOfRegisteredScript() {
190190
public void scriptNamesShouldReturnEmptySetWhenNoScriptRegistered() {
191191
assertThat(scriptOps.getScriptNames(), is(empty()));
192192
}
193+
194+
/**
195+
* @see DATAMONGO-1465
196+
*/
197+
@Test
198+
public void executeShouldNotQuoteStrings() {
199+
assertThat(scriptOps.execute(EXECUTABLE_SCRIPT, "spring-data"), is((Object) "spring-data"));
200+
}
193201
}

0 commit comments

Comments
 (0)