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

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


The following commit(s) were added to refs/heads/master by this push:
     new 84a6cf35bc [type:fix] fixed #5057 (#5058)
84a6cf35bc is described below

commit 84a6cf35bc761a73e63c6fb5456e1949e5b51d8c
Author: Kerwin Bryant <[email protected]>
AuthorDate: Fri Sep 8 13:45:22 2023 +0800

    [type:fix] fixed #5057 (#5058)
    
    * Added support for enabling debug logs for okhttp
    
    * Create LICENSE-logging-interceptor.txt
    
    * Fix incorrect api path in specific configuration
    
    * fixed tests error
    
    ---------
    
    Co-authored-by: dragon-zhang <[email protected]>
    Co-authored-by: yunlongn <[email protected]>
    Co-authored-by: moremind <[email protected]>
---
 .../init/SpringMvcClientEventListener.java         | 27 +++++++++++++-----
 .../init/SpringMvcClientEventListenerTest.java     | 33 +++++++++++++++++-----
 .../ShenyuSpringMvcClientConfiguration.java        |  3 +-
 3 files changed, 47 insertions(+), 16 deletions(-)

diff --git 
a/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientEventListener.java
 
b/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientEventListener.java
index 78f9063426..67692b801d 100644
--- 
a/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientEventListener.java
+++ 
b/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientEventListener.java
@@ -40,6 +40,7 @@ import org.slf4j.LoggerFactory;
 import org.springframework.context.ApplicationContext;
 import org.springframework.core.annotation.AnnotatedElementUtils;
 import org.springframework.core.annotation.AnnotationUtils;
+import org.springframework.core.env.Environment;
 import org.springframework.lang.NonNull;
 import org.springframework.lang.Nullable;
 import org.springframework.stereotype.Controller;
@@ -65,7 +66,7 @@ import java.util.stream.Stream;
 public class SpringMvcClientEventListener extends 
