From 01d2d550c5ac08b47655b76e132666b5fc09e00f Mon Sep 17 00:00:00 2001 From: Luciano Vernaschi Date: Tue, 25 Jun 2024 13:22:52 +0200 Subject: [PATCH] feat: add support for JsonNode subclasses (#2537) * Add support for JsonNode subclasses * Restrict to the 3 non-value types --------- Co-authored-by: Vlad Rindevich --- .../transfertypes/TransferTypesPlugin.java | 4 + .../jsonnode/JsonNodeEndpoint.java | 12 ++- .../transfertypes/jsonnode/Nonnull.java | 11 +++ .../transfertypes/jsonnode/Nullable.java | 11 +++ .../transfertypes/jsonnode/openapi.json | 74 ++++++++++++++++++- 5 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 packages/java/parser-jvm-plugin-transfertypes/src/test/java/com/vaadin/hilla/parser/plugins/transfertypes/jsonnode/Nonnull.java create mode 100644 packages/java/parser-jvm-plugin-transfertypes/src/test/java/com/vaadin/hilla/parser/plugins/transfertypes/jsonnode/Nullable.java diff --git a/packages/java/parser-jvm-plugin-transfertypes/src/main/java/com/vaadin/hilla/parser/plugins/transfertypes/TransferTypesPlugin.java b/packages/java/parser-jvm-plugin-transfertypes/src/main/java/com/vaadin/hilla/parser/plugins/transfertypes/TransferTypesPlugin.java index 51bde133e6..628384c86e 100644 --- a/packages/java/parser-jvm-plugin-transfertypes/src/main/java/com/vaadin/hilla/parser/plugins/transfertypes/TransferTypesPlugin.java +++ b/packages/java/parser-jvm-plugin-transfertypes/src/main/java/com/vaadin/hilla/parser/plugins/transfertypes/TransferTypesPlugin.java @@ -10,6 +10,8 @@ import javax.annotation.Nonnull; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; import com.vaadin.hilla.mappedtypes.Order; import com.vaadin.hilla.mappedtypes.Pageable; import com.vaadin.hilla.mappedtypes.Sort; @@ -44,6 +46,8 @@ public final class TransferTypesPlugin classMap.put("com.vaadin.hilla.EndpointSubscription", EndpointSubscription.class); classMap.put(JsonNode.class.getName(), Object.class); + classMap.put(ObjectNode.class.getName(), Object.class); + classMap.put(ArrayNode.class.getName(), List.class); } public TransferTypesPlugin() { diff --git a/packages/java/parser-jvm-plugin-transfertypes/src/test/java/com/vaadin/hilla/parser/plugins/transfertypes/jsonnode/JsonNodeEndpoint.java b/packages/java/parser-jvm-plugin-transfertypes/src/test/java/com/vaadin/hilla/parser/plugins/transfertypes/jsonnode/JsonNodeEndpoint.java index 3d4149fcf8..f614adfd42 100644 --- a/packages/java/parser-jvm-plugin-transfertypes/src/test/java/com/vaadin/hilla/parser/plugins/transfertypes/jsonnode/JsonNodeEndpoint.java +++ b/packages/java/parser-jvm-plugin-transfertypes/src/test/java/com/vaadin/hilla/parser/plugins/transfertypes/jsonnode/JsonNodeEndpoint.java @@ -1,10 +1,20 @@ package com.vaadin.hilla.parser.plugins.transfertypes.jsonnode; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; @Endpoint public class JsonNodeEndpoint { - public JsonNode echo(JsonNode node) { + public JsonNode jsonNode(JsonNode node) { + return node; + } + + public ObjectNode objectNode(ObjectNode node) { + return node; + } + + public ArrayNode arrayNode(ArrayNode node) { return node; } } diff --git a/packages/java/parser-jvm-plugin-transfertypes/src/test/java/com/vaadin/hilla/parser/plugins/transfertypes/jsonnode/Nonnull.java b/packages/java/parser-jvm-plugin-transfertypes/src/test/java/com/vaadin/hilla/parser/plugins/transfertypes/jsonnode/Nonnull.java new file mode 100644 index 0000000000..006adf9022 --- /dev/null +++ b/packages/java/parser-jvm-plugin-transfertypes/src/test/java/com/vaadin/hilla/parser/plugins/transfertypes/jsonnode/Nonnull.java @@ -0,0 +1,11 @@ +package com.vaadin.hilla.parser.plugins.transfertypes.jsonnode; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE_USE }) +public @interface Nonnull { +} diff --git a/packages/java/parser-jvm-plugin-transfertypes/src/test/java/com/vaadin/hilla/parser/plugins/transfertypes/jsonnode/Nullable.java b/packages/java/parser-jvm-plugin-transfertypes/src/test/java/com/vaadin/hilla/parser/plugins/transfertypes/jsonnode/Nullable.java new file mode 100644 index 0000000000..efb7f9dcf7 --- /dev/null +++ b/packages/java/parser-jvm-plugin-transfertypes/src/test/java/com/vaadin/hilla/parser/plugins/transfertypes/jsonnode/Nullable.java @@ -0,0 +1,11 @@ +package com.vaadin.hilla.parser.plugins.transfertypes.jsonnode; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE_USE }) +public @interface Nullable { +} diff --git a/packages/java/parser-jvm-plugin-transfertypes/src/test/resources/com/vaadin/hilla/parser/plugins/transfertypes/jsonnode/openapi.json b/packages/java/parser-jvm-plugin-transfertypes/src/test/resources/com/vaadin/hilla/parser/plugins/transfertypes/jsonnode/openapi.json index 097592ea73..c8b8d5e1ca 100644 --- a/packages/java/parser-jvm-plugin-transfertypes/src/test/resources/com/vaadin/hilla/parser/plugins/transfertypes/jsonnode/openapi.json +++ b/packages/java/parser-jvm-plugin-transfertypes/src/test/resources/com/vaadin/hilla/parser/plugins/transfertypes/jsonnode/openapi.json @@ -17,12 +17,82 @@ } ], "paths" : { - "/JsonNodeEndpoint/echo" : { + "/JsonNodeEndpoint/arrayNode" : { "post" : { "tags" : [ "JsonNodeEndpoint" ], - "operationId" : "JsonNodeEndpoint_echo_POST", + "operationId" : "JsonNodeEndpoint_arrayNode_POST", + "requestBody" : { + "content" : { + "application/json" : { + "schema" : { + "type" : "object", + "properties" : { + "node" : { + "type" : "array", + "nullable" : true + } + } + } + } + } + }, + "responses" : { + "200" : { + "description" : "", + "content" : { + "application/json" : { + "schema" : { + "type" : "array", + "nullable" : true + } + } + } + } + } + } + }, + "/JsonNodeEndpoint/jsonNode" : { + "post" : { + "tags" : [ + "JsonNodeEndpoint" + ], + "operationId" : "JsonNodeEndpoint_jsonNode_POST", + "requestBody" : { + "content" : { + "application/json" : { + "schema" : { + "type" : "object", + "properties" : { + "node" : { + "type" : "object" + } + } + } + } + } + }, + "responses" : { + "200" : { + "description" : "", + "content" : { + "application/json" : { + "schema" : { + "type" : "object" + } + } + } + } + } + } + }, + "/JsonNodeEndpoint/objectNode" : { + "post" : { + "tags" : [ + "JsonNodeEndpoint" + ], + "operationId" : "JsonNodeEndpoint_objectNode_POST", "requestBody" : { "content" : { "application/json" : {