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

Fix issue where RESTEasy Reactive kept incorrect media type state #20149

Merged
merged 1 commit into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.quarkus.resteasy.reactive.server.test.mediatype;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.is;

import java.util.function.Supplier;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class ContentTypeCaseTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.setArchiveProducer(new Supplier<>() {
@Override
public JavaArchive get() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(HelloResource.class);
}
});

@Test
public void test() {
given().header("test", "TeXt/Plain").get("/hello")
.then()
.statusCode(200)
.contentType("text/plain")
.body(is("text/plain"));

given().header("test", "text/plain").get("/hello")
.then()
.statusCode(200)
.contentType("text/plain")
.body(is("text/plain"));

given().header("test", "TEXT/PLAIN").get("/hello")
.then()
.statusCode(200)
.contentType("text/plain")
.body(is("text/plain"));
}

@Path("hello")
public static class HelloResource {

@GET
public Response hello(@HeaderParam("test") String contentType) {
MediaType mediaType = MediaType.valueOf(contentType);
return Response.ok(mediaType.toString()).header("content-type", mediaType).build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class MediaTypeHeaderDelegate implements RuntimeDelegate.HeaderDelegate<M
public static final MediaTypeHeaderDelegate INSTANCE = new MediaTypeHeaderDelegate();
private static final int MAX_MT_CACHE_SIZE = 200;
private static final char[] quotedChars = "()<>@,;:\\\"/[]?= \t\r\n".toCharArray();
private static final Map<String, MediaType> map = new ConcurrentHashMap<String, MediaType>();
private static final Map<MediaType, String> reverseMap = new ConcurrentHashMap<MediaType, String>();
private static final Map<String, MediaType> map = new ConcurrentHashMap<>();
private static final Map<MediaType, String> reverseMap = new ConcurrentHashMap<>();

protected static boolean isValid(String str) {
if (str == null || str.length() == 0)
Expand Down Expand Up @@ -55,7 +55,7 @@ public static MediaType parse(String type) {
reverseMap.clear();
}
map.put(type, result);
reverseMap.put(result, type);
reverseMap.put(result, internalToString(result));
}
return result;
}
Expand Down Expand Up @@ -141,10 +141,10 @@ public String toString(MediaType o) {
return result;
}

private String internalToString(MediaType type) {
StringBuilder buf = new StringBuilder();
private static String internalToString(MediaType type) {
StringBuilder buf = new StringBuilder(type.getType().length() + type.getSubtype().length() + 1);

buf.append(type.getType().toLowerCase()).append("/").append(type.getSubtype().toLowerCase());
buf.append(type.getType().toLowerCase()).append('/').append(type.getSubtype().toLowerCase());
if (type.getParameters() == null || type.getParameters().size() == 0)
return buf.toString();
for (String name : type.getParameters().keySet()) {
Expand Down