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

@ConfigProperty not working in ContextResolver #4020

Closed
abelsromero opened this issue Sep 14, 2019 · 9 comments · Fixed by #4024
Closed

@ConfigProperty not working in ContextResolver #4020

abelsromero opened this issue Sep 14, 2019 · 9 comments · Fixed by #4024
Assignees
Labels
area/resteasy-classic kind/bug Something isn't working
Milestone

Comments

@abelsromero
Copy link
Contributor

Describe the bug
Properties marked for configuration injection with @ConfigProperty in a ContextResolver are null unless class is annotated with @ApplicationScoped.

Expected behavior
Property should have the value defined in application.properties without additional configuration.

Actual behavior
Property value is null unless class is annotated with @ApplicationScoped.

To Reproduce
Steps to reproduce the behavior:

  1. Create project and add JsonB configuration as
@Provider
public class JsonConfig implements ContextResolver<Jsonb> {

    @ConfigProperty(name = "json.date-format")
    String dateFormat;;

    @Override
    public Jsonb getContext(Class type) {
		System.out.println(dateFormat);
        final JsonbConfig config = new JsonbConfig()
            .withDateFormat(dateFormat, Locale.getDefault())
        return JsonbBuilder.create(config);
    }

}
  1. Add json.date-format=yyyy-MM-dd'T'HH:mm:ss.SSSX to application properties.
  2. Make a call to a service that returns a Json response.
  3. Debug or check the println to see a null value.
  4. Add @ApplicationScoped to class and repeat to see the value is not null.

Configuration
N/A

Screenshots
N/A

Environment (please complete the following information):
Quarkus version: 0.21.2
Extensions: [agroal, cdi, hibernate-orm, hibernate-validator, jdbc-postgresql, narayana-jta, resteasy, resteasy-jsonb, security, smallrye-jwt]

Maven home: /home/asalgadr/.sdkman/candidates/maven/current
Java version: 1.8.0_222, vendor: Oracle Corporation, runtime: /home/asalgadr/.sdkman/candidates/java/19.2.0-grl/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.1.21-1-manjaro", arch: "amd64", family: "unix"

Additional context
(Add any other context about the problem here.)

@abelsromero abelsromero added the kind/bug Something isn't working label Sep 14, 2019
@gwenneg gwenneg self-assigned this Sep 14, 2019
@gwenneg
Copy link
Member

gwenneg commented Sep 14, 2019

@abelsromero If you want this injection to work before 0.23.0 is released, you can try adding the javax.inject.Inject annotation to the dateFormat field.

@geoand
Copy link
Contributor

geoand commented Sep 14, 2019

@gwenneg any idea why it doesn't work in previous versions?
I would expect this to work since I thought @Provider classes were beans

@gwenneg
Copy link
Member

gwenneg commented Sep 14, 2019

@geoand It looks like there's a @BuildStep ordering issue here. AutoInjectFieldProcessor.annotationTransformer() is being run after ResteasyServerCommonProcessor.annotationTransformer() while I think it should be the opposite. Therefore, when the method that transforms @Provider into @Singleton is run, the @ConfigProperty-annotated fields are not annotated with @Inject yet.

I'm not sure of the best way to force the @BuildStep order though (I know about the general logic, I'm speaking of this specific case).

@gwenneg
Copy link
Member

gwenneg commented Sep 14, 2019

@geoand I've been looking for an existing BuildItem that could be produced by AutoInjectFieldProcessor.annotationTransformer() and then consumed by ResteasyServerCommonProcessor.annotationTransformer() to force the order, but I didn't find anything convincing :)

@geoand
Copy link
Contributor

geoand commented Sep 14, 2019

@gwenneg interesting. I haden't seen ResteasyServerCommonProcessor#annotationTransformer before but I'm wondering why that can't be replaced with a BeanDefiningAnnotation.
Unless I'm missing something, that would achieve the same result and also overcome the problem you describe

@mkouba
Copy link
Contributor

mkouba commented Sep 16, 2019

@geoand I don't remember the details but it was something like that we don't register all providers found on the class path. See #1924 comments for more details.

@gwenneg So your solution is ok for @ConfigProperty but it wouldn't work for other annotations (CDI qualifiers in general). I'll try to modify the code and make use of AutoInjectAnnotationBuildItem.

@gwenneg
Copy link
Member

gwenneg commented Sep 16, 2019

@mkouba That's why I was looking for a better way to fix this than #4024 :) Could you ping me when you submit the PR? I'm interested in the way you'll improve this.

@abelsromero
Copy link
Contributor Author

In case someone comes here looking for the TLDR.
Just updated to 0.23.1 and it works perfectly with only @Provider annotation.

@gwenneg
Copy link
Member

gwenneg commented Sep 26, 2019

Thanks for your feedback @abelsromero!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/resteasy-classic kind/bug Something isn't working
Projects
None yet
5 participants