SOLR-8392 type safety on SolrParam (#1556)

This commit is contained in:
Mike Drob 2020-06-11 14:21:54 -05:00 committed by GitHub
parent 75d25ad677
commit fb98f30a61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 49 deletions

View File

@ -29,7 +29,7 @@ import org.apache.solr.cloud.ZkSolrResourceLoader;
import org.apache.solr.common.MapSerializable;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.SolrZkClient;
import org.apache.solr.common.params.MapSolrParams;
import org.apache.solr.common.params.MultiMapSolrParams;
import org.apache.solr.common.util.Utils;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.data.Stat;
@ -85,32 +85,27 @@ public class RequestParams implements MapSerializable {
* This converts Lists to arrays of strings. Because Solr expects
* params to be String[]
*/
@SuppressWarnings({"unchecked", "rawtypes"})
private static Map getMapCopy(Map value) {
@SuppressWarnings({"rawtypes"})
Map copy = new LinkedHashMap<>();
for (Object o1 : value.entrySet()) {
@SuppressWarnings({"rawtypes"})
Map.Entry entry = (Map.Entry) o1;
private static Map<String,String[]> getMapCopy(Map<String,?> value) {
Map<String, String[]> copy = new LinkedHashMap<>();
for (Map.Entry<String, ?> entry : value.entrySet()) {
if ("".equals(entry.getKey())) {
copy.put(entry.getKey(), entry.getValue());
continue;
}
if (entry.getValue() != null) {
if (entry.getValue() instanceof List) {
@SuppressWarnings({"rawtypes"})
List l = (List) entry.getValue();
String[] sarr = new String[l.size()];
for (int i = 0; i < l.size(); i++) {
if (l.get(i) != null) sarr[i] = String.valueOf(l.get(i));
}
copy.put(entry.getKey(), sarr);
// Why is this a special case?
if (entry.getValue() instanceof String[]) {
copy.put("", (String[]) entry.getValue());
} else {
copy.put(entry.getKey(), String.valueOf(entry.getValue()));
throw new IllegalArgumentException();
}
} else if (entry.getValue() == null) {
copy.put(entry.getKey(), null);
} else if (entry.getValue() instanceof List) {
List<?> l = (List<?>) entry.getValue();
String[] sarr = new String[l.size()];
for (int i = 0; i < l.size(); i++) {
if (l.get(i) != null) sarr[i] = String.valueOf(l.get(i));
}
copy.put(entry.getKey(), sarr);
} else {
copy.put(entry.getKey(), entry.getValue());
copy.put(entry.getKey(), new String[] { entry.getValue().toString() });
}
}
return copy;
@ -287,11 +282,10 @@ public class RequestParams implements MapSerializable {
}
}
public static class VersionedParams extends MapSolrParams {
public static class VersionedParams extends MultiMapSolrParams {
final ParamSet paramSet;
@SuppressWarnings({"unchecked", "rawtypes"})
public VersionedParams(Map map, ParamSet paramSet) {
public VersionedParams(Map<String,?> map, ParamSet paramSet) {
super(getMapCopy(map));
this.paramSet = paramSet;
}

View File

@ -139,11 +139,7 @@ public class SchemaHandler extends RequestHandlerBase implements SolrCoreAware,
break;
}
case "/schema/zkversion": {
int refreshIfBelowVersion = -1;
Object refreshParam = req.getParams().get("refreshIfBelowVersion");
if (refreshParam != null)
refreshIfBelowVersion = (refreshParam instanceof Number) ? ((Number) refreshParam).intValue()
: Integer.parseInt(refreshParam.toString());
int refreshIfBelowVersion = req.getParams().getInt("refreshIfBelowVersion");
int zkVersion = -1;
IndexSchema schema = req.getSchema();
if (schema instanceof ManagedIndexSchema) {

View File

@ -125,12 +125,9 @@ public abstract class BaseSolrResource extends ServerResource {
SolrCore.preDecorateResponse(solrRequest, solrResponse);
// client application can set a timeout for update requests
Object updateTimeoutSecsParam = getSolrRequest().getParams().get(UPDATE_TIMEOUT_SECS);
String updateTimeoutSecsParam = getSolrRequest().getParams().get(UPDATE_TIMEOUT_SECS);
if (updateTimeoutSecsParam != null)
updateTimeoutSecs = (updateTimeoutSecsParam instanceof Number)
? ((Number) updateTimeoutSecsParam).intValue()
: Integer.parseInt(updateTimeoutSecsParam.toString());
updateTimeoutSecs = Integer.parseInt(updateTimeoutSecsParam);
}
}
} catch (Throwable t) {

View File

@ -20,33 +20,29 @@ import java.util.Iterator;
import java.util.Map;
/**
*
* {@link SolrParams} implementation that can be built from and is backed by a {@link Map}.
*/
public class MapSolrParams extends SolrParams {
protected final Map<String,String> map;
public MapSolrParams(Map<String,String> map) {
assert map.entrySet().stream().allMatch(e -> {
boolean hasStringKey = e.getKey() == null || e.getKey().getClass() == String.class;
boolean hasStringValue = e.getValue() == null || e.getValue().getClass() == String.class;
return hasStringKey && hasStringValue;
});
this.map = map;
}
@Override
public String get(String name) {
Object o = map.get(name);
if(o == null) return null;
if (o instanceof String) return (String) o;
if (o instanceof String[]) {
String[] strings = (String[]) o;
if(strings.length == 0) return null;
return strings[0];
}
return String.valueOf(o);
return map.get(name);
}
@Override
public String[] getParams(String name) {
Object val = map.get(name);
if (val instanceof String[]) return (String[]) val;
return val==null ? null : new String[]{String.valueOf(val)};
String val = map.get(name);
return val == null ? null : new String[] { val };
}
@Override

View File

@ -55,6 +55,11 @@ public class MultiMapSolrParams extends SolrParams {
public MultiMapSolrParams(Map<String,String[]> map) {
assert map.entrySet().stream().allMatch(e -> {
boolean hasStringKey = e.getKey() == null || e.getKey().getClass() == String.class;
boolean hasStringArrayValue = e.getValue() == null || e.getValue().getClass() == String[].class;
return hasStringKey && hasStringArrayValue;
});
this.map = map;
}