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

JSON-B type adapter can't inject CDI beans #11340

Closed
sdaschner opened this issue Aug 12, 2020 · 6 comments · Fixed by #11350
Closed

JSON-B type adapter can't inject CDI beans #11340

sdaschner opened this issue Aug 12, 2020 · 6 comments · Fixed by #11350
Assignees
Labels
area/arc Issue related to ARC (dependency injection) kind/bug Something isn't working
Milestone

Comments

@sdaschner
Copy link
Contributor

sdaschner commented Aug 12, 2020

When I inject a CDI bean in a JsonbAdapter, the instance is null and will fail with an NPE without further notice. As specified in the JSON-B spec (JSR 367) 4.7.1, this injection should work.

See the following reproducer:

public class SomeTypeAdapter implements JsonbAdapter<SomeType, String> {

    @Inject
    CoffeeShop coffeeShop;

    @Override
    public String adaptToJson(LocalDate date) throws Exception {
        // coffeeShop is null
    }

    @Override
    public LocalDate adaptFromJson(String date) throws Exception {
        // coffeeShop is null
    }
}
// within a class that is mapped via JSON-B

    @JsonbTypeAdapter(SomeTypeAdapter.class)
    private SomeType someType;

Tested with Quarkus version 1.7.0.Final. It seems to be a similar issue like in #6094.

@sdaschner sdaschner added the kind/bug Something isn't working label Aug 12, 2020
@quarkusbot
Copy link

/cc @mkouba, @manovotn

@quarkusbot quarkusbot added the area/arc Issue related to ARC (dependency injection) label Aug 12, 2020
@gsmet
Copy link
Member

gsmet commented Aug 12, 2020

I think it would work if you added a scope annotation to your adapter.

It's indeed the same issue as the one you reported with HV. I'm not sure it's a good idea to add scope annotations automatically everywhere via annotation transformers. @mkouba WDYT?

@mkouba
Copy link
Contributor

mkouba commented Aug 12, 2020

I'm not sure it's a good idea to add scope annotations automatically everywhere via annotation transformers

I'm not sure either. I think that for JAX-RS providers we only add @Singleton if the class contains @Inject or a qualifier annotation.

@gsmet
Copy link
Member

gsmet commented Aug 12, 2020

Yes, that's probably what we would do here too (I think I would also consider the lifecycle annotations).

My question was more: do we want to install this type of mechanism for each potentially injected resources? I don't think we can generalize as the default scope might be different.

In other words: is making what looks like CDI beans CDI beans automatically for all the types that could potentially be CDI beans a good thing or not?

@mkouba
Copy link
Contributor

mkouba commented Aug 12, 2020

In other words:...

I think that could we generalize the tools, i.e. there could be a higher-level API instead of annotation transformer. But even then an extension would have to produce some kind of build item anyway. I don't think that we should do this for all classes automatically.

@mkouba mkouba self-assigned this Aug 12, 2020
@gsmet
Copy link
Member

gsmet commented Aug 12, 2020

Yeah a build item taking a list of classes and a default scope would be nice.

mkouba added a commit to mkouba/quarkus that referenced this issue Aug 12, 2020
...annotation transformers are used to add a scope annotation to a class
- also fixes quarkusio#11340
mkouba added a commit to mkouba/quarkus that referenced this issue Aug 13, 2020
...annotation transformers are used to add a scope annotation to a class
- also fixes quarkusio#11340
mkouba added a commit to mkouba/quarkus that referenced this issue Aug 13, 2020
...annotation transformers are used to add a scope annotation to a class
- also fixes quarkusio#11340
@gsmet gsmet added this to the 1.8.0 - master milestone Aug 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/arc Issue related to ARC (dependency injection) kind/bug Something isn't working
Projects
None yet
4 participants