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

unwrap config instead of just cast #871

Merged
merged 2 commits into from
Jan 18, 2023

Conversation

craig-day
Copy link
Contributor

@craig-day craig-day commented Jan 5, 2023

My app uses MicroProfile Config generically, and also uses the SmallRye implementation. My config provider returns a class called SmallRyeAppSettings which implements Config. I think this is valid according to the MP spec. However, when I try to inject config, I end up with a class cast exception.

Caused by: java.lang.ClassCastException: class my.app.SmallRyeAppSettings cannot be cast to class io.smallrye.config.SmallRyeConfig (my.app.SmallRyeAppSettings and io.smallrye.config.SmallRyeConfig are in unnamed module of loader 'app')

I traced the error to this specific cast.

Following the docs from both MP and SmallRye, I think we should use unwrap when handling results from ConfigProvider.getConfig() rather than casting. I added an instanceof shortcut check here to maintain the previous code path. Probably not necessary.

Alternatively, should this be using the small rye provider directly? Maybe this?

SmallRyeConfigProviderResolver.instance().getConfig(getContextClassLoader());

I'm not sure if this will create a circular dependency so I went with the more generic unwrap route first.

I tried to write a test, but to register an override for a ConfigProvider, you need to create something ServiceLoader can pick up. I couldn't find a nice way to do something for just a single test class that didn't impact all the other test classes.

@craig-day craig-day changed the title try to unwrap config instead of just a blind cast unwrap config instead of just cast Jan 6, 2023
@radcortez
Copy link
Member

Hi, @craig-day Thanks for the PR.

We have other places in the code where we are casting to SmallRyeConfig. Do you mind also fixing these cases?

@craig-day
Copy link
Contributor Author

@radcortez I've updated all the casts I could find that were not test. Do you think we should unwrap in test too?

@radcortez
Copy link
Member

@radcortez I've updated all the casts I could find that were not test. Do you think we should unwrap in test too?

Yes, please, go ahead. Thank you!

unwrap to SmallRyeConfig in other places too

unwrap over cast to SmallRyeConfig in test
@craig-day
Copy link
Contributor Author

@radcortez I replaced all the casts with unwrap in test too. Also updated to latest main and squashed my commits. Thanks!

@radcortez
Copy link
Member

Can you run the build locally to fix the formatting issues and push again? :) Thanks!

@craig-day
Copy link
Contributor Author

@radcortez updated and pushed :D

@radcortez
Copy link
Member

Great! Thank you!

@radcortez radcortez merged commit ddb4beb into smallrye:main Jan 18, 2023
@craig-day craig-day deleted the unwrap-not-cast branch January 18, 2023 16:33
@radcortez radcortez added this to the 2.13.2 milestone Feb 1, 2023
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.

3 participants