isaric commented on code in PR #5311: URL: https://github.com/apache/solr/pull/5311#discussion_r4526445451
########## solr/solrj/src/test/org/apache/solr/client/solrj/request/schema/SchemaRequestBodyTest.java: ########## @@ -0,0 +1,109 @@ +/* + * 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.request.schema; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import org.apache.solr.SolrTestCase; +import org.junit.Test; + +/** Pins the JSON bodies that {@link SchemaRequest.Update} requests send to the Schema API. */ +public class SchemaRequestBodyTest extends SolrTestCase { + + @Test + public void testAddFieldTypeBody() throws IOException { + FieldTypeDefinition fieldType = new FieldTypeDefinition(); + fieldType.setAttributes(map("name", "text_test", "class", "solr.TextField")); + AnalyzerDefinition analyzer = new AnalyzerDefinition(); + analyzer.setAttributes(map("type", "index")); + analyzer.setCharFilters(List.of(map("class", "solr.HTMLStripCharFilterFactory"))); + analyzer.setTokenizer(map("class", "solr.StandardTokenizerFactory")); + analyzer.setFilters( + List.of( + map("class", "solr.LowerCaseFilterFactory"), + map("class", "solr.ASCIIFoldingFilterFactory"))); + fieldType.setIndexAnalyzer(analyzer); + fieldType.setSimilarity(map("class", "solr.BM25SimilarityFactory", "k1", 1.2)); + + assertJSONEquals( + """ + { + "add-field-type": { + "name": "text_test", + "class": "solr.TextField", + "indexAnalyzer": { + "type": "index", + "charFilters": [{"class": "solr.HTMLStripCharFilterFactory"}], + "tokenizer": {"class": "solr.StandardTokenizerFactory"}, + "filters": [ + {"class": "solr.LowerCaseFilterFactory"}, + {"class": "solr.ASCIIFoldingFilterFactory"} + ] + }, + "similarity": {"class": "solr.BM25SimilarityFactory", "k1": 1.2} + } + } + """, + body(new SchemaRequest.AddFieldType(fieldType))); + } + + @Test + public void testAddFieldBody() throws IOException { + assertJSONEquals( + """ + {"add-field": {"name": "f1", "type": "string", "stored": true}} + """, + body(new SchemaRequest.AddField(map("name", "f1", "type", "string", "stored", true)))); + } + + @Test + public void testMultiUpdateRepeatsCommandKeys() throws IOException { + SchemaRequest.MultiUpdate multiUpdate = + new SchemaRequest.MultiUpdate( + List.of( + new SchemaRequest.AddField(map("name", "f1", "type", "string")), + new SchemaRequest.AddField(map("name", "f2", "type", "string")), + new SchemaRequest.DeleteField("f0"))); + + // The Schema API takes several commands of the same kind in one object, so the body must keep + // the repeated key. Compared as text: a JSON parser would collapse the duplicate. + assertEquals( + """ + {"add-field":{"name":"f1","type":"string"},\ + "add-field":{"name":"f2","type":"string"},\ Review Comment: Not a bug, and not new in this PR. MultiUpdate.getRequestParameters is untouched here and has always concatenated the sub-requests into one NamedList, so the repeated key was always on the wire; this test only pins it. The server reads the body with CommandOperation.parse, which walks the object with a streaming JSONParser and emits one CommandOperation per key occurrence rather than building a map, so both commands run. The ref guide documents this form under "Multiple Commands in a Single POST" (two `"add-field"` keys in one body, with the list form as the alternative), and TestBulkSchemaAPI.testMultipleCommands exercises it end to end; I also confirmed it with SchemaTest.testMultipleUpdateRequestAccuracy, which sends a MultiUpdate with two add-field commands and finds both fields afterwards. I've extended the test's comment to point at the ref guide section. ########## changelog/unreleased/SOLR-18612-solrj-simpleorderedmap.yml: ########## @@ -0,0 +1,14 @@ +# See https://github.com/apache/solr/blob/main/dev-docs/changelog.adoc + +title: > + SolrJ internals: where SolrJ builds a NamedList whose keys never repeat (Schema API request bodies, Review Comment: Dropped. -- 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]
