poorbarcode commented on code in PR #16577:
URL: https://github.com/apache/pulsar/pull/16577#discussion_r922938965


##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/web/DynamicSkipUnknownPropertyHandler.java:
##########
@@ -0,0 +1,62 @@
+/**
+ * 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.pulsar.broker.web;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonDeserializer;
+import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
+import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
+import java.io.IOException;
+import java.util.Collection;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class DynamicSkipUnknownPropertyHandler extends 
DeserializationProblemHandler {
+
+    @Getter
+    @Setter
+    private boolean skipUnknownProperty = true;
+
+    @Override
+    public boolean handleUnknownProperty(DeserializationContext 
deserializationContext, JsonParser p,
+                                         JsonDeserializer<?> deserializer, 
Object beanOrClass,
+                                         String propertyName) throws 
IOException {
+        if (skipUnknownProperty){
+            StringBuilder warnLog = new StringBuilder();
+            warnLog.append("Deserialize json to 
[").append(beanOrClass.getClass().getName())
+                    .append("], found unknown property 
[").append(propertyName).append("] and skipped. ");
+            if (p.isExpectedStartArrayToken()){
+                warnLog.append("The requested value is an array.");
+            } else if (p.isExpectedStartObjectToken()){
+                warnLog.append("The requested value is an object.");
+            } else {
+                warnLog.append("The requested value is 
[").append(p.getText()).append("].");
+            }
+            log.warn(warnLog.toString());
+            p.skipChildren();
+            return skipUnknownProperty;
+        } else {
+            Collection<Object> propIds = (deserializer == null) ? null : 
deserializer.getKnownPropertyNames();

Review Comment:
   > I'm not sure if the Jetty will print the detailed logs.
   
   There should be no warning logs by default, but there is a detailed 
explanation in response. 
   see 
[JsonMappingExceptionMapper](https://github.com/javaee/jersey/blob/master/media/json-jackson/src/main/java/org/glassfish/jersey/jackson/internal/jackson/jaxrs/base/JsonMappingExceptionMapper.java).
 *And I will rewrite the response content in the next PR*.
   
   The default implementation of error response looks like this:
   
   ```
   Unrecognized field "retention_time_in_minutes" (class 
org.apache.pulsar.common.policies.data.RetentionPolicies), not marked as 
ignorable (2 known properties: "retentionSizeInMB", "retentionTimeInMinutes"])
    at [Source: 
(org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream);
 line: 1, column: 35] (through reference chain: 
org.apache.pulsar.common.policies.data.RetentionPolicies["retention_time_in_minutes"])
   ```
   
   I plan to change it to this(Next PR):
   
   ```
   Unknown property "retention_time_in_minutes", perhaps you want to use one of 
these: ["retentionSizeInMB", "retentionTimeInMinutes"]
   ```
   
   > Instead, we should have a detailed error log here? If Jetty can tell us 
which property is the unknown property, I think we don't need error logs here.
   
   On the above, warn logs should not be needed.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to