Skip to content

Commit

Permalink
Add UTF-8 as charset when RESTEasy Reactive returns text/plain
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Aug 24, 2021
1 parent d1e65ac commit decd432
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ public void testAnnotationFreeSubresource() throws Exception {
Assertions.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
Assertions.assertEquals(response.readEntity(String.class), "got");
Assertions.assertNotNull(response.getHeaderString("Content-Type"));
Assertions.assertNotNull(response.getHeaderString("Content-Type"));
Assertions.assertEquals(MediaType.TEXT_PLAIN_TYPE.toString(),
Assertions.assertEquals("text/plain;charset=UTF-8",
response.getHeaderString("Content-Type"));
}
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -490,6 +491,7 @@ private ResourceMethod createResourceMethod(ClassInfo currentClassInfo, ClassInf

String[] produces = extractProducesConsumesValues(currentMethodInfo.annotation(PRODUCES), classProduces);
produces = applyDefaultProduces(produces, nonAsyncReturnType);
produces = addDefaultCharsets(produces);

String sseElementType = classSseElementType;
AnnotationInstance sseElementTypeAnnotation = currentMethodInfo.annotation(REST_SSE_ELEMENT_TYPE);
Expand Down Expand Up @@ -626,6 +628,22 @@ private String[] applyDefaultProduces(String[] produces, Type nonAsyncReturnType
return applyAdditionalDefaults(nonAsyncReturnType);
}

// see https://github.com/quarkusio/quarkus/issues/19535
private String[] addDefaultCharsets(String[] produces) {
if ((produces == null) || (produces.length == 0)) {
return produces;
}
List<String> result = new ArrayList<>(produces.length);
for (String p : produces) {
if (p.equals(MediaType.TEXT_PLAIN)) {
result.add(MediaType.TEXT_PLAIN + ";charset=" + StandardCharsets.UTF_8.name());
} else {
result.add(p);
}
}
return result.toArray(EMPTY_STRING_ARRAY);
}

protected String[] applyAdditionalDefaults(Type nonAsyncReturnType) {
// FIXME: primitives
if (STRING.equals(nonAsyncReturnType.name()))
Expand Down Expand Up @@ -724,7 +742,7 @@ private static String[] extractProducesConsumesValues(AnnotationInstance annotat
result.add(t.trim());
}
}
return result.toArray(new String[0]);
return result.toArray(EMPTY_STRING_ARRAY);
} else {
return originalStrings;
}
Expand Down

0 comments on commit decd432

Please sign in to comment.