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 incorrect generic type passed to MessageBodyWriter#writeTo #31864

Merged
merged 2 commits into from
Mar 15, 2023
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
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));
}
}

}