Copilot commented on code in PR #4747:
URL: https://github.com/apache/arrow-adbc/pull/4747#discussion_r4010968132


##########
java/driver/flight-sql/src/main/java/org/apache/arrow/adbc/driver/flightsql/FlightSqlSessionUtil.java:
##########
@@ -0,0 +1,295 @@
+/*
+ * 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.arrow.adbc.driver.flightsql;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import org.apache.arrow.adbc.core.AdbcException;
+import org.apache.arrow.adbc.core.AdbcStatusCode;
+import org.apache.arrow.adbc.core.TypedKey;
+import org.apache.arrow.flight.NoOpSessionOptionValueVisitor;
+import org.apache.arrow.flight.SessionOptionValue;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+/** Package-private helpers for Flight SQL session option serialization and 
type conversion. */
+final class FlightSqlSessionUtil {
+
+  static final ObjectMapper MAPPER = new ObjectMapper();
+
+  /**
+   * Extracts the native Java value from a {@link SessionOptionValue}. String 
arrays are defensively
+   * cloned; Void returns {@code null} (callers must handle null before 
calling {@link #cast}).
+   */
+  static final NoOpSessionOptionValueVisitor<Object> TO_JAVA =
+      new NoOpSessionOptionValueVisitor<Object>() {
+        @Override
+        public Object visit(String v) {
+          return v;
+        }
+
+        @Override
+        public Object visit(boolean v) {
+          return v;
+        }
+
+        @Override
+        public Object visit(long v) {
+          return v;
+        }
+
+        @Override
+        public Object visit(double v) {
+          return v;
+        }
+
+        @Override
+        public Object visit(String[] v) {
+          return v.clone();
+        }
+      };
+
+  /**
+   * Extracts a JSON-safe Java value from a {@link SessionOptionValue}. JSON 
has no representation
+   * for NaN/Infinity, so non-finite doubles are represented by their {@link 
String} form (for
+   * example {@code "NaN"}).
+   */
+  static final NoOpSessionOptionValueVisitor<Object> TO_JSON_JAVA =
+      new NoOpSessionOptionValueVisitor<Object>() {
+        @Override
+        public Object visit(String v) {
+          return v;
+        }
+
+        @Override
+        public Object visit(boolean v) {
+          return v;
+        }
+
+        @Override
+        public Object visit(long v) {
+          return v;
+        }
+
+        @Override
+        public Object visit(double v) {
+          return Double.isFinite(v) ? (Object) v : Double.toString(v);
+        }
+
+        @Override
+        public Object visit(String[] v) {
+          return v.clone();
+        }
+      };
+
+  /** Serializes all session options to a JSON object string. */
+  static String toJson(Map<String, SessionOptionValue> opts) throws 
AdbcException {
+    Map<String, @Nullable Object> map = new LinkedHashMap<>();
+    for (Map.Entry<String, SessionOptionValue> e : opts.entrySet()) {
+      map.put(e.getKey(), e.getValue().acceptVisitor(TO_JSON_JAVA));
+    }
+    try {
+      return MAPPER.writeValueAsString(map);
+    } catch (JsonProcessingException e) {
+      throw AdbcException.internal("[Flight SQL] Failed to serialize session 
options").withCause(e);
+    }
+  }
+
+  /** Parses a strict JSON string array (used when a string-list option is 
supplied as JSON). */
+  static String[] parseJsonArray(String json) throws AdbcException {
+    try {
+      final JsonNode root = MAPPER.readTree(json);

Review Comment:
   `ObjectMapper.readTree` in Jackson 2.x does not reject trailing tokens 
unless `FAIL_ON_TRAILING_TOKENS` is enabled, so inputs such as `["a"] []` are 
accepted even though this helper promises a strict JSON array. Parse with a 
reader that enables trailing-token validation so malformed option values cannot 
be silently truncated.



-- 
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