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

Ensure that jakarta json types can be deserialized in native mode #45097

Merged
merged 1 commit into from
Dec 13, 2024
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
@@ -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"));
Comment on lines +31 to +42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add the test in something more core than the Qute ITs. Reasoning is that if at some point we drop Qute, we will lose these tests which are related to basic core features.

(Yeah, sorry for being annoying!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked for another place and didn't find a good candidate.

Agreed it's not a good place, but I have no better candidates in mind

}

@Test
Expand Down
Loading