This is an automated email from the ASF dual-hosted git repository.

panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new e46f68d  Fixes NPE when starting proxy if there is no table in 
configured databases (#8348)
e46f68d is described below

commit e46f68d9d0af842de3db99839a1bc9b474f9a51f
Author: Haoran Meng <[email protected]>
AuthorDate: Thu Nov 26 11:51:51 2020 +0800

    Fixes NPE when starting proxy if there is no table in configured databases 
(#8348)
    
    * Fixes npe when starting proxy without tables
    
    * Add an empty line at the end of yaml
---
 .../core/yaml/swapper/SchemaYamlSwapper.java           |  4 +++-
 .../core/yaml/swapper/SchemaYamlSwapperTest.java       | 17 +++++++++++++++++
 .../src/test/resources/yaml/schema-without-table.yaml  | 18 ++++++++++++++++++
 3 files changed, 38 insertions(+), 1 deletion(-)

diff --git 
a/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-common/src/main/java/org/apache/shardingsphere/governance/core/yaml/swapper/SchemaYamlSwapper.java
 
b/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-common/src/main/java/org/apache/shardingsphere/governance/core/yaml/swapper/SchemaYamlSwapper.java
index 10fe6ce..f78d8ae 100644
--- 
a/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-common/src/main/java/org/apache/shardingsphere/governance/core/yaml/swapper/SchemaYamlSwapper.java
+++ 
b/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-common/src/main/java/org/apache/shardingsphere/governance/core/yaml/swapper/SchemaYamlSwapper.java
@@ -17,6 +17,8 @@
 
 package org.apache.shardingsphere.governance.core.yaml.swapper;
 
+import com.google.common.collect.Maps;
+import org.apache.commons.collections4.MapUtils;
 import 
org.apache.shardingsphere.governance.core.yaml.config.schema.YamlColumnMetaData;
 import 
org.apache.shardingsphere.governance.core.yaml.config.schema.YamlIndexMetaData;
 import org.apache.shardingsphere.governance.core.yaml.config.schema.YamlSchema;
@@ -55,7 +57,7 @@ public final class SchemaYamlSwapper implements 
YamlSwapper<YamlSchema, Sharding
     }
     
     private ShardingSphereSchema convertSchema(final YamlSchema schema) {
-        return new ShardingSphereSchema(schema.getTables().entrySet().stream()
+        return new ShardingSphereSchema(MapUtils.isEmpty(schema.getTables()) ? 
Maps.newLinkedHashMap() : schema.getTables().entrySet().stream()
                 .collect(Collectors.toMap(Entry::getKey, entry -> 
convertTable(entry.getValue()), (oldValue, currentValue) -> oldValue, 
LinkedHashMap::new)));
     }
     
diff --git 
a/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-common/src/test/java/org/apache/shardingsphere/governance/core/yaml/swapper/SchemaYamlSwapperTest.java
 
b/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-common/src/test/java/org/apache/shardingsphere/governance/core/yaml/swapper/SchemaYamlSwapperTest.java
index ea43734..d0497a8 100644
--- 
a/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-common/src/test/java/org/apache/shardingsphere/governance/core/yaml/swapper/SchemaYamlSwapperTest.java
+++ 
b/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-common/src/test/java/org/apache/shardingsphere/governance/core/yaml/swapper/SchemaYamlSwapperTest.java
@@ -38,6 +38,8 @@ public final class SchemaYamlSwapperTest {
     
     private static final String YAML = "yaml/schema.yaml";
     
+    private static final String YAML_WITHOUT_TABLE = 
"yaml/schema-without-table.yaml";
+    
     @Test
     public void assertSwapToYamlSchema() {
         ShardingSphereSchema schema = new 
SchemaYamlSwapper().swapToObject(YamlEngine.unmarshal(readYAML(YAML), 
YamlSchema.class));
@@ -58,6 +60,21 @@ public final class SchemaYamlSwapperTest {
         assertThat(schema.get("t_order").getColumns().keySet(), 
is(Collections.singleton("id")));
     }
     
+    @Test
+    public void assertSwapToYamlSchemaWithoutTable() {
+        ShardingSphereSchema schema = new 
SchemaYamlSwapper().swapToObject(YamlEngine.unmarshal(readYAML(YAML_WITHOUT_TABLE),
 YamlSchema.class));
+        YamlSchema yamlSchema = new 
SchemaYamlSwapper().swapToYamlConfiguration(schema);
+        assertNotNull(yamlSchema);
+        assertThat(yamlSchema.getTables().size(), is(0));
+    }
+    
+    @Test
+    public void assertSwapToShardingSphereSchemaWithoutTable() {
+        YamlSchema yamlSchema = 
YamlEngine.unmarshal(readYAML(YAML_WITHOUT_TABLE), YamlSchema.class);
+        ShardingSphereSchema schema = new 
SchemaYamlSwapper().swapToObject(yamlSchema);
+        assertThat(schema.getAllTableNames().size(), is(0));
+    }
+    
     @SneakyThrows({URISyntaxException.class, IOException.class})
     private String readYAML(final String yamlFile) {
         return 
Files.readAllLines(Paths.get(ClassLoader.getSystemResource(yamlFile).toURI()))
diff --git 
a/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-common/src/test/resources/yaml/schema-without-table.yaml
 
b/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-common/src/test/resources/yaml/schema-without-table.yaml
new file mode 100644
index 0000000..4c456c0
--- /dev/null
+++ 
b/shardingsphere-governance/shardingsphere-governance-core/shardingsphere-governance-core-common/src/test/resources/yaml/schema-without-table.yaml
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+tables:

Reply via email to