liubao68 closed pull request #714: [SCB-579] fix NullPointerException when 
consumer upload null file
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/714
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/RestClientRequestImpl.java
 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/RestClientRequestImpl.java
index 1158bc116..c6bb7e94f 100644
--- 
a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/RestClientRequestImpl.java
+++ 
b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/param/RestClientRequestImpl.java
@@ -83,6 +83,10 @@ public Buffer getBodyBuffer() throws Exception {
 
   @Override
   public void attach(String name, Part part) {
+    if (null == part) {
+      LOGGER.debug("null file is ignored, file name = [{}]", name);
+      return;
+    }
     uploads.put(name, part);
   }
 
diff --git 
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestRestClientRequestImpl.java
 
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestRestClientRequestImpl.java
index 2f5607138..d14c10e36 100644
--- 
a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestRestClientRequestImpl.java
+++ 
b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/param/TestRestClientRequestImpl.java
@@ -16,17 +16,22 @@
  */
 package org.apache.servicecomb.common.rest.codec.param;
 
+import java.util.Map;
+
 import javax.servlet.http.Part;
 import javax.ws.rs.core.MediaType;
 
+import org.hamcrest.Matchers;
 import org.junit.Assert;
 import org.junit.Test;
+import org.mockito.Mockito;
 
 import io.vertx.core.MultiMap;
 import io.vertx.core.buffer.Buffer;
 import io.vertx.core.http.CaseInsensitiveHeaders;
 import io.vertx.core.http.HttpClientRequest;
 import io.vertx.core.http.HttpHeaders;
+import mockit.Deencapsulation;
 import mockit.Expectations;
 import mockit.Mock;
 import mockit.MockUp;
@@ -34,7 +39,7 @@
 
 public class TestRestClientRequestImpl {
   @Mocked
-  HttpClientRequest request;
+  private HttpClientRequest request;
 
   @Test
   public void testForm() throws Exception {
@@ -113,4 +118,27 @@ public void 
fileBoundaryInfo_validSubmittedFileName(@Mocked Part part) {
         "Content-Transfer-Encoding: binary\r\n" +
         "\r\n", buffer.toString());
   }
+
+  @Test
+  public void testAttach() {
+    RestClientRequestImpl restClientRequest = new 
RestClientRequestImpl(request, null, null);
+    Part part = Mockito.mock(Part.class);
+    String fileName = "fileName";
+
+    restClientRequest.attach(fileName, part);
+
+    Map<String, Part> uploads = Deencapsulation.getField(restClientRequest, 
"uploads");
+    Assert.assertEquals(1, uploads.size());
+    Assert.assertThat(uploads, Matchers.hasEntry(fileName, part));
+  }
+
+  @Test
+  public void testAttachOnPartIsNull() {
+    RestClientRequestImpl restClientRequest = new 
RestClientRequestImpl(request, null, null);
+
+    restClientRequest.attach("fileName", null);
+
+    Map<String, Part> uploads = Deencapsulation.getField(restClientRequest, 
"uploads");
+    Assert.assertTrue(uploads.isEmpty());
+  }
 }
diff --git 
a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxServerRequestToHttpServletRequest.java
 
b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxServerRequestToHttpServletRequest.java
index 94bc41cb6..31b3892cc 100644
--- 
a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxServerRequestToHttpServletRequest.java
+++ 
b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxServerRequestToHttpServletRequest.java
@@ -17,7 +17,6 @@
 
 package org.apache.servicecomb.foundation.vertx.http;
 
-import java.io.IOException;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashMap;
@@ -27,7 +26,6 @@
 import java.util.Set;
 
 import javax.servlet.AsyncContext;
-import javax.servlet.ServletException;
 import javax.servlet.ServletInputStream;
 import javax.servlet.http.Cookie;
 import javax.servlet.http.Part;
@@ -106,7 +104,6 @@ public String getParameter(String name) {
     return this.vertxRequest.getParam(name);
   }
 
-
   @Override
   public String[] getParameterValues(String name) {
     List<String> paramList = this.vertxRequest.params().getAll(name);
@@ -154,7 +151,6 @@ public int getLocalPort() {
     return this.vertxRequest.localAddress().port();
   }
 
-
   @Override
   public String getHeader(String name) {
     return this.vertxRequest.getHeader(name);
@@ -203,7 +199,6 @@ public String getRequestURI() {
     return this.path;
   }
 
-
   @Override
   public String getServletPath() {
     return this.getPathInfo();
@@ -215,7 +210,7 @@ public String getContextPath() {
   }
 
   @Override
-  public ServletInputStream getInputStream() throws IOException {
+  public ServletInputStream getInputStream() {
     if (inputStream == null) {
       inputStream = new BufferInputStream(context.getBody().getByteBuf());
     }
@@ -228,13 +223,13 @@ public AsyncContext getAsyncContext() {
   }
 
   @Override
-  public Part getPart(String name) throws IOException, ServletException {
+  public Part getPart(String name) {
     Optional<FileUpload> upload = context.fileUploads()
         .stream()
         .filter(fileUpload -> fileUpload.name().equals(name))
         .findFirst();
     if (!upload.isPresent()) {
-      LOGGER.error("No such file with name: {}.", name);
+      LOGGER.debug("No such file with name: {}.", name);
       return null;
     }
 
diff --git 
a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/arguments/consumer/ConsumerArgumentSame.java
 
b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/arguments/consumer/ConsumerArgumentSame.java
index d6c5d4315..eaad2cd74 100644
--- 
a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/arguments/consumer/ConsumerArgumentSame.java
+++ 
b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/arguments/consumer/ConsumerArgumentSame.java
@@ -20,8 +20,12 @@
 import org.apache.servicecomb.swagger.invocation.SwaggerInvocation;
 import org.apache.servicecomb.swagger.invocation.arguments.ArgumentMapper;
 import org.apache.servicecomb.swagger.invocation.converter.Converter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public final class ConsumerArgumentSame implements ArgumentMapper {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(ConsumerArgumentSame.class);
+
   private int consumerIdx;
 
   private int swaggerIdx;
@@ -36,6 +40,10 @@ public ConsumerArgumentSame(int consumerIdx, int swaggerIdx, 
Converter converter
 
   @Override
   public void mapArgument(SwaggerInvocation invocation, Object[] 
consumerArguments) {
+    if (null == consumerArguments[consumerIdx]) {
+      LOGGER.debug("null argument is ignored, consumerIdx = [{}]", 
consumerIdx);
+      return;
+    }
     Object swaggerParam = converter.convert(consumerArguments[consumerIdx]);
     invocation.setSwaggerArgument(swaggerIdx, swaggerParam);
   }
diff --git 
a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/arguments/consumer/ConsumerArgumentSameTest.java
 
b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/arguments/consumer/ConsumerArgumentSameTest.java
new file mode 100644
index 000000000..1af5cd8ac
--- /dev/null
+++ 
b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/arguments/consumer/ConsumerArgumentSameTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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.servicecomb.swagger.invocation.arguments.consumer;
+
+import org.apache.servicecomb.swagger.invocation.SwaggerInvocation;
+import org.apache.servicecomb.swagger.invocation.converter.Converter;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class ConsumerArgumentSameTest {
+
+  private Converter mockConverter = Mockito.mock(Converter.class);
+
+  private ConsumerArgumentSame consumerArgumentSame = new 
ConsumerArgumentSame(0, 0, mockConverter);
+
+  @Test
+  public void testMapArgumentOnArgument() {
+    SwaggerInvocation swaggerInvocation = 
Mockito.mock(SwaggerInvocation.class);
+    String[] args = {"testArg"};
+
+    Mockito.when(mockConverter.convert(args[0])).thenReturn(args[0]);
+
+    consumerArgumentSame.mapArgument(swaggerInvocation, args);
+
+    Mockito.verify(mockConverter, Mockito.times(1)).convert(args[0]);
+    Mockito.verify(swaggerInvocation, Mockito.times(1)).setSwaggerArgument(0, 
args[0]);
+  }
+
+  @Test
+  public void testMapArgumentOnArgumentIsNull() {
+    SwaggerInvocation swaggerInvocation = 
Mockito.mock(SwaggerInvocation.class);
+
+    consumerArgumentSame.mapArgument(swaggerInvocation, new Object[1]);
+
+    Mockito.verify(mockConverter, 
Mockito.never()).convert(Mockito.anyObject());
+    Mockito.verify(swaggerInvocation, 
Mockito.never()).setSwaggerArgument(Mockito.anyInt(), Mockito.anyObject());
+  }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to