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

RoutingDataSourceHealthContributor should heed Maps with null values in keys #27694

Closed
timmalich opened this issue Aug 16, 2021 · 5 comments
Closed
Labels
status: superseded An issue that has been superseded by another

Comments

@timmalich
Copy link

I just upgraded our project from springboot 2.4.1 to 2.5.3. I am super exited, that there is support for AbstractRoutingDataSource now. Thank' and kudos for that!
Due to the update the health check now run's into an NPE which kills the whole application.
It happens because the entryset 'routingDataSource.getResolvedDataSources().entrySet()' contains null as a key.

We create our datasource like this:

@Bean
@Primary
public DataSource dataSource() {
  final RoutingDataSource routingDataSource = new RoutingDataSource();

  final DataSource primaryDataSource = buildDataSource("PrimaryHikariPool", primaryDataSourceProperties());
  final DataSource replicaDataSource = buildDataSource("ReplicaHikariPool", replicaDataSourceProperties());

  final Map<Object, Object> targetDataSources = new HashMap<>();
  // HERE WE PUT IN OUR EVIL null KEY
  targetDataSources.put(null, primaryDataSource);
  targetDataSources.put(RoutingDataSource.Route.REPLICA, replicaDataSource);

  routingDataSource.setTargetDataSources(targetDataSources);
  routingDataSource.setDefaultTargetDataSource(primaryDataSource);

  return routingDataSource;
}

It might be strange or even stupid to put 'null' as a key into a map, but the RoutingDataSourceHealthContributor shouldn't crash because of that anyway.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 16, 2021
@philwebb philwebb added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 16, 2021
@philwebb philwebb added this to the 2.5.x milestone Aug 16, 2021
thegeekyasian pushed a commit to thegeekyasian/spring-boot that referenced this issue 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. 

This closes spring-projects#27694
@thegeekyasian
Copy link
Contributor

thegeekyasian commented Aug 16, 2021

@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?

@philwebb
Copy link
Member

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

@timmalich

This comment has been minimized.

@philwebb

This comment has been minimized.

@philwebb
Copy link
Member

Closing in favor of PR #27698

@philwebb philwebb added status: superseded An issue that has been superseded by another and removed type: bug A general bug labels Aug 17, 2021
@philwebb philwebb removed this from the 2.5.x milestone Aug 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: superseded An issue that has been superseded by another
Projects
None yet
4 participants