AbstractContextRefreshedEventListener<Object, ShenyuSpringMvcClient> {
 
     private static final Logger LOG = 
LoggerFactory.getLogger(SpringMvcClientEventListener.class);
-    
+
     private final ShenyuClientRegisterEventPublisher publisher = 
ShenyuClientRegisterEventPublisher.getInstance();
 
     private final List<Class<? extends Annotation>> mappingAnnotation = new 
ArrayList<>(3);
@@ -76,15 +77,20 @@ public class SpringMvcClientEventListener extends 
AbstractContextRefreshedEventL
 
     private final boolean addPrefixed;
 
+    private final Environment env;
+
     /**
      * Instantiates a new context refreshed event listener.
      *
      * @param clientConfig                   the shenyu client config
      * @param shenyuClientRegisterRepository the shenyuClientRegisterRepository
+     * @param env                            the env
      */
     public SpringMvcClientEventListener(final PropertiesConfig clientConfig,
-                                        final ShenyuClientRegisterRepository 
shenyuClientRegisterRepository) {
+                                        final ShenyuClientRegisterRepository 
shenyuClientRegisterRepository,
+                                        final Environment env) {
         super(clientConfig, shenyuClientRegisterRepository);
+        this.env = env;
         Properties props = clientConfig.getProps();
         this.isFull = 
Boolean.parseBoolean(props.getProperty(ShenyuClientConstants.IS_FULL, 
Boolean.FALSE.toString()));
         this.protocol = props.getProperty(ShenyuClientConstants.PROTOCOL, 
ShenyuClientConstants.HTTP);
@@ -150,15 +156,18 @@ public class SpringMvcClientEventListener extends 
AbstractContextRefreshedEventL
 
     @Override
     protected String buildApiSuperPath(final Class<?> clazz, @Nullable final 
ShenyuSpringMvcClient beanShenyuClient) {
+        final String servletPath = 
StringUtils.defaultString(this.env.getProperty("spring.mvc.servlet.path"), "");
+        final String servletContextPath = 
StringUtils.defaultString(this.env.getProperty("server.servlet.context-path"), 
"");
+        final String rootPath = String.format("/%s/%s/", servletContextPath, 
servletPath);
         if (Objects.nonNull(beanShenyuClient) && 
StringUtils.isNotBlank(beanShenyuClient.path())) {
-            return beanShenyuClient.path();
+            return formatPath(String.format("%s/%s", rootPath, 
beanShenyuClient.path()));
         }
         RequestMapping requestMapping = AnnotationUtils.findAnnotation(clazz, 
RequestMapping.class);
         // Only the first path is supported temporarily
         if (Objects.nonNull(requestMapping) && 
ArrayUtils.isNotEmpty(requestMapping.path()) && 
StringUtils.isNotBlank(requestMapping.path()[0])) {
-            return requestMapping.path()[0];
+            return formatPath(String.format("%s/%s", rootPath, 
requestMapping.path()[0]));
         }
-        return "";
+        return formatPath(rootPath);
     }
 
     @Override
@@ -197,6 +206,10 @@ public class SpringMvcClientEventListener extends 
AbstractContextRefreshedEventL
         return pathJoin(contextPath, superPath);
     }
 
+    private String formatPath(final String path) {
+        return path.replaceAll("/+", "/").replaceFirst("/$", "");
+    }
+
     private String getPathByMethod(@NonNull final Method method) {
         for (Class<? extends Annotation> mapping : mappingAnnotation) {
             final String pathByAnnotation = 
getPathByAnnotation(AnnotatedElementUtils.findMergedAnnotation(method, 
mapping));
@@ -246,14 +259,14 @@ public class SpringMvcClientEventListener extends 
AbstractContextRefreshedEventL
                 .registerMetaData(shenyuClient.registerMetaData())
                 .build();
     }
-    
+
     @Override
     protected ApiDocRegisterDTO.ApiExt customApiDocExt(final 
ApiDocRegisterDTO.ApiExt ext) {
         ext.setProtocol(protocol);
         ext.setAddPrefixed(addPrefixed);
         return ext;
     }
-    
+
     @Override
     public String getPort() {
         final int port = 
Integer.parseInt(Optional.ofNullable(super.getPort()).orElseGet(() -> "-1"));
diff --git 
a/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientEventListenerTest.java
 
b/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientEventListenerTest.java
index 89df6b2d95..09681b3b43 100644
--- 
a/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientEventListenerTest.java
+++ 
b/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientEventListenerTest.java
@@ -36,6 +36,8 @@ import org.mockito.stubbing.Answer;
 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.event.ContextRefreshedEvent;
+import org.springframework.core.annotation.AnnotatedElementUtils;
+import org.springframework.core.env.Environment;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -72,10 +74,13 @@ public class SpringMvcClientEventListenerTest {
 
     @Mock
     private ApplicationContext applicationContext;
-    
+
     @Mock
     private AutowireCapableBeanFactory beanFactory;
 
+    @Mock
+    private Environment env;
+
     private ContextRefreshedEvent contextRefreshedEvent;
 
     private void init() {
@@ -90,7 +95,7 @@ public class SpringMvcClientEventListenerTest {
 
         PropertiesConfig clientConfig = mock(PropertiesConfig.class);
         when(clientConfig.getProps()).thenReturn(properties);
-        Assert.assertThrows(ShenyuClientIllegalArgumentException.class, () -> 
new SpringMvcClientEventListener(clientConfig, 
mock(ShenyuClientRegisterRepository.class)));
+        Assert.assertThrows(ShenyuClientIllegalArgumentException.class, () -> 
new SpringMvcClientEventListener(clientConfig, 
mock(ShenyuClientRegisterRepository.class), env));
     }
 
     @Test
@@ -141,9 +146,9 @@ public class SpringMvcClientEventListenerTest {
         mockRegisterCenter.setServerLists("http://127.0.0.1:9095";);
         mockRegisterCenter.setRegisterType("http");
         mockRegisterCenter.setProps(properties);
-        return new SpringMvcClientEventListener(config, 
ShenyuClientRegisterRepositoryFactory.newInstance(mockRegisterCenter));
+        return new SpringMvcClientEventListener(config, 
ShenyuClientRegisterRepositoryFactory.newInstance(mockRegisterCenter), env);
     }
-    
+
     @Test
     public void testOnApplicationEvent() {
         init();
@@ -152,13 +157,13 @@ public class SpringMvcClientEventListenerTest {
         MockedStatic<PortUtils> portUtilsMockedStatic = 
mockStatic(PortUtils.class);
         portUtilsMockedStatic.when(() -> 
PortUtils.findPort(beanFactory)).thenReturn(8080);
         springMvcClientEventListener.onApplicationEvent(contextRefreshedEvent);
-    
+
         // hit `!registered.compareAndSet(false, true)`
         springMvcClientEventListener.onApplicationEvent(contextRefreshedEvent);
         portUtilsMockedStatic.close();
         registerUtilsMockedStatic.close();
     }
-    
+
     @Test
     public void testOnApplicationEventError() {
         init();
@@ -166,7 +171,21 @@ public class SpringMvcClientEventListenerTest {
         Assert.assertThrows(ShenyuException.class, () -> 
springMvcClientEventListener.onApplicationEvent(contextRefreshedEvent));
         registerUtilsMockedStatic.close();
     }
-    
+
+    @Test
+    public void testOnBuildApiSuperPath() {
+        SpringMvcClientEventListener springMvcClientEventListener = 
buildSpringMvcClientEventListener(false, false);
+
+        Assert.assertEquals("super-path", "/order", 
springMvcClientEventListener.buildApiSuperPath(
+            SpringMvcClientTestBean.class, 
AnnotatedElementUtils.findMergedAnnotation(SpringMvcClientTestBean.class, 
ShenyuSpringMvcClient.class)));
+
+        
when(env.getProperty("spring.mvc.servlet.path")).thenReturn("/servlet-path");
+        
when(env.getProperty("server.servlet.context-path")).thenReturn("/servlet-context-path");
+        Assert.assertEquals("super-path", 
"/servlet-context-path/servlet-path/order", 
springMvcClientEventListener.buildApiSuperPath(
+            SpringMvcClientTestBean.class, 
AnnotatedElementUtils.findMergedAnnotation(SpringMvcClientTestBean.class, 
ShenyuSpringMvcClient.class)));
+        registerUtilsMockedStatic.close();
+    }
+
     @RestController
     @RequestMapping("/order")
     @ShenyuSpringMvcClient(path = "/order")
diff --git 
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-springmvc/src/main/java/org/apache/shenyu/springboot/starter/client/springmvc/ShenyuSpringMvcClientConfiguration.java
 
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-springmvc/src/main/java/org/apache/shenyu/springboot/starter/client/springmvc/ShenyuSpringMvcClientConfiguration.java
index 4a5dbdedd0..7a7f8fdc6f 100644
--- 
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-springmvc/src/main/java/org/apache/shenyu/springboot/starter/client/springmvc/ShenyuSpringMvcClientConfiguration.java
+++ 
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-springmvc/src/main/java/org/apache/shenyu/springboot/starter/client/springmvc/ShenyuSpringMvcClientConfiguration.java
@@ -33,7 +33,6 @@ import 
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.core.env.Environment;
-
 import java.util.Properties;
 
 /**
@@ -74,6 +73,6 @@ public class ShenyuSpringMvcClientConfiguration {
                 props.setProperty(ShenyuClientConstants.CONTEXT_PATH, 
String.format("/%s", applicationName));
             }
         }
-        return new SpringMvcClientEventListener(clientPropertiesConfig, 
shenyuClientRegisterRepository);
+        return new SpringMvcClientEventListener(clientPropertiesConfig, 
shenyuClientRegisterRepository, env);
     }
 }

Reply via email to