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

getAcceptableMediaTypes call in PreMatching filter prevent resource matching with manipulated accept headers #5317

Closed
nmuser opened this issue Apr 27, 2023 · 3 comments · Fixed by #5320

Comments

@nmuser
Copy link

nmuser commented Apr 27, 2023

I'm using accept headers for rest api versioning. A @PreMatching request filter is used to set a default rest api version in case the client does not specify the version:

@Provider
@PreMatching
public class APIVersionRequestFilterr implements ContainerRequestFilter  {
  @Override
  public void filter(ContainerRequestContext containerRequestContext) throws IOException {
    List<MediaType> mediaTypes = containerRequestContext.getAcceptableMediaTypes();
    containerRequestContext.getHeaders().get("accept").add("application/vnd.x.qi-v3.0+json");
  }
}

When I use getAcceptableMediaTypes() to check if the client requested a specific version the following accept header modification has no effect for the resource matching.

When i do not call getAcceptableMediaTypes or use jersey < 2.38 the accept header modification effects the resource matching:

@Path("/")
public class VersionV3{

  @Post
  @Produces({"application/vnd.x.qi-v3.0+json" })
  @Path("/service")
  public Response do(){
    return Response.ok().build();
  }
}
@jansupol
Copy link
Contributor

I suspect this has been fixed in 2.39.1. What Jersey do you use?

@nmuser
Copy link
Author

nmuser commented Apr 28, 2023

I tested 2.38, 2.39 and 2.39.1. In all these versions the resource matching changed. Maybe the new acceptTypeCache in InboundMessageContext is the reason the manipulated headers will be ignored and the accepted types in cache will be used for resource matching?
Thanks for your help

@jansupol
Copy link
Contributor

jansupol commented Apr 28, 2023

Yes, that is a bug. As a workaround, please use containerRequestContext.getHeaders().putSingle("accept","application/vnd.x.qi-v3.0+json");. Well, if you do not mind replacing the existing accept headers, though...

@jansupol jansupol linked a pull request May 3, 2023 that will close this issue
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 a pull request may close this issue.

2 participants