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

Jakarta - EE 10 - @Inject should work instead of the deprecated @Context in RESTEasy Classic #29240

Closed
agoncal opened this issue Nov 13, 2022 · 7 comments · Fixed by #29315
Closed
Assignees
Labels
area/jakarta kind/bug Something isn't working
Milestone

Comments

@agoncal
Copy link
Contributor

agoncal commented Nov 13, 2022

Describe the bug

In JAX-RS we used to use @Context to inject several managed objects, including UriInfo:

import jakarta.ws.rs.core.Context;

@Path("/hello")
public class GreetingResource {

    @Context
    UriInfo uriInfo;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        System.out.println("Base URI: " + uriInfo.getBaseUri());
        return "Hello RESTEasy";
    }
}

With Jakarta EE 10 and JAX-RS 4 we can use @Inject instead:

@Inject
UriInfo uriInfo;

ATM, this code breaks with RESTEasy Classic (it works with RESTEasy Reactive) with the following exception:

Caused by: jakarta.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type jakarta.ws.rs.core.UriInfo and qualifiers [@Default]
        - java member: org.acme.GreetingResource#uriInfo
        - declared on CLASS bean [types=[org.acme.GreetingResource, java.lang.Object], qualifiers=[@Default, @Any], target=org.acme.GreeetingResource]
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-reactive</artifactId>
    </dependency>

See https://jakarta.ee/specifications/restful-ws/4.0
Relates to: jakartaee/rest#951

Expected behavior

No response

Actual behavior

No response

How to Reproduce?

No response

Output of uname -a or ver

No response

Output of java -version

No response

GraalVM version (if different from Java)

No response

Quarkus version or git rev

3.0.0.Alpha1

Build tool (ie. output of mvnw --version or gradlew --version)

No response

Additional information

No response

@agoncal agoncal added the kind/bug Something isn't working label Nov 13, 2022
@geoand
Copy link
Contributor

geoand commented Nov 14, 2022

Are you using quarkus-resteasy or quarkus-resteasy-reactive?

Asking because the latter should already support what you are asking for (I don't know about the former)

@quarkus-bot
Copy link

quarkus-bot bot commented Nov 14, 2022

@agoncal agoncal changed the title Jakarta - EE 10 - @Inject should work instead of the deprecated @Context Jakarta - EE 10 - @Inject should work instead of the deprecated @Context in RESTEasy Classi Nov 14, 2022
@agoncal agoncal changed the title Jakarta - EE 10 - @Inject should work instead of the deprecated @Context in RESTEasy Classi Jakarta - EE 10 - @Inject should work instead of the deprecated @Context in RESTEasy Classic Nov 14, 2022
@agoncal
Copy link
Contributor Author

agoncal commented Nov 14, 2022

@geoand good point. I've just updated the title and body of the issue. It works with RestEasy Reactive but not RestEasy Classic

@geoand
Copy link
Contributor

geoand commented Nov 14, 2022

Thanks for checking.

cc @jamezp @gsmet for RESTEasy Classic

@jamezp
Copy link
Contributor

jamezp commented Nov 14, 2022

It should work with RESTEasy 6.2 if the org.jboss.resteasy:resteasy-cdi dependency is used. Method parameter injection does not currently work, but it's also not required in Jakarta REST 3.1.

@agoncal
Copy link
Contributor Author

agoncal commented Nov 14, 2022

Correct, if I have the following 2 dependencies, it works. Will it stay like that or will depending just on quarkus-resteasy be enough in future releases?

  <dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-resteasy</artifactId>
  </dependency>
  <dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-cdi</artifactId>
  </dependency>

@gsmet
Copy link
Member

gsmet commented Nov 15, 2022

We should definitely add resteasy-cdi. I will take care of it.

@gsmet gsmet self-assigned this Nov 15, 2022
gsmet added a commit to gsmet/quarkus that referenced this issue Nov 16, 2022
@quarkus-bot quarkus-bot bot added this to the 2.15 - main milestone Nov 16, 2022
@gsmet gsmet modified the milestones: 2.15 - main, 2.14.2.Final Nov 21, 2022
gsmet added a commit to gsmet/quarkus that referenced this issue Nov 21, 2022
michalvavrik added a commit to michalvavrik/quarkus-test-suite that referenced this issue Mar 9, 2023
[It is now possible](gsmet/quarkus@2d43322) to use `@Inject` on field injection points in RESTEasy Classic providers instead of `@Context.
RESTEasy Reactive has already supported this approach. [There is a plan to drop support](quarkusio/quarkus#29240)
of `@Context` on method parameters too, however this is not supported by RESTEasy (classic) yet.

- module HTTP advanced:
  - `UriInfo` is newly injected with `@Inject`, however legacy `@Context` is stll used for `HttpRequest` injection
- module HTTP advanced reactive:
  - in `PremierLeagueContainerRequestFilter`, `UriInfo` is newly injected with `@Inject`, however legacy `@Context` is stll used for `HttpRequest` injection 
  - in `StringResponseSerializer`, `HttpServerRequest` is newly using `@Inject`
- module HTTP Rest Client Reactive:
  - I dropped the only `@Conext` that was there as the provider member wasn't used at all
- module OpenTelemetry Reactive (uses RESTEasy Mutiny):
  - dropped `@Context` from resource method param as we have several tests where resources are using `@Context` and I consider it interesting that `@Context` is in fact not needed there at all (we can see similar behavior on multiple RESTEasy Reactive upstream tests, e.g. in `io.quarkus.resteasy.reactive.jsonb.deployment.test.sse.SseResource`, `io.quarkus.resteasy.reactive.jackson.deployment.test.streams.StreamResource` and plenty others (same goes for `@Context` usage, it's frequently used in upstream)
- module Security - Basic
  - dropped `@Context SecurityContext security` from resource method parameters and instead, I used `@Inject` on setter. IMO we should test this pattern as it's legit and in this module, `@Context SecurityContext security` is used in 5 other cases (`UserResource`, `DenyAllResource`, `EveryoneResource`, `PermitAllResource` and `UnannotatedResource`). Note: `@Inject` can't be used on fields.

This change is done according to the [QUARKUS-2736 test plan](https://github.com/quarkus-qe/quarkus-test-plans/blob/main/QUARKUS-2736.md).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/jakarta kind/bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants