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

NullPointerException in RoutingDataSourceHealthContributor when a routing data source has a target with a null routing key #27698

Closed
wants to merge 3 commits into from

Conversation

thegeekyasian
Copy link
Contributor

@thegeekyasian thegeekyasian commented Aug 16, 2021

The RoutingDataSourceHealthContributor constructor throws a NullPointerException (NPE) if the provided map has a null key. With this commit the NPE issue has been fixed by filtering the entries that are not-null.

Alternatively, a constant value can be added when the key for a datasource in the ‘Map’ is null.

This closes #27694

The RoutingDataSourceHealthContributor constructor throws a NullPointerException (NPE) if the provided map has a null key. With this commit the NPE issue has been fixed by filtering the entries that are not-null. 

This closes spring-projects#27694
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 16, 2021
Updating the file author as per the commit message guide
@philwebb
Copy link
Member

Thanks for the pull-request @thegeekyasian. I'm wondering if we'd be better off changing e.getKey().toString() to something else so that null entries aren't removed? Perhaps null could be replaced with "unnamed" or something similar.

We should also add some tests to DataSourceHealthContributorAutoConfigurationTests.

Are you interested in updating your pull request in that direction?

@philwebb philwebb added the status: waiting-for-feedback We need additional information before we can continue label Aug 16, 2021
@thegeekyasian
Copy link
Contributor Author

thegeekyasian commented Aug 16, 2021

Thanks for the pull-request @thegeekyasian. I'm wondering if we'd be better off changing e.getKey().toString() to something else so that null entries aren't removed? Perhaps null could be replaced with "unnamed" or something similar.

We should also add some tests to DataSourceHealthContributorAutoConfigurationTests.

Are you interested in updating your pull request in that direction?

Thanks @philwebb Definitely, we can replace the null one with a public constant string value 'unnamed'. I would like to do the suggested changes and update the PR.

Another option that we have is to keep null values with 'null' key with the below code snippet:

.collect(Collectors.toMap((e) -> Optional.of(e.getKey()).map(Object::toString) .orElse(null), Map.Entry::getValue));

Thoughts?

@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 Aug 16, 2021
@philwebb
Copy link
Member

@thegeekyasian I don't think null keys will work because of this assertion.

@thegeekyasian
Copy link
Contributor Author

thegeekyasian commented Aug 16, 2021

@philwebb Ah, I see! 'unnamed' key is the way to go then.

Should a warning be logged in this case though, when setting the datasource for unnamed?

@philwebb
Copy link
Member

From @timmalich in #27694 (comment)

Thanks @thegeekyasian, I've added a comment to the pull-request.

@philwebb, a PR has been created to fix the null pointer by filtering the entries with the non-null only. Can you please review and share your thoughts?

Thank you both very much.

Wouldn't that filter just drop an otherwise valid configuration? Wouldn't it be better to check null within the next line? Like:
e.getKey()==null?null:e.getKey().toString()

Option 2) Would it be possible to at least log a warning?

@philwebb
Copy link
Member

I think the warning is unnecessary since the health info name is only for reference. The only problem might be if Map happens to contain null and "unnamed" which seem quite unlikely.

Instead of throwing an exception, the datasource will be saved as 'unnamed' in the map. In this commit, the 'unnamed' key has been added, along with the required tests
@thegeekyasian
Copy link
Contributor Author

thegeekyasian commented Aug 16, 2021

@philwebb, the PR has been updated with the suggested changes and the tests have been updated to assert the 'unnamed' datasource. With the change done, even if the unnamed key is used in the Map, it would work as expected by the user.

@philwebb philwebb 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 Aug 17, 2021
@philwebb philwebb added this to the 2.4.x milestone Aug 17, 2021
@philwebb philwebb modified the milestones: 2.4.x, 2.5.x Aug 17, 2021
@wilkinsona wilkinsona changed the title Fixing the NullPointerException in RoutingDataSourceHealthContributor NullPointerException in RoutingDataSourceHealthContributor when a routing data source has a target with a null routing key Aug 17, 2021
@wilkinsona wilkinsona self-assigned this Aug 23, 2021
@wilkinsona wilkinsona modified the milestones: 2.5.x, 2.5.5 Aug 23, 2021
@wilkinsona
Copy link
Member

@thegeekyasian Thanks very much for making your first contribution to Spring Boot.

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.

RoutingDataSourceHealthContributor should heed Maps with null values in keys
5 participants