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

JdbcClient.MappedQuerySpec::single can return null #33300

Closed
Harmelodic opened this issue Aug 1, 2024 · 2 comments
Closed

JdbcClient.MappedQuerySpec::single can return null #33300

Harmelodic opened this issue Aug 1, 2024 · 2 comments
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: bug A general bug
Milestone

Comments

@Harmelodic
Copy link

Harmelodic commented Aug 1, 2024

Affects: 3.3.2 / main branch


In the JdbcClient.MappedQuerySpec, the docs and method contract conveys that .single() can never return null. Except it can, but because the docs/method contract says it can't, my IDE complains.

If I have a thingies table that looks like this:

id thingy
1 "Something"
2 null

and then execute the following:

String thingy = jdbcClient.sql("SELECT thingy FROM thingies WHERE id = :id")
        .param("id", "2")
        .query(String.class)
        .single();

assert thingy == null;
// IDE complains: Condition 'thingy == null' is always 'false'
// Actual result is 'true', so the IDE is wrong, but the IDE is basing it's information on the method contract.

Offending docs line: https://github.com/spring-projects/spring-framework/blob/main/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/JdbcClient.java#L399

Not sure how the method contract works it out, as I'm not au fait with the Spring Framework code base.

I don't mind this behaviour being what it is, but I would prefer the method contract/docs to reflect that the method can return null values when the value in the DB is null, so that the IDE doesn't complain.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 1, 2024
@jhoeller jhoeller self-assigned this Aug 1, 2024
@jhoeller jhoeller added in: data Issues in data modules (jdbc, orm, oxm, tx) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 1, 2024
@jhoeller jhoeller added this to the 6.1.12 milestone Aug 1, 2024
@jhoeller
Copy link
Contributor

jhoeller commented Aug 5, 2024

The intention is a guaranteed non-null value from single(). However, due to the unspecified nullness of collections (with no constraints applied by IntelliJ IDEA and NullAway inspections), we missed to enforce this. If you need null as a potential result value there, please call optional() instead of single().

I'm adding an not-null assertion to DataAccessUtils.requiredSingleResult (which JdbcClient is delegating to there) as well as DataAccessUtils.requiredUniqueResult. However, due to current user code potentially accidentally relying on a null value in certain cases, we are only going to enforce this in 6.2 (with RC1 coming up in September, GA in November).

@jhoeller jhoeller modified the milestones: 6.1.12, 6.2.0-M7 Aug 5, 2024
@Harmelodic
Copy link
Author

The intention is a guaranteed non-null value from single()
...
If you need null as a potential result value there, please call optional() instead of single().

Roger that! 👍
Thanks for the attention on the issue. I appreciate your efforts 🤗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants