serhiy-bzhezytskyy commented on code in PR #4640: URL: https://github.com/apache/solr/pull/4640#discussion_r3699950183
########## solr/solrj/src/test/org/apache/solr/client/solrj/response/QueryResponseJsonParserIntegrationTest.java: ########## @@ -0,0 +1,95 @@ +/* + * 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.client.solrj.response; + +import static org.apache.solr.SolrTestCaseJ4.sdoc; + +import org.apache.solr.SolrTestCase; +import org.apache.solr.client.solrj.SolrClient; +import org.apache.solr.client.solrj.request.SolrQuery; +import org.apache.solr.client.solrj.response.json.JsonMapResponseParser; +import org.apache.solr.util.ExternalPaths; +import org.apache.solr.util.SolrJettyTestRule; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; + +/** + * End-to-end: a real HTTP query with the JSON map response parser must return a fully typed + * QueryResponse, proving SolrRequest.process() normalizes the non-canonical JSON response at the + * boundary before the response classes read it (SOLR-17316). + */ +public class QueryResponseJsonParserIntegrationTest extends SolrTestCase { + + @ClassRule public static SolrJettyTestRule solrTestRule = new SolrJettyTestRule(); + + @BeforeClass + public static void beforeClass() throws Exception { + System.setProperty("solr.security.allow.paths", "*"); + solrTestRule.startSolr(); + solrTestRule.newCollection().withConfigSet(ExternalPaths.TECHPRODUCTS_CONFIGSET).create(); + + try (SolrClient client = solrTestRule.getSolrClient()) { + client.add( + java.util.List.of( Review Comment: Fixed — `java.util.List` imported. ########## solr/solrj/src/test/org/apache/solr/client/solrj/response/QueryResponseJsonParserIntegrationTest.java: ########## @@ -0,0 +1,95 @@ +/* + * 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.client.solrj.response; + +import static org.apache.solr.SolrTestCaseJ4.sdoc; + +import org.apache.solr.SolrTestCase; +import org.apache.solr.client.solrj.SolrClient; +import org.apache.solr.client.solrj.request.SolrQuery; +import org.apache.solr.client.solrj.response.json.JsonMapResponseParser; +import org.apache.solr.util.ExternalPaths; +import org.apache.solr.util.SolrJettyTestRule; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; + +/** + * End-to-end: a real HTTP query with the JSON map response parser must return a fully typed + * QueryResponse, proving SolrRequest.process() normalizes the non-canonical JSON response at the + * boundary before the response classes read it (SOLR-17316). + */ +public class QueryResponseJsonParserIntegrationTest extends SolrTestCase { + + @ClassRule public static SolrJettyTestRule solrTestRule = new SolrJettyTestRule(); + + @BeforeClass + public static void beforeClass() throws Exception { + System.setProperty("solr.security.allow.paths", "*"); + solrTestRule.startSolr(); + solrTestRule.newCollection().withConfigSet(ExternalPaths.TECHPRODUCTS_CONFIGSET).create(); + + try (SolrClient client = solrTestRule.getSolrClient()) { Review Comment: Fixed. The javadoc on `getSolrClient(String)` says it outright — "The caller doesn't need to close it" — so the try-with-resources is gone. ########## solr/solrj/src/test/org/apache/solr/client/solrj/response/QueryResponseSectionParityTest.java: ########## @@ -0,0 +1,162 @@ +/* + * 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.client.solrj.response; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import org.apache.solr.SolrTestCase; +import org.apache.solr.client.solrj.response.json.JsonMapResponseParser; +import org.apache.solr.common.util.NamedList; +import org.apache.solr.common.util.ResponseNormalizer; +import org.junit.Test; + +/** + * Each test feeds a JSON (json.nl=map) response for one QueryResponse section through the + * normalizer and asserts the typed accessor works. Sections with a numeric cast (grouping, facets, + * spellcheck) also guard the Number widening; the rest guard the structural Map -> NamedList / + * SolrDocumentList reconstruction the section relies on. + */ +public class QueryResponseSectionParityTest extends SolrTestCase { + + private static QueryResponse parse(String json) throws Exception { + NamedList<Object> parsed = + new JsonMapResponseParser() + .processResponse( + new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)), "UTF-8"); + QueryResponse r = new QueryResponse(); + r.setResponse(ResponseNormalizer.normalize(parsed)); + return r; + } + + private static final String HEADER = "\"responseHeader\":{\"status\":0,\"QTime\":1},"; + + /** pivot facets: count is an Integer cast (QueryResponse readPivots). */ + @Test + public void testPivotFacets() throws Exception { + String json = + "{" Review Comment: Fixed — all eight JSON literals are text blocks now, which also removed every escaped quote in the file. ########## solr/solrj/src/java/org/apache/solr/client/solrj/response/ResponseNormalizer.java: ########## Review Comment: Moved to `org.apache.solr.client.solrj.response`, along with its test. ########## 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) { + // Already canonical (binary/XML), but its children may still need normalizing. + NamedList<Object> in = (NamedList<Object>) val; + SimpleOrderedMap<Object> out = new SimpleOrderedMap<>(in.size()); + for (Map.Entry<String, Object> e : in) { + out.add(e.getKey(), normalizeValue(e.getValue())); + } + return out; + } else if (val instanceof Map) { + Map<String, Object> m = (Map<String, Object>) val; Review Comment: Fixed throughout `ResponseNormalizer` — `instanceof NamedList<?> in`, `instanceof Map<?,?> raw`, `instanceof List<?> in`. -- 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]
