Skip to content

Commit 0566557

Browse files
geoandgsmet
authored andcommitted
Fix incorrect generic type passed to MessageBodyWriter#writeTo
Fixes: #31818 (cherry picked from commit 5221dac)
1 parent 12be197 commit 0566557

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/core/ServerSerialisers.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,19 @@ public static boolean invokeWriter(ResteasyReactiveRequestContext context, Objec
198198
WriterInterceptor[] writerInterceptors = context.getWriterInterceptors();
199199
boolean outputStreamSet = context.getOutputStream() != null;
200200
context.serverResponse().setPreCommitListener(HEADER_FUNCTION);
201+
202+
RuntimeResource target = context.getTarget();
203+
Type genericType;
204+
if (context.hasGenericReturnType()) { // make sure that when a Response with a GenericEntity was returned, we use it
205+
genericType = context.getGenericReturnType();
206+
} else {
207+
genericType = target == null ? null : target.getReturnType();
208+
}
209+
201210
try {
202211
if (writer instanceof ServerMessageBodyWriter && writerInterceptors == null && !outputStreamSet) {
203212
ServerMessageBodyWriter<Object> quarkusRestWriter = (ServerMessageBodyWriter<Object>) writer;
204-
RuntimeResource target = context.getTarget();
205-
Type genericType;
206-
if (context.hasGenericReturnType()) { // make sure that when a Response with a GenericEntity was returned, we use it
207-
genericType = context.getGenericReturnType();
208-
} else {
209-
genericType = target == null ? null : target.getReturnType();
210-
}
213+
211214
Class<?> entityClass = entity.getClass();
212215
if (quarkusRestWriter.isWriteable(
213216
entityClass,
@@ -230,7 +233,7 @@ public static boolean invokeWriter(ResteasyReactiveRequestContext context, Objec
230233
context.setResponseContentType(mediaType);
231234
}
232235
if (writerInterceptors == null) {
233-
writer.writeTo(entity, entity.getClass(), context.getGenericReturnType(),
236+
writer.writeTo(entity, entity.getClass(), genericType,
234237
context.getAllAnnotations(), context.getResponseMediaType(), response.getHeaders(),
235238
context.getOrCreateOutputStream());
236239
context.getOrCreateOutputStream().close();

independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/SimpleQuarkusRestResource.java

+6
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ public TestClass writer() {
233233
return new TestClass();
234234
}
235235

236+
@GET
237+
@Path("uni-writer")
238+
public Uni<TestClass> uniWriter() {
239+
return Uni.createFrom().item(new TestClass());
240+
}
241+
236242
@GET
237243
@Path("fast-writer")
238244
@Produces("text/plain")

independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/SimpleQuarkusRestTestCase.java

+2
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ public void testWriter() {
206206
.then().body(Matchers.equalTo("OK"));
207207
RestAssured.get("/simple/writer")
208208
.then().body(Matchers.equalTo("WRITER"));
209+
RestAssured.get("/simple/uni-writer")
210+
.then().body(Matchers.equalTo("WRITER"));
209211

210212
RestAssured.get("/simple/fast-writer")
211213
.then().body(Matchers.equalTo("OK"));

independent-projects/resteasy-reactive/server/vertx/src/test/java/org/jboss/resteasy/reactive/server/vertx/test/simple/TestWriter.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotat
2626
public void writeTo(TestClass t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
2727
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
2828
throws IOException, WebApplicationException {
29-
entityStream.write("WRITER".getBytes(StandardCharsets.UTF_8));
29+
if (genericType.getTypeName().equals(TestClass.class.getName())) {
30+
entityStream.write("WRITER".getBytes(StandardCharsets.UTF_8));
31+
} else {
32+
entityStream.write("INCORRECT GENERIC TYPE".getBytes(StandardCharsets.UTF_8));
33+
}
3034
}
3135

3236
}

0 commit comments

Comments
 (0)