[ 
https://issues.apache.org/jira/browse/SCB-778?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16560540#comment-16560540
 ] 

ASF GitHub Bot commented on SCB-778:
------------------------------------

liubao68 closed pull request #838: [SCB-778]add container path to base path
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/838
 
 
   

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/core/src/main/java/org/apache/servicecomb/core/definition/schema/ProducerSchemaFactory.java
 
b/core/src/main/java/org/apache/servicecomb/core/definition/schema/ProducerSchemaFactory.java
index a3b0a38cf..87e7cb2d8 100644
--- 
a/core/src/main/java/org/apache/servicecomb/core/definition/schema/ProducerSchemaFactory.java
+++ 
b/core/src/main/java/org/apache/servicecomb/core/definition/schema/ProducerSchemaFactory.java
@@ -17,6 +17,9 @@
 
 package org.apache.servicecomb.core.definition.schema;
 
+import static 
org.apache.servicecomb.serviceregistry.api.Const.REGISTER_URL_PREFIX;
+import static org.apache.servicecomb.serviceregistry.api.Const.URL_PREFIX;
+
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Set;
@@ -39,9 +42,11 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectWriter;
+import com.netflix.config.DynamicPropertyFactory;
 
 import io.swagger.models.Operation;
 import io.swagger.models.Swagger;
@@ -129,6 +134,13 @@ protected SchemaMeta createSchema(ProducerSchemaContext 
context) {
           swaggerContent);
     }
 
+    String urlPrefix = System.getProperty(URL_PREFIX);
+    if (!StringUtils.isEmpty(urlPrefix) && 
!swagger.getBasePath().startsWith(urlPrefix)
+        && DynamicPropertyFactory.getInstance()
+        .getBooleanProperty(REGISTER_URL_PREFIX, false).get()) {
+      LOGGER.info("Add swagger base path prefix for {} with {}", 
swagger.getBasePath(), urlPrefix);
+      swagger.setBasePath(urlPrefix + swagger.getBasePath());
+    }
     // 注册契约
     return schemaLoader.registerSchema(context.getMicroserviceMeta(), 
context.getSchemaId(), swagger);
   }
diff --git 
a/core/src/test/java/org/apache/servicecomb/core/definition/schema/TestProducerSchemaFactory.java
 
b/core/src/test/java/org/apache/servicecomb/core/definition/schema/TestProducerSchemaFactory.java
index 6500c6439..97f7ddbf3 100644
--- 
a/core/src/test/java/org/apache/servicecomb/core/definition/schema/TestProducerSchemaFactory.java
+++ 
b/core/src/test/java/org/apache/servicecomb/core/definition/schema/TestProducerSchemaFactory.java
@@ -33,6 +33,7 @@
 import org.apache.servicecomb.core.unittest.UnitTestMeta;
 import org.apache.servicecomb.foundation.common.utils.BeanUtils;
 import org.apache.servicecomb.foundation.common.utils.ReflectUtils;
+import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils;
 import org.apache.servicecomb.serviceregistry.RegistryUtils;
 import org.apache.servicecomb.swagger.engine.SwaggerEnvironment;
 import org.apache.servicecomb.swagger.engine.SwaggerProducerOperation;
@@ -49,6 +50,7 @@
 import org.junit.BeforeClass;
 import org.junit.Test;
 
+import io.swagger.models.Swagger;
 import mockit.Mock;
 import mockit.MockUp;
 
@@ -57,8 +59,6 @@
 
   private static ProducerSchemaFactory producerSchemaFactory = new 
ProducerSchemaFactory();
 
-  private static SchemaMeta schemaMeta;
-
   public static class TestProducerSchemaFactoryImpl {
     public int add(int x, int y) {
       return x + y;
@@ -102,11 +102,6 @@ public static void init() {
         return (T) normalExecutor;
       }
     };
-
-    schemaMeta = producerSchemaFactory.getOrCreateProducerSchema("app:ms",
-        "schema",
-        TestProducerSchemaFactoryImpl.class,
-        new TestProducerSchemaFactoryImpl());
   }
 
   @AfterClass
