Fix CassandraTemplate and CqlTemplate methods violating their contract. [DATACASS-279] #451
Labels
in: core
Issues in core support
status: declined
A suggestion or change that we don't feel we should currently apply
type: bug
A general bug
John Blum opened DATACASS-279 and commented
When going through the SDC Reference Guide, exercising the examples, I stumbled upon an inconsistency in the CassandraTemplate / CqlTemplate API.
For instance, when calling
[cqlTemplate.queryForObject(select, Person.class)](http://docs.spring.io/spring-data/cassandra/docs/1.4.1.RELEASE/api/org/springframework/cassandra/core/CqlTemplate.html#queryForObject-java.lang.String-java.lang.Class-)
, I expected to get back an instance ofPerson
. Instead, I got back aString
based on the type of the first Column (i.e. "id") in the CQL Table (e.g. "People") queried in theSELECT
statement.Of course, as the documentation explains...
The template method tries to return the value of the first Column (i.e. "id") in the Row (e.g. "{'id':'123', 'name':'Jon Doe', 'age':40}") as an instance of the "requiredType" and not the entire Row mapped to instance of the "requiredType" as I initially suspected.
However, the API call did not honor my "required" type, either by mapping or conversion, despite the templates having access to Spring's Converter API.
It is fine the API returns the value from the first Column in the Row returned from the table queried in Cassandra, as advertised, but it is +not+ fine to violate the "required" type, which can clearly lead to a
ClassCastException
in the user application, unexpectedly and without advance warning.If the column value cannot be mapped or converted to an instance of the "required" class type, or the Column in the Cassandra/CQL Table is not of the "required" type, then the API method should throw an
IllegalArgumentException
, or similarDataAccessException
. It should not simply ignore the "required" type and return whatever it chooses.Attempting to map or convert the payload/value or failing fast is consistent with other Spring API methods with similar functionality and intent, for example...
[RestTemplate.getForObject(:URI, responseType:Class)](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html#getForObject-java.net.URI-java.lang.Class-)
Affects: 1.3.4 (Gosling SR4), 1.4.2 (Hopper SR2)
Issue Links:
("depends on")
The text was updated successfully, but these errors were encountered: