dsmiley commented on code in PR #4640:
URL: https://github.com/apache/solr/pull/4640#discussion_r3704065183


##########
solr/solrj/src/java/org/apache/solr/common/util/ResponseNormalizer.java:
##########
@@ -0,0 +1,123 @@
+/*
+ * 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.solr.common.util;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import org.apache.solr.common.SolrDocument;
+import org.apache.solr.common.SolrDocumentList;
+
+/**
+ * Converts a parsed response into the canonical shape the SolrJ response 
classes expect (the shape
+ * the binary and XML parsers produce): nested JSON objects become {@link 
NamedList}s and a {@code
+ * {numFound, docs}} object becomes a {@link SolrDocumentList}.
+ *
+ * <p>Only unambiguous, self-describing conversions are performed. It is a 
no-op for values already
+ * in canonical form (so binary/XML responses pass through unchanged). It does 
not attempt to
+ * interpret the ambiguous flat arrays produced by {@code json.nl=flat}; a 
typed JSON parser should
+ * request {@code json.nl=map} for its own reads.
+ */
+public final class ResponseNormalizer {
+
+  private ResponseNormalizer() {}
+
+  /** Returns a normalized copy of the given response NamedList. */
+  public static NamedList<Object> normalize(NamedList<Object> response) {
+    if (response == null) {
+      return null;
+    }
+    SimpleOrderedMap<Object> out = new SimpleOrderedMap<>(response.size());
+    for (Map.Entry<String, Object> e : response) {
+      out.add(e.getKey(), normalizeValue(e.getValue()));
+    }
+    return out;
+  }
+
+  @SuppressWarnings("unchecked")
+  private static Object normalizeValue(Object val) {
+    if (val instanceof SolrDocumentList || val instanceof SolrDocument) {
+      // Already canonical (binary/XML produce these directly); leave 
untouched. Must precede the
+      // List/Map branches since SolrDocumentList is a List and SolrDocument 
is a Map.
+      return val;
+    } else if (val instanceof NamedList) {

Review Comment:
   >Two things I checked ...
   
   Yes this is by design.  It's why SOM exists.  It's for efficient response 
formulation -- no safety check, and conveys a Map and thus it renders different 
with JSON.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to