Skip to content
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

Fix CassandraTemplate and CqlTemplate methods violating their contract. [DATACASS-279] #451

Closed
spring-projects-issues opened this issue Apr 21, 2016 · 4 comments
Assignees
Labels
in: core status: declined type: bug

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Apr 21, 2016

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 of Person. Instead, I got back a String based on the type of the first Column (i.e. "id") in the CQL Table (e.g. "People") queried in the SELECT statement.

Of course, as the documentation explains...

Executes the provided query and tries to return the first column of the first Row as a Class.

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 similar DataAccessException. 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:

  • DATACASS-310 Fix CqlTemplate and CassandraTemplate returning null for data access operations returning a Collection
    ("depends on")
@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Apr 21, 2016

John Blum commented

Note, the proper way to handle this call is by using the [cqlTemplate.queryForObject(select, :RowMapper)](http://docs.spring.io/spring-data/cassandra/docs/1.4.1.RELEASE/api/org/springframework/cassandra/core/CqlTemplate.html#queryForObject-java.lang.String-org.springframework.cassandra.core.RowMapper-) method, passing in a custom [RowMapper](http://docs.spring.io/spring-data/cassandra/docs/1.4.1.RELEASE/api/org/springframework/cassandra/core/RowMapper.html) implementation to map the contents of the Row to the desired/required application domain object/entity class type

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Jul 1, 2016

John Blum commented

Given the description of this bug is but one example of many problems involving both the CqlTemplate and CassandraTemplate, this JIRA will represent an umbrella, epic user story covering more specific problems that will be filed in other JIRAs

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Dec 6, 2016

Mark Paluch commented

Is this ticket still valid given DATACASS-292 is resolved with 2.0.x?

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Jun 7, 2019

Mark Paluch commented

Template API was generally overhauled with 2.0.

@spring-projects-issues spring-projects-issues added type: bug status: declined in: core labels Dec 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core status: declined type: bug
Projects
None yet
Development

No branches or pull requests

2 participants