rmannibucau commented on code in PR #100:
URL: https://github.com/apache/johnzon/pull/100#discussion_r1170893016


##########
src/site/markdown/index.md:
##########
@@ -512,7 +512,9 @@ This module provides some extension to JSON-B.
 
 #### Polymorphism
 
-This extension provides a way to handle polymorphism:
+This extension shouldn't be used anymore if you don't absolutely rely on the 
JSON format it generates/parses.

Review Comment:
   What about "If your aim is only to be polymorphic you can use the built-in 
JSON-B feature but some cases remain unhandled and this extension enable them 
(mainly JSON-B to JSON-B cases like configurations and service to service calls 
but unlikely javascript to java cases)".



##########
johnzon-mapper/src/main/java/org/apache/johnzon/mapper/Mappings.java:
##########
@@ -18,11 +18,16 @@
  */
 package org.apache.johnzon.mapper;
 
-import static java.util.Arrays.asList;

Review Comment:
   oops (autoformat)?



##########
johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JsonbPolymorphismHandler.java:
##########
@@ -0,0 +1,191 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.jsonb;
+
+import org.apache.johnzon.mapper.access.Meta;
+
+import jakarta.json.JsonObject;
+import jakarta.json.JsonString;
+import jakarta.json.JsonValue;
+import jakarta.json.bind.JsonbException;
+import jakarta.json.bind.annotation.JsonbSubtype;
+import jakarta.json.bind.annotation.JsonbTypeInfo;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class JsonbPolymorphismHandler {
+    public boolean hasPolymorphism(Class<?> clazz) {
+        return clazz.isAnnotationPresent(JsonbTypeInfo.class) || 
getParentWithTypeInfo(clazz) != null;
+    }
+
+    public Map.Entry<String, String>[] 
getPolymorphismPropertiesToSerialize(Class<?> clazz, Collection<String> 
otherProperties) {
+        List<Map.Entry<String, String>> result = new ArrayList<>();
+
+        Class<?> current = clazz;
+        while (current != null) {
+            // Only try to resolve types when there's a JsonbTypeInfo 
Annotation present on the current type, Meta.getAnnotation tries to
+            // walk up parents by itself until it finds the given Annotation 
and could incorrectly cause JsonbExceptions to be thrown
+            // (multiple JsonbTypeInfos with same key found even if thats not 
actually the case)
+            if (current.isAnnotationPresent(JsonbTypeInfo.class)) {
+                JsonbTypeInfo typeInfo = Meta.getAnnotation(current, 
JsonbTypeInfo.class);
+                if (otherProperties.contains(typeInfo.key())) {
+                    throw new JsonbException("JsonbTypeInfo key '" + 
typeInfo.key() + "' collides with other properties in json");
+                }
+
+                String bestMatchingAlias = null;
+                for (JsonbSubtype subtype : typeInfo.value()) {
+                    if (subtype.type().isAssignableFrom(clazz)) {
+                        bestMatchingAlias = subtype.alias();
+
+                        if (clazz == subtype.type()) { // Exact match found, 
no need to continue further
+                            break;
+                        }
+                    }
+                }
+
+                if (bestMatchingAlias != null) {
+                    result.add(0, Map.entry(typeInfo.key(), 
bestMatchingAlias));
+                }
+            }
+
+            current = getParentWithTypeInfo(current);
+        }
+
+        return result.toArray(Map.Entry[]::new);
+    }
+
+    public Class<?> getTypeToDeserialize(JsonObject jsonObject, Class<?> 
clazz) {

Review Comment:
   implemented like that this must *not* be called at runtime, overall consider 
any reflection is forbidden at runtime except to discover the passed instance 
type so this impl should be limited to (pseudo code) 
`typeInfos.get(jsonObject.getString("..."))` so the reflection should have been 
stored in the handler earlier (likely in validate or 
getPolymorphismPropertiesToSerialize to avoid to do it > once) and the lookup 
shouldnt be a loop but a map access



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@johnzon.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to