You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by GitBox <gi...@apache.org> on 2022/04/23 12:59:27 UTC

[GitHub] [unomi] sergehuber commented on a diff in pull request #407: UNOMI-562

sergehuber commented on code in PR #407:
URL: https://github.com/apache/unomi/pull/407#discussion_r856888724


##########
rest/src/main/java/org/apache/unomi/rest/deserializers/ContextRequestDeserializer.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.unomi.rest.deserializers;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import org.apache.unomi.api.ContextRequest;
+import org.apache.unomi.api.Event;
+import org.apache.unomi.api.Profile;
+import org.apache.unomi.api.services.PersonalizationService;
+import org.apache.unomi.api.services.SchemaRegistry;
+
+import javax.ws.rs.BadRequestException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Custom deserializer for ContextRequest that do validate the object using JSon Schema

Review Comment:
   Minor typo: should be JSON schema



##########
services/src/main/resources/META-INF/cxs/schemas/contextrequest.json:
##########
@@ -0,0 +1,64 @@
+{
+  "$id": "https://unomi.apache.org/schemas/json/contextrequest/1-0-0",
+  "$schema": "https://json-schema.org/draft/2019-09/schema",
+  "title": "ContextRequest",
+  "type": "object",
+  "properties": {
+    "source": {
+      "$ref": "https://unomi.apache.org/schemas/json/item/1-0-0"
+    },
+    "requireSegments": {
+      "type": ["null", "boolean"]
+    },
+    "requiredProfileProperties": {
+      "type": ["null", "array"],
+      "items": {
+        "type": "string"
+      }
+    },
+    "requiredSessionProperties": {
+      "type": ["null", "array"],
+      "items": {
+        "type": "string"
+      }
+    },
+    "requireScores": {
+      "type": ["null", "boolean"]
+    },
+    "events": {
+      "type": ["null", "array"],
+      "items": {
+        "$ref": "https://unomi.apache.org/schemas/json/event/1-0-0"
+      }
+    },
+    "filters": {
+      "type": ["null", "array"],
+      "items": {
+        "$ref": "https://unomi.apache.org/schemas/json/personalization/personalizedcontent/1-0-0"
+      }
+    },
+    "personalizations": {
+      "type": ["null", "array"],
+      "items": {
+        "$ref": "https://unomi.apache.org/schemas/json/event/1-0-0"

Review Comment:
   This looks wrong, shouldn't it be personalizedRequest instead ?



##########
api/src/main/java/org/apache/unomi/api/services/SchemaRegistry.java:
##########
@@ -51,7 +52,7 @@ public interface SchemaRegistry {
      * @param schemaId id of the schema used for the validation
      * @return true is the object is valid
      */
-    boolean isValid(Object object, String schemaId);
+    boolean isValid(JsonNode object, String schemaId);

Review Comment:
   Could we maybe rename the object to node and change the Javadoc ?



##########
rest/src/main/java/org/apache/unomi/rest/deserializers/ContextRequestDeserializer.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.unomi.rest.deserializers;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import org.apache.unomi.api.ContextRequest;
+import org.apache.unomi.api.Event;
+import org.apache.unomi.api.Profile;
+import org.apache.unomi.api.services.PersonalizationService;
+import org.apache.unomi.api.services.SchemaRegistry;
+
+import javax.ws.rs.BadRequestException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Custom deserializer for ContextRequest that do validate the object using JSon Schema
+ */
+public class ContextRequestDeserializer extends StdDeserializer<ContextRequest> {
+
+    private final SchemaRegistry schemaRegistry;
+
+    public ContextRequestDeserializer(SchemaRegistry schemaRegistry) {
+        this(null,  schemaRegistry);
+    }
+
+    public ContextRequestDeserializer(Class<ContextRequest> vc, SchemaRegistry schemaRegistry) {
+        super(vc);
+        this.schemaRegistry = schemaRegistry;
+    }
+
+    @Override
+    public ContextRequest deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException, JsonProcessingException {
+        JsonNode node = jsonParser.getCodec().readTree(jsonParser);
+        // Validate schema on it
+        if (schemaRegistry.isValid(node, "https://unomi.apache.org/schemas/json/contextrequest/1-0-0")) {
+            ContextRequest cr = new ContextRequest();
+            if (node.get("requireSegments") != null) {
+                cr.setRequireSegments(node.get("requireSegments").booleanValue());
+            }
+            final JsonNode requiredProfileProperties = node.get("requiredProfileProperties");
+            if (requiredProfileProperties instanceof ArrayNode) {
+                List<String> profileProperties = new ArrayList<>();
+                requiredProfileProperties.elements().forEachRemaining(el -> profileProperties.add(el.textValue()));
+                cr.setRequiredProfileProperties(profileProperties);
+            }
+            final JsonNode requiredSessionPropertiesNode = node.get("requiredSessionProperties");
+            if (requiredSessionPropertiesNode instanceof ArrayNode) {
+                List<String> requiredSessionProperties = new ArrayList<>();
+                requiredSessionPropertiesNode.elements().forEachRemaining(el -> requiredSessionProperties.add(el.textValue()));
+                cr.setRequiredProfileProperties(requiredSessionProperties);
+            }
+            if (node.get("requireScores") != null) {
+                cr.setRequireScores(node.get("requireScores").booleanValue());
+            }
+            final JsonNode eventsNode = node.get("events");
+            if (eventsNode instanceof ArrayNode) {
+                ArrayNode events = (ArrayNode) eventsNode;
+                List<Event> filteredEvents = new ArrayList<>();
+                for (JsonNode event : events) {
+                    if (schemaRegistry.isValid(event, "https://unomi.apache.org/schemas/json/events/" + event.get("eventType").textValue() + "/1-0-0")) {
+                        filteredEvents.add(jsonParser.getCodec().treeToValue(event, Event.class));
+                    }
+                }
+                cr.setEvents(filteredEvents);
+            }
+            final JsonNode filtersNode = node.get("filters");
+            if (filtersNode instanceof ArrayNode) {
+                ArrayNode filters = (ArrayNode) filtersNode;
+                List<PersonalizationService.PersonalizedContent> f = new ArrayList<>();
+                filters.elements().forEachRemaining(el -> {
+                    try {
+                        jsonParser.getCodec().treeToValue(el, PersonalizationService.PersonalizedContent.class);
+                    } catch (JsonProcessingException e) {
+                        // Unable to deserialize, ignore the entry
+                    }
+                });
+                cr.setFilters(f);
+            }
+            final JsonNode personalizationsNode = node.get("personalizations");
+            if (personalizationsNode instanceof ArrayNode) {
+                ArrayNode personalizations = (ArrayNode) personalizationsNode;
+                List<PersonalizationService.PersonalizationRequest> p = new ArrayList<>();
+                personalizations.elements().forEachRemaining(el -> {
+                    try {
+                        jsonParser.getCodec().treeToValue(el, PersonalizationService.PersonalizationRequest.class);

Review Comment:
   Same comment here ? p.add is missing ?



##########
rest/src/main/java/org/apache/unomi/rest/deserializers/ContextRequestDeserializer.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.unomi.rest.deserializers;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import org.apache.unomi.api.ContextRequest;
+import org.apache.unomi.api.Event;
+import org.apache.unomi.api.Profile;
+import org.apache.unomi.api.services.PersonalizationService;
+import org.apache.unomi.api.services.SchemaRegistry;
+
+import javax.ws.rs.BadRequestException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Custom deserializer for ContextRequest that do validate the object using JSon Schema
+ */
+public class ContextRequestDeserializer extends StdDeserializer<ContextRequest> {
+
+    private final SchemaRegistry schemaRegistry;
+
+    public ContextRequestDeserializer(SchemaRegistry schemaRegistry) {
+        this(null,  schemaRegistry);
+    }
+
+    public ContextRequestDeserializer(Class<ContextRequest> vc, SchemaRegistry schemaRegistry) {
+        super(vc);
+        this.schemaRegistry = schemaRegistry;
+    }
+
+    @Override
+    public ContextRequest deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException, JsonProcessingException {
+        JsonNode node = jsonParser.getCodec().readTree(jsonParser);
+        // Validate schema on it
+        if (schemaRegistry.isValid(node, "https://unomi.apache.org/schemas/json/contextrequest/1-0-0")) {
+            ContextRequest cr = new ContextRequest();
+            if (node.get("requireSegments") != null) {
+                cr.setRequireSegments(node.get("requireSegments").booleanValue());
+            }
+            final JsonNode requiredProfileProperties = node.get("requiredProfileProperties");
+            if (requiredProfileProperties instanceof ArrayNode) {
+                List<String> profileProperties = new ArrayList<>();
+                requiredProfileProperties.elements().forEachRemaining(el -> profileProperties.add(el.textValue()));
+                cr.setRequiredProfileProperties(profileProperties);
+            }
+            final JsonNode requiredSessionPropertiesNode = node.get("requiredSessionProperties");
+            if (requiredSessionPropertiesNode instanceof ArrayNode) {
+                List<String> requiredSessionProperties = new ArrayList<>();
+                requiredSessionPropertiesNode.elements().forEachRemaining(el -> requiredSessionProperties.add(el.textValue()));
+                cr.setRequiredProfileProperties(requiredSessionProperties);
+            }
+            if (node.get("requireScores") != null) {
+                cr.setRequireScores(node.get("requireScores").booleanValue());
+            }
+            final JsonNode eventsNode = node.get("events");
+            if (eventsNode instanceof ArrayNode) {
+                ArrayNode events = (ArrayNode) eventsNode;
+                List<Event> filteredEvents = new ArrayList<>();
+                for (JsonNode event : events) {
+                    if (schemaRegistry.isValid(event, "https://unomi.apache.org/schemas/json/events/" + event.get("eventType").textValue() + "/1-0-0")) {
+                        filteredEvents.add(jsonParser.getCodec().treeToValue(event, Event.class));
+                    }
+                }
+                cr.setEvents(filteredEvents);
+            }
+            final JsonNode filtersNode = node.get("filters");
+            if (filtersNode instanceof ArrayNode) {
+                ArrayNode filters = (ArrayNode) filtersNode;
+                List<PersonalizationService.PersonalizedContent> f = new ArrayList<>();
+                filters.elements().forEachRemaining(el -> {
+                    try {
+                        jsonParser.getCodec().treeToValue(el, PersonalizationService.PersonalizedContent.class);
+                    } catch (JsonProcessingException e) {
+                        // Unable to deserialize, ignore the entry
+                    }
+                });
+                cr.setFilters(f);
+            }
+            final JsonNode personalizationsNode = node.get("personalizations");
+            if (personalizationsNode instanceof ArrayNode) {
+                ArrayNode personalizations = (ArrayNode) personalizationsNode;
+                List<PersonalizationService.PersonalizationRequest> p = new ArrayList<>();
+                personalizations.elements().forEachRemaining(el -> {
+                    try {
+                        jsonParser.getCodec().treeToValue(el, PersonalizationService.PersonalizationRequest.class);
+                    } catch (JsonProcessingException e) {
+                        // Unable to deserialize, ignore the entry
+                    }
+                });
+                cr.setPersonalizations(p);
+            }
+            if (node.get("profileOverrides") != null) {
+                cr.setProfileOverrides(jsonParser.getCodec().treeToValue(node.get("profileOverrides"), Profile.class));
+            }
+            if (node.get("sessionPropertiesOverrides") != null) {
+                jsonParser.getCodec().treeToValue(node.get("sessionPropertiesOverrides"), Map.class);

Review Comment:
   Also here it seems we are not assigning the value



##########
rest/src/main/java/org/apache/unomi/rest/deserializers/ContextRequestDeserializer.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.unomi.rest.deserializers;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import org.apache.unomi.api.ContextRequest;
+import org.apache.unomi.api.Event;
+import org.apache.unomi.api.Profile;
+import org.apache.unomi.api.services.PersonalizationService;
+import org.apache.unomi.api.services.SchemaRegistry;
+
+import javax.ws.rs.BadRequestException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Custom deserializer for ContextRequest that do validate the object using JSon Schema
+ */
+public class ContextRequestDeserializer extends StdDeserializer<ContextRequest> {
+
+    private final SchemaRegistry schemaRegistry;
+
+    public ContextRequestDeserializer(SchemaRegistry schemaRegistry) {
+        this(null,  schemaRegistry);
+    }
+
+    public ContextRequestDeserializer(Class<ContextRequest> vc, SchemaRegistry schemaRegistry) {
+        super(vc);
+        this.schemaRegistry = schemaRegistry;
+    }
+
+    @Override
+    public ContextRequest deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException, JsonProcessingException {
+        JsonNode node = jsonParser.getCodec().readTree(jsonParser);
+        // Validate schema on it
+        if (schemaRegistry.isValid(node, "https://unomi.apache.org/schemas/json/contextrequest/1-0-0")) {
+            ContextRequest cr = new ContextRequest();
+            if (node.get("requireSegments") != null) {
+                cr.setRequireSegments(node.get("requireSegments").booleanValue());
+            }
+            final JsonNode requiredProfileProperties = node.get("requiredProfileProperties");
+            if (requiredProfileProperties instanceof ArrayNode) {
+                List<String> profileProperties = new ArrayList<>();
+                requiredProfileProperties.elements().forEachRemaining(el -> profileProperties.add(el.textValue()));
+                cr.setRequiredProfileProperties(profileProperties);
+            }
+            final JsonNode requiredSessionPropertiesNode = node.get("requiredSessionProperties");
+            if (requiredSessionPropertiesNode instanceof ArrayNode) {
+                List<String> requiredSessionProperties = new ArrayList<>();
+                requiredSessionPropertiesNode.elements().forEachRemaining(el -> requiredSessionProperties.add(el.textValue()));
+                cr.setRequiredProfileProperties(requiredSessionProperties);
+            }
+            if (node.get("requireScores") != null) {
+                cr.setRequireScores(node.get("requireScores").booleanValue());
+            }
+            final JsonNode eventsNode = node.get("events");
+            if (eventsNode instanceof ArrayNode) {
+                ArrayNode events = (ArrayNode) eventsNode;
+                List<Event> filteredEvents = new ArrayList<>();
+                for (JsonNode event : events) {
+                    if (schemaRegistry.isValid(event, "https://unomi.apache.org/schemas/json/events/" + event.get("eventType").textValue() + "/1-0-0")) {
+                        filteredEvents.add(jsonParser.getCodec().treeToValue(event, Event.class));
+                    }
+                }
+                cr.setEvents(filteredEvents);
+            }
+            final JsonNode filtersNode = node.get("filters");
+            if (filtersNode instanceof ArrayNode) {
+                ArrayNode filters = (ArrayNode) filtersNode;
+                List<PersonalizationService.PersonalizedContent> f = new ArrayList<>();
+                filters.elements().forEachRemaining(el -> {
+                    try {
+                        jsonParser.getCodec().treeToValue(el, PersonalizationService.PersonalizedContent.class);

Review Comment:
   Are we not missing f.add here ?



##########
services/src/main/resources/META-INF/cxs/schemas/contextrequest.json:
##########
@@ -0,0 +1,64 @@
+{
+  "$id": "https://unomi.apache.org/schemas/json/contextrequest/1-0-0",
+  "$schema": "https://json-schema.org/draft/2019-09/schema",
+  "title": "ContextRequest",
+  "type": "object",
+  "properties": {
+    "source": {
+      "$ref": "https://unomi.apache.org/schemas/json/item/1-0-0"
+    },
+    "requireSegments": {
+      "type": ["null", "boolean"]
+    },
+    "requiredProfileProperties": {
+      "type": ["null", "array"],
+      "items": {
+        "type": "string"
+      }
+    },
+    "requiredSessionProperties": {
+      "type": ["null", "array"],
+      "items": {
+        "type": "string"
+      }
+    },
+    "requireScores": {
+      "type": ["null", "boolean"]
+    },
+    "events": {
+      "type": ["null", "array"],
+      "items": {
+        "$ref": "https://unomi.apache.org/schemas/json/event/1-0-0"
+      }
+    },
+    "filters": {
+      "type": ["null", "array"],
+      "items": {
+        "$ref": "https://unomi.apache.org/schemas/json/personalization/personalizedcontent/1-0-0"
+      }
+    },
+    "personalizations": {
+      "type": ["null", "array"],
+      "items": {
+        "$ref": "https://unomi.apache.org/schemas/json/event/1-0-0"
+      }
+    },
+    "profileOverrides": {
+      "$ref": "https://unomi.apache.org/schemas/json/profile/1-0-0"
+    },
+    "sessionPropertiesOverrides": {
+      "type": ["null", "object"]

Review Comment:
   We might need to set a maximum here like : maxProperties : 50



##########
services/src/main/java/org/apache/unomi/services/impl/events/EventServiceImpl.java:
##########
@@ -139,7 +141,8 @@ public boolean isEventAllowed(Event event, String thirdPartyId) {
     }
 
     public boolean isEventValid(Event event) {
-        return schemaRegistry.isValid(event, "https://unomi.apache.org/schemas/json/events/" + event.getEventType() + "/1-0-0");
+

Review Comment:
   Does this method still make sense to keep if all validation was done at the endpoint level ?



-- 
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-unsubscribe@unomi.apache.org

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