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

klease pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git


The following commit(s) were added to refs/heads/main by this push:
     new 63197c9  CAMEL-17535: Fix server URL in generated swagger UI. (#435)
63197c9 is described below

commit 63197c9cead556d6ce20d3c33b42abd1c4497134
Author: klease <[email protected]>
AuthorDate: Fri Feb 11 10:39:33 2022 +0100

    CAMEL-17535: Fix server URL in generated swagger UI. (#435)
    
    * CAMEL-17535: Fix server URL in generated swagger UI.
    Account for both possible spring context path and Camel REST context path.
---
 .../springdoc/SpringdocAutoConfiguration.java      | 48 +++++++++++++++++++---
 1 file changed, 43 insertions(+), 5 deletions(-)

diff --git 
a/components-starter/camel-springdoc-starter/src/main/java/org/apache/camel/springboot/springdoc/SpringdocAutoConfiguration.java
 
b/components-starter/camel-springdoc-starter/src/main/java/org/apache/camel/springboot/springdoc/SpringdocAutoConfiguration.java
index 0b46143..d1e94be 100644
--- 
a/components-starter/camel-springdoc-starter/src/main/java/org/apache/camel/springboot/springdoc/SpringdocAutoConfiguration.java
+++ 
b/components-starter/camel-springdoc-starter/src/main/java/org/apache/camel/springboot/springdoc/SpringdocAutoConfiguration.java
@@ -23,6 +23,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.SerializationFeature;
 import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
 import io.apicurio.datamodels.openapi.models.OasDocument;
+import io.apicurio.datamodels.openapi.v3.models.Oas30Document;
+import io.apicurio.datamodels.core.models.common.Server;
 import io.swagger.v3.oas.models.OpenAPI;
 import io.swagger.v3.oas.models.info.Contact;
 import io.swagger.v3.oas.models.info.Info;
@@ -38,6 +40,7 @@ import org.apache.camel.spi.RestConfiguration;
 import org.apache.camel.spring.boot.CamelContextConfiguration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.autoconfigure.AutoConfigureAfter;
 import org.springframework.boot.autoconfigure.AutoConfigureBefore;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@@ -72,6 +75,9 @@ public class SpringdocAutoConfiguration {
     private final RestOpenApiReader reader = new RestOpenApiReader();
     private final RestDefinitionsResolver resolver = new 
DefaultRestDefinitionsResolver();
     private final OpenAPI openapi = new OpenAPI();
+    
+    @Value("${server.servlet.context-path:}")
+    private String springContextPath;
 
     @Bean
     CamelContextConfiguration springdocOnBeforeStart(final 
GenericApplicationContext ac, final CamelContext camelContext, final 
SpringdocConfiguration config) {
@@ -94,7 +100,7 @@ public class SpringdocAutoConfiguration {
                                 
Optional.ofNullable(created.getExternalDocs()).ifPresent(openapi::setExternalDocs);
                                 
Optional.ofNullable(created.getPaths()).ifPresent(openapi::setPaths);
                                 
Optional.ofNullable(created.getTags()).ifPresent(openapi::setTags);
-                                // Note: do not copy servers, since we use the 
spring-boot configured setting
+                                
Optional.ofNullable(created.getServers()).ifPresent(openapi::setServers);
                             });
                 } catch (Exception e) {
                     LOG.warn("Error generating OpenAPI from Camel Rest DSL due 
to: {}. This exception is ignored.", e.getMessage(), e);
@@ -124,14 +130,24 @@ public class SpringdocAutoConfiguration {
 
         final BeanConfig bc = new BeanConfig();
         final Info info = new Info();
-
         final RestConfiguration rc = camelContext.getRestConfiguration();
-        initOpenApi(bc, info, 
Optional.ofNullable(rc.getApiProperties()).orElseGet(HashMap::new));
+        Map<String, Object> apiProps = 
Optional.ofNullable(rc.getApiProperties()).orElseGet(HashMap::new);
+        initOpenApi(bc, info, apiProps, 
+                getBasePath(springContextPath, apiProps.get("base.path"), 
rc.getContextPath()));
 
         final OasDocument openApi = reader.read(camelContext, rests, bc, null, 
camelContext.getClassResolver());
         if (!rc.isApiVendorExtension()) {
             clearVendorExtensions(openApi);
         }
+        // Set relative path in URL if basepath is set
+        if (bc.getBasePath()!=null && !bc.getBasePath().isEmpty() && 
openApi.is3xDocument()) {
+            for (Server server : ((Oas30Document)openApi).getServers()) {
+                if (server.url.endsWith(bc.getBasePath())) {
+                    LOG.info("Setting Server URL in ApiDoc to base path: {}", 
bc.getBasePath());
+                    server.url = bc.getBasePath();
+                }
+            }
+        }
 
         // dump to json
         final ObjectMapper mapper = new ObjectMapper(new JsonFactory());
@@ -151,6 +167,28 @@ public class SpringdocAutoConfiguration {
     }
 
     /**
+     * Return the basePath for the REST services
+     * @param springContextPath the spring context path if set or empty
+     * @param basePath The apiProperty "base.path" from the REST configuration
+     * @param contextPath the REST contextPath or null. 
+     * Used instead of basePath if both are non-null.
+     * @return the combined contextPath
+     */
+    private String getBasePath(String springContextPath, Object basePath, 
String contextPath) {
+        if (contextPath == null) {
+            contextPath = (String)basePath; // could still be null
+        }
+        if (contextPath != null && !contextPath.isEmpty()) {
+            return springContextPath + contextPath;
+        }
+        else {
+            // This will cause problems when using the Camel servlet as the 
REST component! 
+            LOG.warn("No REST context path set in Camel Rest-DSL!");
+            return springContextPath;
+        }
+    }
+
+    /**
      * This consumes a property object, if non-null, by converting it to a 
string and running the given
      * string consumer on the value.
      */
@@ -159,10 +197,10 @@ public class SpringdocAutoConfiguration {
                     .map(String.class::cast)
                     .ifPresent(b);
 
-    private static void initOpenApi(BeanConfig bc, Info info, Map<String, 
Object> config) {
+    private static void initOpenApi(BeanConfig bc, Info info, Map<String, 
Object> config, String basePath) {
         // configure openApi options
         consumeProperty.accept(config.get("openapi.version"), bc::setVersion);
-        consumeProperty.accept(config.get("base.path"), bc::setBasePath);
+        consumeProperty.accept(basePath, bc::setBasePath);
         consumeProperty.accept(config.get("host"), bc::setHost);
         consumeProperty.accept(config.get("api.version"), info::setVersion);
         consumeProperty.accept(config.get("api.description"), 
info::setDescription);

Reply via email to