From 0879ce431e1cbdb599cf852c4fc142ea98dba22c Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Thu, 12 Dec 2024 20:45:16 +0200 Subject: [PATCH] Ensure that jakarta json types can be deserialized in native mode Fixes: #45084 --- .../jsonp/ServerJsonArrayHandler.java | 16 ++++++++++++- .../jsonp/ServerJsonObjectHandler.java | 15 +++++++++++- .../jsonp/ServerJsonStructureHandler.java | 15 +++++++++++- .../jsonp/ServerJsonValueHandler.java | 16 ++++++++++++- .../java/io/quarkus/it/qute/JsonResource.java | 24 +++++++++++++++++++ .../java/io/quarkus/it/qute/QuteTestCase.java | 12 ++++++++++ 6 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 integration-tests/qute/src/main/java/io/quarkus/it/qute/JsonResource.java diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/providers/serialisers/jsonp/ServerJsonArrayHandler.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/providers/serialisers/jsonp/ServerJsonArrayHandler.java index 8ea6a5ec31d3f..750adf79c8678 100644 --- a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/providers/serialisers/jsonp/ServerJsonArrayHandler.java +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/providers/serialisers/jsonp/ServerJsonArrayHandler.java @@ -1,6 +1,7 @@ package org.jboss.resteasy.reactive.server.providers.serialisers.jsonp; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.lang.reflect.Type; import jakarta.json.JsonArray; @@ -11,10 +12,12 @@ import org.jboss.resteasy.reactive.common.providers.serialisers.jsonp.JsonArrayHandler; import org.jboss.resteasy.reactive.common.providers.serialisers.jsonp.JsonpUtil; import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo; +import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyReader; import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter; import org.jboss.resteasy.reactive.server.spi.ServerRequestContext; -public class ServerJsonArrayHandler extends JsonArrayHandler implements ServerMessageBodyWriter { +public class ServerJsonArrayHandler extends JsonArrayHandler + implements ServerMessageBodyWriter, ServerMessageBodyReader { @Override public boolean isWriteable(Class type, Type genericType, ResteasyReactiveResourceInfo target, MediaType mediaType) { @@ -30,4 +33,15 @@ public void writeResponse(JsonArray o, Type genericType, ServerRequestContext co context.serverResponse().end(out.toByteArray()); } + @Override + public boolean isReadable(Class type, Type genericType, ResteasyReactiveResourceInfo lazyMethod, + MediaType mediaType) { + return JsonArray.class.isAssignableFrom(type); + } + + @Override + public JsonArray readFrom(Class type, Type genericType, MediaType mediaType, + ServerRequestContext context) throws WebApplicationException, IOException { + return JsonpUtil.reader(context.getInputStream(), mediaType).readArray(); + } } diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/providers/serialisers/jsonp/ServerJsonObjectHandler.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/providers/serialisers/jsonp/ServerJsonObjectHandler.java index fcaa1d9091bc2..ba5dc772cdbd9 100644 --- a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/providers/serialisers/jsonp/ServerJsonObjectHandler.java +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/providers/serialisers/jsonp/ServerJsonObjectHandler.java @@ -11,10 +11,12 @@ import org.jboss.resteasy.reactive.common.providers.serialisers.jsonp.JsonObjectHandler; import org.jboss.resteasy.reactive.common.providers.serialisers.jsonp.JsonpUtil; import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo; +import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyReader; import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter; import org.jboss.resteasy.reactive.server.spi.ServerRequestContext; -public class ServerJsonObjectHandler extends JsonObjectHandler implements ServerMessageBodyWriter { +public class ServerJsonObjectHandler extends JsonObjectHandler + implements ServerMessageBodyWriter, ServerMessageBodyReader { @Override public boolean isWriteable(Class type, Type genericType, ResteasyReactiveResourceInfo target, MediaType mediaType) { @@ -30,4 +32,15 @@ public void writeResponse(JsonObject o, Type genericType, ServerRequestContext c context.serverResponse().end(out.toByteArray()); } + @Override + public boolean isReadable(Class type, Type genericType, ResteasyReactiveResourceInfo lazyMethod, + MediaType mediaType) { + return JsonObject.class.isAssignableFrom(type); + } + + @Override + public JsonObject readFrom(Class type, Type genericType, MediaType mediaType, + ServerRequestContext context) throws WebApplicationException { + return JsonpUtil.reader(context.getInputStream(), mediaType).readObject(); + } } diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/providers/serialisers/jsonp/ServerJsonStructureHandler.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/providers/serialisers/jsonp/ServerJsonStructureHandler.java index ef055f5905fe4..ac9c7e6de030c 100644 --- a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/providers/serialisers/jsonp/ServerJsonStructureHandler.java +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/providers/serialisers/jsonp/ServerJsonStructureHandler.java @@ -1,6 +1,7 @@ package org.jboss.resteasy.reactive.server.providers.serialisers.jsonp; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.lang.reflect.Type; import jakarta.json.JsonObject; @@ -12,11 +13,12 @@ import org.jboss.resteasy.reactive.common.providers.serialisers.jsonp.JsonStructureHandler; import org.jboss.resteasy.reactive.common.providers.serialisers.jsonp.JsonpUtil; import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo; +import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyReader; import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter; import org.jboss.resteasy.reactive.server.spi.ServerRequestContext; public class ServerJsonStructureHandler extends JsonStructureHandler - implements ServerMessageBodyWriter { + implements ServerMessageBodyWriter, ServerMessageBodyReader { @Override public boolean isWriteable(Class type, Type genericType, ResteasyReactiveResourceInfo target, MediaType mediaType) { @@ -32,4 +34,15 @@ public void writeResponse(JsonStructure o, Type genericType, ServerRequestContex context.serverResponse().end(out.toByteArray()); } + @Override + public boolean isReadable(Class type, Type genericType, ResteasyReactiveResourceInfo lazyMethod, + MediaType mediaType) { + return JsonStructure.class.isAssignableFrom(type) && !JsonObject.class.isAssignableFrom(type); + } + + @Override + public JsonStructure readFrom(Class type, Type genericType, MediaType mediaType, + ServerRequestContext context) throws WebApplicationException, IOException { + return JsonpUtil.reader(context.getInputStream(), mediaType).read(); + } } diff --git a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/providers/serialisers/jsonp/ServerJsonValueHandler.java b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/providers/serialisers/jsonp/ServerJsonValueHandler.java index b75d0d68b8961..e74804914e9c3 100644 --- a/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/providers/serialisers/jsonp/ServerJsonValueHandler.java +++ b/independent-projects/resteasy-reactive/server/runtime/src/main/java/org/jboss/resteasy/reactive/server/providers/serialisers/jsonp/ServerJsonValueHandler.java @@ -1,6 +1,7 @@ package org.jboss.resteasy.reactive.server.providers.serialisers.jsonp; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.lang.reflect.Type; import jakarta.json.JsonValue; @@ -11,10 +12,12 @@ import org.jboss.resteasy.reactive.common.providers.serialisers.jsonp.JsonValueHandler; import org.jboss.resteasy.reactive.common.providers.serialisers.jsonp.JsonpUtil; import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveResourceInfo; +import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyReader; import org.jboss.resteasy.reactive.server.spi.ServerMessageBodyWriter; import org.jboss.resteasy.reactive.server.spi.ServerRequestContext; -public class ServerJsonValueHandler extends JsonValueHandler implements ServerMessageBodyWriter { +public class ServerJsonValueHandler extends JsonValueHandler + implements ServerMessageBodyWriter, ServerMessageBodyReader { @Override public boolean isWriteable(Class type, Type genericType, ResteasyReactiveResourceInfo target, MediaType mediaType) { @@ -30,4 +33,15 @@ public void writeResponse(JsonValue o, Type genericType, ServerRequestContext co context.serverResponse().end(out.toByteArray()); } + @Override + public boolean isReadable(Class type, Type genericType, ResteasyReactiveResourceInfo lazyMethod, + MediaType mediaType) { + return JsonValue.class.isAssignableFrom(type); + } + + @Override + public JsonValue readFrom(Class type, Type genericType, MediaType mediaType, + ServerRequestContext context) throws WebApplicationException, IOException { + return JsonpUtil.reader(context.getInputStream(), mediaType).readValue(); + } } diff --git a/integration-tests/qute/src/main/java/io/quarkus/it/qute/JsonResource.java b/integration-tests/qute/src/main/java/io/quarkus/it/qute/JsonResource.java new file mode 100644 index 0000000000000..9699c5a7d1fc9 --- /dev/null +++ b/integration-tests/qute/src/main/java/io/quarkus/it/qute/JsonResource.java @@ -0,0 +1,24 @@ +package io.quarkus.it.qute; + +import jakarta.inject.Inject; +import jakarta.json.JsonObject; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.MediaType; + +import io.quarkus.qute.Template; +import io.quarkus.qute.TemplateInstance; + +@Path("json") +public class JsonResource { + + @Inject + Template hello; + + @POST + @Produces(MediaType.TEXT_HTML) + public TemplateInstance get(JsonObject request) { + return hello.data("name", request.get("name")); + } +} diff --git a/integration-tests/qute/src/test/java/io/quarkus/it/qute/QuteTestCase.java b/integration-tests/qute/src/test/java/io/quarkus/it/qute/QuteTestCase.java index 81c791ea56e18..5124c67bd95b7 100644 --- a/integration-tests/qute/src/test/java/io/quarkus/it/qute/QuteTestCase.java +++ b/integration-tests/qute/src/test/java/io/quarkus/it/qute/QuteTestCase.java @@ -28,6 +28,18 @@ public void testTemplates() throws InterruptedException { .body(containsString("Hello Ciri!")); RestAssured.when().get("/beer").then().body(containsString("Beer Pilsner, completed: true, done: true")); RestAssured.when().get("/defaultmethod").then().body(containsString("Hello MK")); + RestAssured + .given() + .contentType("application/json") + .body(""" + { + "name": "foo" + } + """) + .when().post("/json") + .then() + .statusCode(200) + .body(containsString("foo")); } @Test