dsmiley commented on code in PR #4640: URL: https://github.com/apache/solr/pull/4640#discussion_r3696530420
########## 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", "*"); Review Comment: is this necessary? FYI I'm hoping for this to go away basically... there's a JIRA and a PR albeit presently it's stalled: https://github.com/apache/solr/pull/4215#issuecomment-4329198060 ########## solr/solrj/src/test/org/apache/solr/client/solrj/response/AdminResponseNumericTypeTest.java: ########## Review Comment: TBH I'd rather see an integration test, showing something working realisitcally that previously didn't. I'm not a fan of unit tests like this. I can't tell yet as I'm coming to understand this PR still but it's possible there's existting tests that just need to randomly pick the "wt". We use randomization a lot, and it can reduce the testing maintenance & increase net tested exposure IMO. ########## solr/solrj/src/java/org/apache/solr/common/util/ResponseNormalizer.java: ########## Review Comment: IMO this goes in the org.apache.solr.client.solrj.response package. It's not a common utility. ########## 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: We should not (on principle) replace every NamedList with a SimpleOrderedMap. Some NL's are a SOM; some are not. A SOM conveys unique keys that may not be true with a general NL. ########## 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: I believe we have linting rules prohibiting FQNs ########## 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: We're using Java 17 here; please use multi-line strings ########## 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: Doesn't need to be closed; the rule manages that. ########## solr/solrj/src/java/org/apache/solr/client/solrj/impl/HttpSolrClient.java: ########## @@ -229,6 +230,9 @@ protected NamedList<Object> processErrorsAndResponse( NamedList<Object> rsp; try { rsp = processor.processResponse(is, encoding); + if (!processor.producesCanonicalForm()) { Review Comment: hmm; this seems bolted on. Could we instead have processor.processCanonicalResponse that internally has this similar logic? This puts the processor in charge. No `processor.producesCanonicalForm` predicate would be needed. ########## 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: We're using Java 17 which supports cast-less instanceof checks. Broadly your PR would benefit from updated Java. -- 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]
