From 7d19c9041e93d82cdf2789aaaba64568f30ea9f1 Mon Sep 17 00:00:00 2001 From: Noble Paul Date: Tue, 15 Dec 2020 14:18:09 +1100 Subject: [PATCH] SOLR-14977: improved plugin configuration (#2099) --- .../org/apache/solr/api/AnnotatedApi.java | 4 +++- .../solr/api/ContainerPluginsRegistry.java | 23 ++++++++----------- .../solrj/request/beans/PluginMeta.java | 13 +++++++++-- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/api/AnnotatedApi.java b/solr/core/src/java/org/apache/solr/api/AnnotatedApi.java index 9ec86ce75951..7b5706957988 100644 --- a/solr/core/src/java/org/apache/solr/api/AnnotatedApi.java +++ b/solr/core/src/java/org/apache/solr/api/AnnotatedApi.java @@ -34,6 +34,7 @@ import java.util.Map; import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.solr.client.solrj.SolrRequest; import org.apache.solr.common.SolrException; @@ -64,7 +65,8 @@ public class AnnotatedApi extends Api implements PermissionNameProvider , Closeable { private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private static final ObjectMapper mapper = SolrJacksonAnnotationInspector.createObjectMapper() - .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .disable(MapperFeature.AUTO_DETECT_FIELDS); public static final String ERR = "Error executing commands :"; private EndPoint endPoint; diff --git a/solr/core/src/java/org/apache/solr/api/ContainerPluginsRegistry.java b/solr/core/src/java/org/apache/solr/api/ContainerPluginsRegistry.java index 9fa0261e6158..8f1d3868058b 100644 --- a/solr/core/src/java/org/apache/solr/api/ContainerPluginsRegistry.java +++ b/solr/core/src/java/org/apache/solr/api/ContainerPluginsRegistry.java @@ -24,16 +24,12 @@ import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; +import java.util.*; import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Supplier; import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.lucene.util.ResourceLoaderAware; import org.apache.solr.client.solrj.SolrRequest; @@ -73,7 +69,8 @@ public class ContainerPluginsRegistry implements ClusterPropertiesListener, MapW private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private static final ObjectMapper mapper = SolrJacksonAnnotationInspector.createObjectMapper() - .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .disable(MapperFeature.AUTO_DETECT_FIELDS); private final List listeners = new CopyOnWriteArrayList<>(); @@ -390,9 +387,11 @@ public void init() throws Exception { } if (instance instanceof ConfigurablePlugin) { Class c = getConfigClass((ConfigurablePlugin) instance); - if (c != null && holder.meta.config != null) { - MapWriter initVal = mapper.readValue(Utils.toJSON(holder.meta.config), c); - ((ConfigurablePlugin) instance).configure(initVal); + if (c != null) { + Map original = (Map) holder.original.getOrDefault("config", Collections.emptyMap()); + holder.meta.config = mapper.readValue(Utils.toJSON(original), c); + ((ConfigurablePlugin) instance).configure(holder.meta.config); + } } if (instance instanceof ResourceLoaderAware) { @@ -437,10 +436,6 @@ public static Class getConfigClass(ConfigurablePlugin o) { public ApiInfo createInfo(Map info, List errs) throws IOException { return new ApiInfo(new PluginMetaHolder(info), errs); - } - public ApiInfo createInfo(PluginMetaHolder info, List errs) { - return new ApiInfo(info, errs); - } public enum Diff { diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/beans/PluginMeta.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/beans/PluginMeta.java index bab68b2cc72a..5aa85325d581 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/beans/PluginMeta.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/beans/PluginMeta.java @@ -17,8 +17,10 @@ package org.apache.solr.client.solrj.request.beans; +import java.io.IOException; import java.util.Objects; +import org.apache.solr.common.MapWriter; import org.apache.solr.common.annotation.JsonProperty; import org.apache.solr.common.util.ReflectMapWriter; @@ -43,8 +45,8 @@ public class PluginMeta implements ReflectMapWriter { public String pathPrefix; /** Plugin configuration object, optional. */ - @JsonProperty - public Object config; + public MapWriter config; + public PluginMeta copy() { @@ -56,6 +58,12 @@ public PluginMeta copy() { return result; } + @Override + public void writeMap(EntryWriter ew) throws IOException { + ReflectMapWriter.super.writeMap(ew); + ew.putIfNotNull("config", config); + } + @Override public boolean equals(Object obj) { if (obj instanceof PluginMeta) { @@ -63,6 +71,7 @@ public boolean equals(Object obj) { return Objects.equals(this.name, that.name) && Objects.equals(this.klass, that.klass) && Objects.equals(this.version, that.version) && + Objects.equals(this.config, that.config); } return false;