-
Notifications
You must be signed in to change notification settings - Fork 38.4k
Fix: type matching for request-scope generic beans #34537
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
Open
currenjin
wants to merge
4
commits into
spring-projects:main
Choose a base branch
from
currenjin:fix-30043
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
03cd38f
Fix: type matching for request-scope generic beans
currenjin 8c06f4a
Improve: resolvable type extraction logic in isTypeMatch method
currenjin 7df2e55
Refactor: test to use variables for bean names
currenjin ad0ddb7
Improve: test with direct assertions and randomized bean names
currenjin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...ans/src/test/java/org/springframework/beans/factory/support/GenericTypeMatchingTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.springframework.beans.factory.support; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.core.ResolvableType; | ||
|
||
import java.util.UUID; | ||
import java.util.function.Supplier; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link AbstractBeanFactory#isTypeMatch} with scoped proxy beans that use generic types. | ||
*/ | ||
class ScopedProxyGenericTypeMatchTests { | ||
|
||
@Test | ||
void scopedProxyBeanTypeMatching() { | ||
DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); | ||
|
||
String proxyBeanName = "wordBean-" + UUID.randomUUID(); | ||
String targetBeanName = "scopedTarget." + proxyBeanName; | ||
|
||
RootBeanDefinition targetDef = new RootBeanDefinition(SomeGenericSupplier.class); | ||
targetDef.setScope("request"); | ||
factory.registerBeanDefinition(targetBeanName, targetDef); | ||
|
||
RootBeanDefinition proxyDef = new RootBeanDefinition(); | ||
proxyDef.setScope("singleton"); | ||
proxyDef.setTargetType(ResolvableType.forClassWithGenerics(Supplier.class, String.class)); | ||
proxyDef.setAttribute("targetBeanName", targetBeanName); | ||
factory.registerBeanDefinition(proxyBeanName, proxyDef); | ||
|
||
ResolvableType supplierType = ResolvableType.forClassWithGenerics(Supplier.class, String.class); | ||
|
||
assertThat(factory.isTypeMatch(proxyBeanName, supplierType)).isTrue(); | ||
|
||
assertThat(factory.getBeanNamesForType(supplierType)).contains(proxyBeanName); | ||
} | ||
|
||
static class SomeGenericSupplier implements Supplier<String> { | ||
@Override | ||
public String get() { | ||
return "value"; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
considering
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/ObjectUtils.html#defaultIfNull(T,T)
#34587
we should be able to reduce all redundancy by using the internal ternary operator hidden inside
ObjectUtils.defaultIfNull
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but of course this can be done afterward or not at all, as its working an acceptable anyways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the suggestion and for pointing me to the ObjectUtils.defaultIfNull from Apache Commons Lang!
I initially wasn't able to use any defaultIfNull method because I was working with org.springframework.util.ObjectUtils, which doesn't have this method in our current version.
Your approach using nested ternary operators looks more concise and elegant. I agree this would be a great improvement, but I think we should leave it for a separate refactoring PR in the future. This way, we can keep the current PR focused on fixing the original issue with clean, minimal changes.
Thanks again for your approval and the valuable feedback throughout this PR!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I just noticed you've added the getIfNull method to ObjectUtils.java - this is fantastic!
Could you guide me on how I can best incorporate this change into my implementation? Should I update my PR to use this new method with the nested ternary approach you suggested, or would you prefer to handle that separately after merging?
I'm excited to use this more elegant solution if that's the right approach at this stage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest merging it as it is and incorporating the refactoring into the Util PR (#34587) so we don’t add empty code and can deliver a practical example right away.
If it’s merged before, then you can, of course, try it here. I would not suggest mixing the PRs now.
Thanks for your heartwarming feedback—teamwork makes the dream work!