Elffuss BraidHub

kikocis/java-driver public

JAVA-974: Validate accessor parameter types against bound statement.
Changeset955409e5a7ada6ff26fb44dbbd9a3ebaf9eb5e4e5f0db68ca7acf235638ab011
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2016-02-19 14:52 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-02-24 18:31 +01:00
Parents05d50b12e01d

Browse the files at this changeset

4 files changed

modified changelog/README.md +1 −0
@@ -17,6 +17,7 @@
1717 - [improvement] JAVA-851: Improve UUIDs javadoc with regard to user-provided timestamps.
1818 - [improvement] JAVA-979: Update javadoc for RegularStatement toString() and getQueryString() to indicate that consistency level and other parameters are not maintained in the query string.
1919 - [improvement] JAVA-1038: Fetch node info by rpc_address if its broadcast_address is not in system.peers.
20+- [improvement] JAVA-974: Validate accessor parameter types against bound statement.
2021
2122 Merged from 2.0 branch:
2223
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationParser.java +1 −2
@@ -320,8 +320,7 @@
320320 // We need a specialized mapper to convert UDT instances in the hierarchy.
321321 return new NestedUDTParamMapper(paramName, idx, inferredCQLType);
322322 } else {
323- // Use the default mapper but provide the extracted type
324- return new ParamMapper(paramName, idx, inferredCQLType.dataType);
323+ return new ParamMapper(paramName, idx);
325324 }
326325 } else {
327326 throw new IllegalArgumentException(String.format("Cannot map class %s for parameter %s of %s.%s", paramType, paramName, className, methodName));
modified driver-mapping/src/main/java/com/datastax/driver/mapping/MethodMapper.java +15 −16
@@ -177,32 +177,31 @@
177177 // We'll only set one of the other. If paramName is null, then paramIdx is used.
178178 private final String paramName;
179179 private final int paramIdx;
180- private final DataType dataType;
181180
182- public ParamMapper(String paramName, int paramIdx, DataType dataType) {
181+ ParamMapper(String paramName, int paramIdx) {
183182 this.paramName = paramName;
184183 this.paramIdx = paramIdx;
185- this.dataType = dataType;
186- }
187-
188- public ParamMapper(String paramName, int paramIdx) {
189- this(paramName, paramIdx, null);
190184 }
191185
192186 void setValue(BoundStatement boundStatement, Object arg, ProtocolVersion protocolVersion) {
193- ByteBuffer serializedArg = (dataType == null)
194- ? DataType.serializeValue(arg, protocolVersion)
195- : dataType.serialize(arg, protocolVersion);
196- if (paramName == null) {
197- if (arg == null)
187+
188+ if (arg == null) {
189+ if (paramName == null)
198190 boundStatement.setToNull(paramIdx);
199191 else
200- boundStatement.setBytesUnsafe(paramIdx, serializedArg);
201- } else {
202- if (arg == null)
203192 boundStatement.setToNull(paramName);
204- else
193+ } else {
194+ ColumnDefinitions variables = boundStatement.preparedStatement().getVariables();
195+ if (paramName == null) {
196+ DataType dataType = variables.getType(paramIdx);
197+ ByteBuffer serializedArg = dataType.serialize(arg, protocolVersion);
198+ boundStatement.setBytesUnsafe(paramIdx, serializedArg);
199+ } else {
200+ DataType dataType = variables.getType(paramName);
201+ ByteBuffer serializedArg = dataType.serialize(arg, protocolVersion);
205202 boundStatement.setBytesUnsafe(paramName, serializedArg);
203+ }
204+
206205 }
207206 }
208207 }
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperAccessorParamsTest.java +25 −0
@@ -18,9 +18,13 @@
1818 import com.datastax.driver.core.CCMTestsSupport;
1919 import com.datastax.driver.core.ResultSet;
2020 import com.datastax.driver.core.Row;
21+import com.datastax.driver.core.exceptions.InvalidTypeException;
2122 import com.datastax.driver.core.utils.CassandraVersion;
2223 import com.datastax.driver.mapping.annotations.*;
2324 import org.testng.annotations.Test;
25+import org.testng.collections.Lists;
26+
27+import java.util.List;
2428
2529 import static org.assertj.core.api.Assertions.assertThat;
2630
@@ -106,6 +110,19 @@
106110 .createAccessor(UserPhoneAccessor_TooManyParams.class);
107111 }
108112
113+ /**
114+ * Ensures that a wrong parameter type is detected when
115+ * the accessor is called with a wrong data type.
116+ *
117+ * @jira_ticket JAVA-974
118+ */
119+ @Test(groups = "short", expectedExceptions = InvalidTypeException.class)
120+ public void should_fail_if_wrong_parameter_type() {
121+ UserPhoneAccessor_WrongParameterTypes accessor = new MappingManager(session())
122+ .createAccessor(UserPhoneAccessor_WrongParameterTypes.class);
123+ accessor.findUsersBykeys(Lists.newArrayList(1, 2, 3));
124+ }
125+
109126 private void assertPhonesEqual(int key, String home, String work) {
110127 Row row = session().execute("select * from user where key = " + key).one();
111128 assertThat(row.getString("home_phone")).isEqualTo(home);
@@ -245,4 +262,12 @@
245262 this.gender = val;
246263 }
247264 }
265+
266+ @Accessor
267+ public interface UserPhoneAccessor_WrongParameterTypes {
268+ @Query("select * from user where key IN (:keys)")
269+ // WRONG, should be "where key IN :keys"
270+ void findUsersBykeys(@Param("keys") List<Integer> keys);
271+ }
272+
248273 }