Skip to content

improve error message when multiple bean candidates are present and all are Secondary #11766

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
wants to merge 3 commits into
base: 4.9.x
Choose a base branch
from

Conversation

edgao
Copy link

@edgao edgao commented Apr 21, 2025

e.g. if you have this code: (kotlin syntax, hopefully the intent is clear)

@Singleton
class SomeOtherBean(val foo: Foo)

interface Foo

@Singleton
@Secondary
class Foo1 : Foo

@Singleton
@Secondary
class Foo2 : Foo

micronaut throws a cryptic java.util.NoSuchElementException, because it blindly calls iterator.next().

This PR just adds a check for empty list, and throws a more informative exception.

(wasn't sure how to update the unit tests to verify this - DefaultBeanContextSpec didn't seem like the right place? I'd appreciate a pointer to the relevant test suite)

@CLAassistant
Copy link

CLAassistant commented Apr 21, 2025

CLA assistant check
All committers have signed the CLA.

@edgao edgao changed the title improve error message when multiple beans are present and all are Secondary improve error message when multiple bean candidates are present and all are Secondary Apr 21, 2025
@graemerocher
Copy link
Contributor

could you add a test please

@edgao
Copy link
Author

edgao commented Apr 22, 2025

wasn't sure where to put tests 😅 is there an existing test suite? (DefaultBeanContextSpec didn't look right, but maybe I'm looking in the wrong place)

@graemerocher
Copy link
Contributor

graemerocher commented Apr 23, 2025

probably can go in test-suite submodule

@edgao
Copy link
Author

edgao commented Apr 24, 2025

done - verified locally that without the runtime diff, the test fails.

I couldn't figure out how to use e.getPossibleCandidates() - AFAICT it returned an empty iterator? So I just asserted on the exception message. Would appreciate a tip if there's a better way to do this, or if I'm instantiating the exception incorrectly.

@edgao
Copy link
Author

edgao commented May 5, 2025

@graemerocher is there anything else I should do here?

@edgao
Copy link
Author

edgao commented May 14, 2025

looks like CI is green! is this PR ready to go?

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.

4 participants