Skip to content

Avoid reference bean name collision with unrelated by-name lookups - #16405

Open
xiincs wants to merge 1 commit into
apache:3.3from
xiincs:fix-reference-bean-name-collision-12637
Open

Avoid reference bean name collision with unrelated by-name lookups#16405
xiincs wants to merge 1 commit into
apache:3.3from
xiincs:fix-reference-bean-name-collision-12637

Conversation

@xiincs

@xiincs xiincs commented Jul 31, 2026

Copy link
Copy Markdown

What is the purpose of the change?

Fixes #12637.

As I understand the issue, when @DubboReference has no explicit id, the reference bean is registered under the bare field name. That name is only checked against bean definitions that already exist at registration time, so it can later collide with an unrelated bean looked up by the same default name elsewhere in the context (for example an independent @Resource field). When that happens, Spring throws BeanNotOfRequiredTypeException instead of falling back to by-type resolution, because CommonAnnotationBeanPostProcessor#autowireResource only takes that fallback when no bean of that name exists yet.

While looking into this, I noticed a previous attempt (#15889) touched ReferenceBean#getObjectType(), but its own regression test still passed on unpatched master, so I don't think it ended up exercising the reported scenario - hopefully this PR's tests close that gap.

My approach here is an opt-in dubbo.application.qualify-reference-bean-name property that suffixes auto-derived reference bean names so they no longer claim a name an unrelated lookup might use. It defaults to false so existing naming behavior is unchanged for anyone already relying on it - I wasn't confident a default-naming change would be safe to make unconditionally given how many downstream users this project has, but I'm happy to adjust the approach if maintainers see a better way to handle it.

Added ReferenceBeanNameCollisionTest (documents the collision under default behavior) and ReferenceBeanNameCollisionFixedTest (confirms the property resolves it), and ran the existing ReferenceAnnotationBeanPostProcessorTest locally to check for regressions.

This is my first contribution to Dubbo, so please let me know if I've missed any conventions.

Checklist

  • Make sure there is a GitHub_issue field for the change.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test to verify your logic correction. If the new feature or significant change is committed, please remember to add sample in dubbo samples project.
  • Make sure gitHub actions can pass. Why the workflow is failing and how to fix it?

@DubboReference registers its reference bean under the bare field
name when no explicit id is given. That name is claimed at
registration time only, so it can silently collide later with an
unrelated bean looked up by the same default name elsewhere in the
context (e.g. a @resource field), which then fails with a confusing
BeanNotOfRequiredTypeException instead of falling back to by-type
resolution, since Spring only takes that fallback path when no bean
of that name exists yet.

Add an opt-in dubbo.application.qualify-reference-bean-name property
that suffixes auto-derived reference bean names so they can no longer
claim a name an unrelated lookup might use. Defaults to false to keep
existing naming behavior unchanged for applications that already rely
on it.

Fixes apache#12637
@codecov-commenter

codecov-commenter commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 60.86%. Comparing base (3a30432) to head (d492932).

Additional details and impacted files
@@             Coverage Diff              @@
##                3.3   #16405      +/-   ##
============================================
- Coverage     60.87%   60.86%   -0.02%     
+ Complexity    11766    11761       -5     
============================================
  Files          1953     1953              
  Lines         89273    89278       +5     
  Branches      13473    13474       +1     
============================================
- Hits          54346    54340       -6     
- Misses        29333    29343      +10     
- Partials       5594     5595       +1     
Flag Coverage Δ
integration-tests-java21 32.14% <60.00%> (-0.01%) ⬇️
integration-tests-java8 32.21% <60.00%> (+<0.01%) ⬆️
samples-tests-java21 32.13% <60.00%> (-0.07%) ⬇️
samples-tests-java8 29.74% <60.00%> (-0.06%) ⬇️
unit-tests-java11 59.10% <100.00%> (-0.02%) ⬇️
unit-tests-java17 58.62% <100.00%> (-0.02%) ⬇️
unit-tests-java21 58.60% <100.00%> (-0.01%) ⬇️
unit-tests-java25 58.59% <100.00%> (+0.03%) ⬆️
unit-tests-java8 59.16% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BeanNotOfRequiredTypeException when using spring bean injection

2 participants