Skip to content

Commit

Permalink
Merge pull request #31864 from geoand/#31818
Browse files Browse the repository at this point in the history
Fix incorrect generic type passed to MessageBodyWriter#writeTo
  • Loading branch information
geoand authored Mar 15, 2023
2 parents 5d1c234 + 304d30e commit 64ced51
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,19 @@ public static boolean invokeWriter(ResteasyReactiveRequestContext context, Objec
WriterInterceptor[] writerInterceptors = context.getWriterInterceptors();
boolean outputStreamSet = context.getOutputStream() != null;
context.serverResponse().setPreCommitListener(HEADER_FUNCTION);

RuntimeResource target = context.getTarget();
Type genericType;
if (context.hasGenericReturnType()) { // make sure that when a Response with a GenericEntity was returned, we use it
genericType = context.getGenericReturnType();
} else {
genericType = target == null ? null : target.getReturnType();
}

try {
if (writer instanceof ServerMessageBodyWriter && writerInterceptors == null && !outputStreamSet) {
ServerMessageBodyWriter<Object> quarkusRestWriter = (ServerMessageBodyWriter<Object>) writer;
RuntimeResource target = context.getTarget();
Type genericType;
if (context.hasGenericReturnType()) { // make sure that when a Response with a GenericEntity was returned, we use it
genericType = context.getGenericReturnType();
} else {
genericType = target == null ? null : target.getReturnType();
}

Class<?> entityClass = entity.getClass();
if (quarkusRestWriter.isWriteable(
entityClass,
Expand All @@ -234,7 +237,7 @@ public static boolean invokeWriter(ResteasyReactiveRequestContext context, Objec
context.setResponseContentType(mediaType);
}
if (writerInterceptors == null) {
writer.writeTo(entity, entity.getClass(), context.getGenericReturnType(),
writer.writeTo(entity, entity.getClass(), genericType,
context.getAllAnnotations(), context.getResponseMediaType(), response.getHeaders(),
context.getOrCreateOutputStream());
context.getOrCreateOutputStream().close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void handle(ResteasyReactiveRequestContext requestContext) throws Excepti
try {
effectiveRequestType = MediaType.valueOf((String) requestType);
} catch (Exception e) {
log.debugv("Incorrect media type", e);
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).build());
}

Expand All @@ -70,6 +71,7 @@ public void handle(ResteasyReactiveRequestContext requestContext) throws Excepti
}
List<MessageBodyReader<?>> readers = serialisers.findReaders(null, type, effectiveRequestType, RuntimeType.SERVER);
if (readers.isEmpty()) {
log.debugv("No matching MessageBodyReader found for type {0} and media type {1}", type, effectiveRequestType);
throw new NotSupportedException();
}
for (MessageBodyReader<?> reader : readers) {
Expand Down Expand Up @@ -102,6 +104,7 @@ public void handle(ResteasyReactiveRequestContext requestContext) throws Excepti
return;
}
}
log.debugv("No matching MessageBodyReader found for type {0} and media type {1}", type, effectiveRequestType);
throw new NotSupportedException("No supported MessageBodyReader found");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ public TestClass writer() {
return new TestClass();
}

@GET
@Path("uni-writer")
public Uni<TestClass> uniWriter() {
return Uni.createFrom().item(new TestClass());
}

@GET
@Path("fast-writer")
@Produces("text/plain")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ public void testWriter() {
.then().body(Matchers.equalTo("OK"));
RestAssured.get("/simple/writer")
.then().body(Matchers.equalTo("WRITER"));
RestAssured.get("/simple/uni-writer")
.then().body(Matchers.equalTo("WRITER"));

RestAssured.get("/simple/fast-writer")
.then().body(Matchers.equalTo("OK"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotat
public void writeTo(TestClass t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
throws IOException, WebApplicationException {
entityStream.write("WRITER".getBytes(StandardCharsets.UTF_8));
if (genericType.getTypeName().equals(TestClass.class.getName())) {
entityStream.write("WRITER".getBytes(StandardCharsets.UTF_8));
} else {
entityStream.write("INCORRECT GENERIC TYPE".getBytes(StandardCharsets.UTF_8));
}
}

}

0 comments on commit 64ced51

Please sign in to comment.