Skip to content

Commit

Permalink
Merge pull request #45097 from geoand/#45084
Browse files Browse the repository at this point in the history
Ensure that jakarta json types can be deserialized in native mode
  • Loading branch information
geoand authored Dec 13, 2024
2 parents 74ce405 + 0879ce4 commit 3caeb08
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<JsonArray> {
public class ServerJsonArrayHandler extends JsonArrayHandler
implements ServerMessageBodyWriter<JsonArray>, ServerMessageBodyReader<JsonArray> {

@Override
public boolean isWriteable(Class<?> type, Type genericType, ResteasyReactiveResourceInfo target, MediaType mediaType) {
Expand All @@ -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<JsonArray> type, Type genericType, MediaType mediaType,
ServerRequestContext context) throws WebApplicationException, IOException {
return JsonpUtil.reader(context.getInputStream(), mediaType).readArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonObject> {
public class ServerJsonObjectHandler extends JsonObjectHandler
implements ServerMessageBodyWriter<JsonObject>, ServerMessageBodyReader<JsonObject> {

@Override
public boolean isWriteable(Class<?> type, Type genericType, ResteasyReactiveResourceInfo target, MediaType mediaType) {
Expand All @@ -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<JsonObject> type, Type genericType, MediaType mediaType,
ServerRequestContext context) throws WebApplicationException {
return JsonpUtil.reader(context.getInputStream(), mediaType).readObject();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<JsonStructure> {
implements ServerMessageBodyWriter<JsonStructure>, ServerMessageBodyReader<JsonStructure> {

@Override
public boolean isWriteable(Class<?> type, Type genericType, ResteasyReactiveResourceInfo target, MediaType mediaType) {
Expand All @@ -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<JsonStructure> type, Type genericType, MediaType mediaType,
ServerRequestContext context) throws WebApplicationException, IOException {
return JsonpUtil.reader(context.getInputStream(), mediaType).read();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<JsonValue> {
public class ServerJsonValueHandler extends JsonValueHandler
implements ServerMessageBodyWriter<JsonValue>, ServerMessageBodyReader<JsonValue> {

@Override
public boolean isWriteable(Class<?> type, Type genericType, ResteasyReactiveResourceInfo target, MediaType mediaType) {
Expand All @@ -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<JsonValue> type, Type genericType, MediaType mediaType,
ServerRequestContext context) throws WebApplicationException, IOException {
return JsonpUtil.reader(context.getInputStream(), mediaType).readValue();
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3caeb08

Please sign in to comment.