Skip to content

Nullpointer Exception on Generating Key with SimpleKeyGenerator #31676

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

Closed
fabian-froehlich opened this issue Nov 24, 2023 · 6 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply

Comments

@fabian-froehlich
Copy link

The issue #31412 introduced an regression for us.
The check KotlinDetector.isSuspendingFunction(method) results in a Nullpointer-Exception since we are calling the generate Method in the following way

                    cacheKeyGenerator.generate(
                        null,
                        null,
                        new CodelistKey(entry.getUri(), entry.getVersion(), entry.getCode())
                    )
@sdeleuze sdeleuze self-assigned this Nov 24, 2023
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 24, 2023
@sdeleuze sdeleuze added type: regression A bug that is also a regression in: core Issues in core modules (aop, beans, core, context, expression) and removed type: regression A bug that is also a regression labels Nov 24, 2023
@sdeleuze
Copy link
Contributor

sdeleuze commented Nov 24, 2023

KeyGenerator#generate method parameter is not annotated with @Nullable and all invocations done in Spring Framework are done with a non-null parameter. Before the change you mentioned, the implementation was leniently accept null value, with this change it does not, but still respect the contract.

Would you consider updating your usage to follow the contract?

@snicoll
Copy link
Member

snicoll commented Nov 24, 2023

You should be calling SimpleKeyGenerator.generateKey(Object...arguments) rather than the current method if you can't honor its contract.

@fabian-froehlich
Copy link
Author

Our usecase is an afterStartup script that populates the cache with information that is retrieved from a database. At the time I implemented it in our boot application I found no clean solution that would work with @Cacheable annotated methods to achieve the same.

I think it would be no problem to change our code in that way, that I directly call SimpleKeyGenerator#generateKey.

Today there is an automatic injected KeyGenerator that auto configured SimpleKeyGenerator.

I can't really tell if I found that solution online or made it up by myself. But if it was the former, then others might experience similar issues which could result in a slower adaption rate of the newer spring framework version.

@sdeleuze
Copy link
Contributor

Thanks for your quick feedback.

I am going to decline this change request because the team is not in favor of making the Method parameter @Nullable and I would like to avoid introducing artificial null-checks that may be reported as unneeded based on our null-safety annotations analysis. So please adapt your code accordingly.

@sdeleuze sdeleuze closed this as not planned Won't fix, can't repro, duplicate, stale Nov 24, 2023
@sdeleuze sdeleuze added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 24, 2023
@snicoll
Copy link
Member

snicoll commented Nov 24, 2023

If you are populating the cache, expecting that further calls on the actual annotated method(s) will match, it's even more important to take control over key generation. Relying on what's auto-configured and calling it will null while the actual production code will be invoked with a method and a target class is an important risk that the generated keys would not match.

To be clear, calling this method with null values does not respect the contract of the API.

@fabian-froehlich
Copy link
Author

Thanks for the constructive feedback and conversation. I will review our caching approach.
@snicoll I tried to ensure the correct key mapping with tests but that already felt fuzzy at that time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

4 participants