Skip to content

DataSouceBuilder can fail with a NPE when the driver is null #45976

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

Conversation

chanbinme
Copy link
Contributor

This PR fixes a potential NullPointerException in SimpleDataSourceProperties when mapping the driver class name property.

When a SimpleDriverDataSource is created without explicitly setting a driver, calling dataSource.getDriver().getClass() throws NPE because getDriver() returns null.

Added null check to safely handle the case when no driver is configured, returning null instead of throwing NPE.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 16, 2025
@wilkinsona
Copy link
Member

Thanks for the PR, @chanbinme. Could you please add a test that fails due to an NPE without the proposed change? If I've understood the problem correctly, I think that will require deriving a DataSourceBuilder from an existing SimpleDriverDataSource instance.

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label Jun 17, 2025
@chanbinme chanbinme force-pushed the fix-npe-in-simple-datasource-properties branch from 05fe8ea to e52480a Compare June 17, 2025 11:51
@chanbinme chanbinme closed this Jun 17, 2025
@chanbinme chanbinme reopened this Jun 17, 2025
@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jun 17, 2025
- Prevent NullPointerException in SimpleDataSourceProperties by adding null check before calling getDriver().getClass().
- Fixes potential crash when driver is not explicitly configured.
- Add null checks in convertToString method to handle null Class values
- Add test case to verify NPE-free operation when deriving DataSource

Signed-off-by: chanbinme <[email protected]>
@chanbinme chanbinme force-pushed the fix-npe-in-simple-datasource-properties branch from e52480a to f92fc35 Compare June 17, 2025 12:20
@chanbinme
Copy link
Contributor Author

chanbinme commented Jun 17, 2025

@wilkinsona

Thank you for the feedback! You're absolutely right about adding a test to demonstrate the NPE issue.

I'll add a test case following the existing naming convention, something like:
buildWhenDerivedFromSimpleDriverDataSourceWithDriverNotSetSucceeds()

While writing the test, I discovered an additional NPE issue in the convertToString method where null values were being passed, causing NullPointerException when trying to call .getName() on a null Class object. I've addressed this as well by adding proper null checks.

@snicoll snicoll added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Jun 17, 2025
@snicoll snicoll added this to the 3.3.13 milestone Jun 17, 2025
@snicoll snicoll self-assigned this Jun 17, 2025
@snicoll snicoll changed the title Fix NPE in SimpleDataSourceProperties when driver is null DataSouceBuilder can fail with a NPE when the driver is null Jun 17, 2025
snicoll pushed a commit that referenced this pull request Jun 17, 2025
@snicoll snicoll closed this in e914539 Jun 17, 2025
@snicoll
Copy link
Member

snicoll commented Jun 17, 2025

Thanks @chanbinme

@chanbinme
Copy link
Contributor Author

@snicoll
Thank you for the guidance and for merging this PR! It was a great learning experience working on this issue. Looking forward to contributing more to Spring Boot in the future. 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants