Skip to content

Commit

Permalink
Merge pull request #22644 from geoand/#21700
Browse files Browse the repository at this point in the history
Ensure that UTF8 is used as the default encoding in RESTEasy Reactive
  • Loading branch information
gsmet authored Jan 5, 2022
2 parents 4596d74 + fe79da7 commit 3063535
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.quarkus.resteasy.reactive.jackson.deployment.test.response;

import java.util.function.Supplier;

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

import org.hamcrest.Matchers;
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;
import io.restassured.RestAssured;

public class ResponseStringNonAsciiTest {

@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() {
RestAssured.get("/hello")
.then().statusCode(200)
.and().body(Matchers.equalTo("{\"message\": \"Καλημέρα κόσμε\"}"))
.and().contentType("application/json");
}

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

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response hello() {
return Response.ok("{\"message\": \"Καλημέρα κόσμε\"}").build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;

import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;
Expand Down Expand Up @@ -35,7 +36,7 @@ public void writeResponse(Object o, Type genericType, ServerRequestContext conte
setContentTypeIfNecessary(context);
OutputStream stream = context.getOrCreateOutputStream();
if (o instanceof String) { // YUK: done in order to avoid adding extra quotes...
stream.write(((String) o).getBytes());
stream.write(((String) o).getBytes(StandardCharsets.UTF_8));
} else {
defaultWriter.writeValue(stream, o);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -46,7 +47,7 @@ public void writeResponse(Object o, Type genericType, ServerRequestContext conte
setContentTypeIfNecessary(context);
OutputStream stream = context.getOrCreateOutputStream();
if (o instanceof String) { // YUK: done in order to avoid adding extra quotes...
stream.write(((String) o).getBytes());
stream.write(((String) o).getBytes(StandardCharsets.UTF_8));
} else {
// First test the names to see if JsonView is used. We do this to avoid doing reflection for the common case
// where JsonView is not used
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.quarkus.kotlin.serialization

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToStream
import kotlinx.serialization.serializer
Expand All @@ -11,6 +10,7 @@ import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter.AllWriteab
import org.jboss.resteasy.reactive.server.spi.ServerRequestContext
import java.io.OutputStream
import java.lang.reflect.Type
import java.nio.charset.StandardCharsets
import javax.inject.Inject
import javax.ws.rs.Produces
import javax.ws.rs.core.MediaType
Expand All @@ -25,7 +25,7 @@ class KotlinSerializationMessageBodyWriter(@Inject var json: Json) : AllWriteabl
) {
JsonMessageBodyWriterUtil.setContentTypeIfNecessary(httpHeaders)
if (o is String) { // YUK: done in order to avoid adding extra quotes...
entityStream.write(o.toByteArray())
entityStream.write(o.toByteArray(StandardCharsets.UTF_8))
} else {
json.encodeToStream(o, entityStream)
}
Expand All @@ -37,7 +37,7 @@ class KotlinSerializationMessageBodyWriter(@Inject var json: Json) : AllWriteabl
val stream: OutputStream = NoopCloseAndFlushOutputStream(originalStream)

if (o is String) { // YUK: done in order to avoid adding extra quotes...
stream.write(o.toByteArray())
stream.write(o.toByteArray(StandardCharsets.UTF_8))
} else {
json.encodeToStream(serializer(genericType), o, stream)
}
Expand All @@ -60,4 +60,4 @@ class KotlinSerializationMessageBodyWriter(@Inject var json: Json) : AllWriteabl
delegate.write(b, off, len)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.nio.charset.StandardCharsets;
import javax.ws.rs.core.MultivaluedMap;

public final class JacksonMessageBodyWriterUtil {
Expand Down Expand Up @@ -43,7 +44,7 @@ public static void doLegacyWrite(Object o, Annotation[] annotations, Multivalued
OutputStream entityStream, ObjectWriter defaultWriter) throws IOException {
setContentTypeIfNecessary(httpHeaders);
if (o instanceof String) { // YUK: done in order to avoid adding extra quotes...
entityStream.write(((String) o).getBytes());
entityStream.write(((String) o).getBytes(StandardCharsets.UTF_8));
} else {
if (annotations != null) {
for (Annotation annotation : annotations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import javax.inject.Inject;
import javax.json.bind.Jsonb;
import javax.ws.rs.WebApplicationException;
Expand All @@ -28,7 +29,7 @@ public void writeTo(Object o, Class<?> type, Type genericType, Annotation[] anno
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
JsonMessageBodyWriterUtil.setContentTypeIfNecessary(httpHeaders);
if (o instanceof String) { // YUK: done in order to avoid adding extra quotes...
entityStream.write(((String) o).getBytes());
entityStream.write(((String) o).getBytes(StandardCharsets.UTF_8));
} else {
json.toJson(o, type, entityStream);
}
Expand All @@ -41,7 +42,7 @@ public void writeResponse(Object o, Type genericType, ServerRequestContext conte
OutputStream originalStream = context.getOrCreateOutputStream();
OutputStream stream = new NoopCloseAndFlushOutputStream(originalStream);
if (o instanceof String) { // YUK: done in order to avoid adding extra quotes...
stream.write(((String) o).getBytes());
stream.write(((String) o).getBytes(StandardCharsets.UTF_8));
} else {
json.toJson(o, stream);
}
Expand Down

0 comments on commit 3063535

Please sign in to comment.