@@ -116,6 +111,12 @@ public static void teardown() {
 
   @Test
   public void testGetOrCreateProducer() throws Exception {
+    SchemaMeta schemaMeta = 
producerSchemaFactory.getOrCreateProducerSchema("app:ms",
+        "schema",
+        TestProducerSchemaFactoryImpl.class,
+        new TestProducerSchemaFactoryImpl());
+    Swagger swagger = schemaMeta.getSwagger();
+    Assert.assertEquals(swagger.getBasePath(), 
"/TestProducerSchemaFactoryImpl");
     OperationMeta operationMeta = schemaMeta.ensureFindOperation("add");
     Assert.assertEquals("add", operationMeta.getOperationId());
 
@@ -146,8 +147,30 @@ public String getInvocationQualifiedName() {
     Assert.assertEquals("Cse Internal Server Error", data.getMessage());
   }
 
+  @Test
+  public void testGetOrCreateProducerWithPrefix() throws Exception {
+    
ArchaiusUtils.setProperty(org.apache.servicecomb.serviceregistry.api.Const.REGISTER_URL_PREFIX,
 "true");
+    
System.setProperty(org.apache.servicecomb.serviceregistry.api.Const.URL_PREFIX, 
"/pojo/test");
+
+    SchemaMeta schemaMeta = 
producerSchemaFactory.getOrCreateProducerSchema("app:ms",
+        "schema2",
+        TestProducerSchemaFactoryImpl.class,
+        new TestProducerSchemaFactoryImpl());
+    OperationMeta operationMeta = schemaMeta.ensureFindOperation("add");
+    Assert.assertEquals("add", operationMeta.getOperationId());
+    Swagger swagger = schemaMeta.getSwagger();
+    Assert.assertEquals(swagger.getBasePath(), 
"/pojo/test/TestProducerSchemaFactoryImpl");
+
+    ArchaiusUtils.resetConfig();
+    
System.getProperties().remove(org.apache.servicecomb.serviceregistry.api.Const.URL_PREFIX);
+  }
+
   @Test
   public void testCompletableFuture() {
+    SchemaMeta schemaMeta = 
producerSchemaFactory.getOrCreateProducerSchema("app:ms",
+        "schema3",
+        TestProducerSchemaFactoryImpl.class,
+        new TestProducerSchemaFactoryImpl());
     OperationMeta operationMeta = schemaMeta.ensureFindOperation("async");
     Assert.assertThat(operationMeta.getExecutor(), 
Matchers.instanceOf(ReactiveExecutor.class));
   }
diff --git 
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/Const.java
 
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/Const.java
index 84e842485..1092f05e8 100644
--- 
a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/Const.java
+++ 
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/api/Const.java
@@ -184,4 +184,6 @@ private Const() {
   public static final String SERVICECENTER_FRAMEWORK_VERSION = "1.0.0";
 
   public static final String REGISTER_SERVICE_PATH = 
"servicecomb.service.registry.registerPath";
+
+  public static final String REGISTER_URL_PREFIX = 
"servicecomb.service.registry.registerUrlPrefix";
 }


 

----------------------------------------------------------------
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:
[email protected]


> In tomcat, support register swagger base path with container prefix
> -------------------------------------------------------------------
>
>                 Key: SCB-778
>                 URL: https://issues.apache.org/jira/browse/SCB-778
>             Project: Apache ServiceComb
>          Issue Type: New Feature
>            Reporter: liubao
>            Assignee: liubao
>            Priority: Major
>
> Current:
>   base path is register with service definition, e.g. /codeFirst/add, and 
> container prefix, e.g. /pojo/rest is not added to swagger. 
>   
>   When using RestTemplate to invoke, users do not need know about container 
> prefix
>  
> Users scenario:
>    They have a lot of client code(old framework and uses full path, e.g. 
> /pojo/rest/codeFirst/add
>  
> Solution:
>    provider give an option to register full path to base path. 



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to