Browse Source

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.
pull/663/head
Christoph Strobl 9 years ago committed by Mark Paluch
parent
commit
2006804133
  1. 16
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultScriptOperations.java
  2. 10
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/DefaultScriptOperationsTests.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2015 the original author or authors. * Copyright 2014-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -98,7 +98,7 @@ class DefaultScriptOperations implements ScriptOperations {
@Override @Override
public Object doInDB(DB db) throws MongoException, DataAccessException { public Object doInDB(DB db) throws MongoException, DataAccessException {
return db.eval(script.getCode(), convertScriptArgs(args)); return db.eval(script.getCode(), convertScriptArgs(false, args));
} }
}); });
} }
@ -116,7 +116,7 @@ class DefaultScriptOperations implements ScriptOperations {
@Override @Override
public Object doInDB(DB db) throws MongoException, DataAccessException { public Object doInDB(DB db) throws MongoException, DataAccessException {
return db.eval(String.format("%s(%s)", scriptName, convertAndJoinScriptArgs(args))); return db.eval(String.format("%s(%s)", scriptName, convertAndJoinScriptArgs(true, args)));
} }
}); });
} }
@ -155,7 +155,7 @@ class DefaultScriptOperations implements ScriptOperations {
return scriptNames; return scriptNames;
} }
private Object[] convertScriptArgs(Object... args) { private Object[] convertScriptArgs(boolean quote, Object... args) {
if (ObjectUtils.isEmpty(args)) { if (ObjectUtils.isEmpty(args)) {
return args; return args;
@ -164,15 +164,15 @@ class DefaultScriptOperations implements ScriptOperations {
List<Object> convertedValues = new ArrayList<Object>(args.length); List<Object> convertedValues = new ArrayList<Object>(args.length);
for (Object arg : args) { for (Object arg : args) {
convertedValues.add(arg instanceof String ? String.format("'%s'", arg) : this.mongoOperations.getConverter() convertedValues.add(arg instanceof String && quote ? String.format("'%s'", arg)
.convertToMongoType(arg)); : this.mongoOperations.getConverter().convertToMongoType(arg));
} }
return convertedValues.toArray(); return convertedValues.toArray();
} }
private String convertAndJoinScriptArgs(Object... args) { private String convertAndJoinScriptArgs(boolean quote, Object... args) {
return ObjectUtils.isEmpty(args) ? "" : StringUtils.arrayToCommaDelimitedString(convertScriptArgs(args)); return ObjectUtils.isEmpty(args) ? "" : StringUtils.arrayToCommaDelimitedString(convertScriptArgs(quote, args));
} }
/** /**

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2014-2015 the original author or authors. * Copyright 2014-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -190,4 +190,12 @@ public class DefaultScriptOperationsTests {
public void scriptNamesShouldReturnEmptySetWhenNoScriptRegistered() { public void scriptNamesShouldReturnEmptySetWhenNoScriptRegistered() {
assertThat(scriptOps.getScriptNames(), is(empty())); assertThat(scriptOps.getScriptNames(), is(empty()));
} }
/**
* @see DATAMONGO-1465
*/
@Test
public void executeShouldNotQuoteStrings() {
assertThat(scriptOps.execute(EXECUTABLE_SCRIPT, "spring-data"), is((Object) "spring-data"));
}
} }

Loading…
Cancel
Save