You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2022/08/23 18:52:03 UTC

[cxf] branch 3.4.x-fixes updated: CXF-8710: automatically set queryConfigEnabled in SwaggerUiConfig to true if any of the params are true (#992)

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

reta pushed a commit to branch 3.4.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/3.4.x-fixes by this push:
     new 63f0e80e85 CXF-8710: automatically set queryConfigEnabled in SwaggerUiConfig to true if any of the params are true (#992)
63f0e80e85 is described below

commit 63f0e80e85453d65c0e386e6c1759c60be29302e
Author: Aleksy Wróblewski <34...@users.noreply.github.com>
AuthorDate: Tue Aug 23 14:56:19 2022 +0200

    CXF-8710: automatically set queryConfigEnabled in SwaggerUiConfig to true if any of the params are true (#992)
    
    Co-authored-by: Aleksy Wróblewski <al...@bbbit.io>
---
 .../cxf/jaxrs/swagger/ui/SwaggerUiConfig.java      |  24 ++++-
 .../cxf/jaxrs/swagger/ui/SwaggerUiConfigTest.java  | 117 +++++++++++++++++++++
 .../SwaggerUiConfigurationQueryConfigTest.java     |  22 +++-
 .../description/SwaggerUiConfigurationTest.java    |  28 ++++-
 4 files changed, 184 insertions(+), 7 deletions(-)

diff --git a/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUiConfig.java b/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUiConfig.java
index 446a39bb1c..38db5328c7 100644
--- a/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUiConfig.java
+++ b/rt/rs/description-swagger-ui/src/main/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUiConfig.java
@@ -63,7 +63,8 @@ public class SwaggerUiConfig {
     private String validatorUrl;
     // Controls whether the "Try it out" section should be enabled by default.
     private Boolean tryItOutEnabled;
-    // Enables overriding configuration parameters via URL search params.
+    // Enables overriding configuration parameters via URL search params. If not explicitly set, it
+    // will be automatically set to true when setter for any other field is called.
     private Boolean queryConfigEnabled;
     
     public String getConfigUrl() {
@@ -72,6 +73,7 @@ public class SwaggerUiConfig {
     
     public void setConfigUrl(final String configUrl) {
         this.configUrl = configUrl;
+        setQueryConfigEnabledIfNeeded();
     }
     
     public String getUrl() {
@@ -80,6 +82,7 @@ public class SwaggerUiConfig {
     
     public void setUrl(final String url) {
         this.url = url;
+        setQueryConfigEnabledIfNeeded();
     }
     
     public String getFilter() {
@@ -88,6 +91,7 @@ public class SwaggerUiConfig {
     
     public void setFilter(final String filter) {
         this.filter = filter;
+        setQueryConfigEnabledIfNeeded();
     }
     
     public Boolean getShowCommonExtensions() {
@@ -96,6 +100,7 @@ public class SwaggerUiConfig {
 
     public void setShowCommonExtensions(Boolean showCommonExtensions) {
         this.showCommonExtensions = showCommonExtensions;
+        setQueryConfigEnabledIfNeeded();
     }
     
     public Boolean getShowExtensions() {
@@ -108,6 +113,7 @@ public class SwaggerUiConfig {
 
     public void setMaxDisplayedTags(Integer maxDisplayedTags) {
         this.maxDisplayedTags = maxDisplayedTags;
+        setQueryConfigEnabledIfNeeded();
     }
 
     public SwaggerUiConfig maxDisplayedTags(Integer value) {
@@ -117,6 +123,7 @@ public class SwaggerUiConfig {
 
     public void setShowExtensions(Boolean showExtensions) {
         this.showExtensions = showExtensions;
+        setQueryConfigEnabledIfNeeded();
     }
     
     public String getDocExpansion() {
@@ -125,6 +132,7 @@ public class SwaggerUiConfig {
 
     public void setDocExpansion(String docExpansion) {
         this.docExpansion = docExpansion;
+        setQueryConfigEnabledIfNeeded();
     }
     
     public Boolean getDisplayRequestDuration() {
@@ -133,6 +141,7 @@ public class SwaggerUiConfig {
 
     public void setDisplayRequestDuration(Boolean displayRequestDuration) {
         this.displayRequestDuration = displayRequestDuration;
+        setQueryConfigEnabledIfNeeded();
     }
     
     public String getDefaultModelRendering() {
@@ -141,6 +150,7 @@ public class SwaggerUiConfig {
 
     public void setDefaultModelRendering(String defaultModelRendering) {
         this.defaultModelRendering = defaultModelRendering;
+        setQueryConfigEnabledIfNeeded();
     }
     
     public Integer getDefaultModelExpandDepth() {
@@ -149,6 +159,7 @@ public class SwaggerUiConfig {
 
     public void setDefaultModelExpandDepth(Integer defaultModelExpandDepth) {
         this.defaultModelExpandDepth = defaultModelExpandDepth;
+        setQueryConfigEnabledIfNeeded();
     }
     
     public Integer getDefaultModelsExpandDepth() {
@@ -157,6 +168,7 @@ public class SwaggerUiConfig {
 
     public void setDefaultModelsExpandDepth(Integer defaultModelsExpandDepth) {
         this.defaultModelsExpandDepth = defaultModelsExpandDepth;
+        setQueryConfigEnabledIfNeeded();
     }
     
     public Boolean getDisplayOperationId() {
@@ -165,6 +177,7 @@ public class SwaggerUiConfig {
 
     public void setDisplayOperationId(Boolean displayOperationId) {
         this.displayOperationId = displayOperationId;
+        setQueryConfigEnabledIfNeeded();
     }
     
     public Boolean getDeepLinking() {
@@ -173,6 +186,7 @@ public class SwaggerUiConfig {
 
     public void setDeepLinking(Boolean deepLinking) {
         this.deepLinking = deepLinking;
+        setQueryConfigEnabledIfNeeded();
     }
     
 
@@ -182,6 +196,7 @@ public class SwaggerUiConfig {
 
     public void setValidatorUrl(String validatorUrl) {
         this.validatorUrl = validatorUrl;
+        setQueryConfigEnabledIfNeeded();
     }
     
     public SwaggerUiConfig validatorUrl(String value) {
@@ -307,11 +322,18 @@ public class SwaggerUiConfig {
         }
     }
 
+    private void setQueryConfigEnabledIfNeeded() {
+        if (queryConfigEnabled == null) {
+            setQueryConfigEnabled(true);
+        }
+    }
+
     public Boolean isTryItOutEnabled() {
         return tryItOutEnabled;
     }
 
     public void setTryItOutEnabled(Boolean tryItOutEnabled) {
         this.tryItOutEnabled = tryItOutEnabled;
+        setQueryConfigEnabledIfNeeded();
     }
 }
diff --git a/rt/rs/description-swagger-ui/src/test/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUiConfigTest.java b/rt/rs/description-swagger-ui/src/test/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUiConfigTest.java
new file mode 100644
index 0000000000..5ee6c3c933
--- /dev/null
+++ b/rt/rs/description-swagger-ui/src/test/java/org/apache/cxf/jaxrs/swagger/ui/SwaggerUiConfigTest.java
@@ -0,0 +1,117 @@
+/**
+ * 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.cxf.jaxrs.swagger.ui;
+
+import java.util.function.Consumer;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+public class SwaggerUiConfigTest {
+
+    @Test
+    public void testQueryConfigEnabledSetsAutomaticallyWithConfigUrl() {
+        testQueryConfigEnabledSetsAutomatically(swaggerUiConfig -> swaggerUiConfig.setConfigUrl(
+                "/cxf/context/swagger-config.yaml"));
+    }
+
+    @Test
+    public void testQueryConfigEnabledSetsAutomaticallyWithUrl() {
+        testQueryConfigEnabledSetsAutomatically(swaggerUiConfig -> swaggerUiConfig.setUrl(
+                "/cxf/context/openapi.json"));
+    }
+
+    @Test
+    public void testQueryConfigEnabledSetsAutomaticallyWithFilter() {
+        testQueryConfigEnabledSetsAutomatically(swaggerUiConfig -> swaggerUiConfig.setFilter("filter"));
+    }
+
+    @Test
+    public void testQueryConfigEnabledSetsAutomaticallyWithDeepLinking() {
+        testQueryConfigEnabledSetsAutomatically(swaggerUiConfig -> swaggerUiConfig.setDeepLinking(true));
+    }
+
+    @Test
+    public void testQueryConfigEnabledSetsAutomaticallyWithDisplayOperationId() {
+        testQueryConfigEnabledSetsAutomatically(swaggerUiConfig -> swaggerUiConfig.setDisplayOperationId(true));
+    }
+
+    @Test
+    public void testQueryConfigEnabledSetsAutomaticallyWithDefaultModelsExpandDepth() {
+        testQueryConfigEnabledSetsAutomatically(swaggerUiConfig -> swaggerUiConfig.setDefaultModelsExpandDepth(5));
+    }
+
+    @Test
+    public void testQueryConfigEnabledSetsAutomaticallyWithDefaultModelExpandDepth() {
+        testQueryConfigEnabledSetsAutomatically(swaggerUiConfig -> swaggerUiConfig.setDefaultModelExpandDepth(5));
+    }
+
+    @Test
+    public void testQueryConfigEnabledSetsAutomaticallyWithDefaultModelRendering() {
+        testQueryConfigEnabledSetsAutomatically(swaggerUiConfig -> swaggerUiConfig.setDefaultModelRendering("model"));
+    }
+
+    @Test
+    public void testQueryConfigEnabledSetsAutomaticallyWithDisplayRequestDuration() {
+        testQueryConfigEnabledSetsAutomatically(swaggerUiConfig -> swaggerUiConfig.setDisplayRequestDuration(false));
+    }
+
+    @Test
+    public void testQueryConfigEnabledSetsAutomaticallyWithDocExpansion() {
+        testQueryConfigEnabledSetsAutomatically(swaggerUiConfig -> swaggerUiConfig.setDocExpansion("list"));
+    }
+
+    @Test
+    public void testQueryConfigEnabledSetsAutomaticallyWithMaxDisplayedTags() {
+        testQueryConfigEnabledSetsAutomatically(swaggerUiConfig -> swaggerUiConfig.setMaxDisplayedTags(3));
+    }
+
+    @Test
+    public void testQueryConfigEnabledSetsAutomaticallyWithShowExtensions() {
+        testQueryConfigEnabledSetsAutomatically(swaggerUiConfig -> swaggerUiConfig.setShowExtensions(true));
+    }
+
+    @Test
+    public void testQueryConfigEnabledSetsAutomaticallyWithShowCommonExtensions() {
+        testQueryConfigEnabledSetsAutomatically(swaggerUiConfig -> swaggerUiConfig.setShowCommonExtensions(false));
+    }
+
+    @Test
+    public void testQueryConfigEnabledSetsAutomaticallyWithValidatorUrl() {
+        testQueryConfigEnabledSetsAutomatically(swaggerUiConfig -> swaggerUiConfig.setValidatorUrl(
+                "https://validator.swagger.io/validator"));
+    }
+
+    @Test
+    public void testQueryConfigEnabledSetsAutomaticallyWithTryItOutEnabled() {
+        testQueryConfigEnabledSetsAutomatically(swaggerUiConfig -> swaggerUiConfig.setTryItOutEnabled(true));
+    }
+
+    private void testQueryConfigEnabledSetsAutomatically(Consumer<SwaggerUiConfig> setter) {
+        SwaggerUiConfig swaggerUiConfig = new SwaggerUiConfig();
+        assertNull(swaggerUiConfig.isQueryConfigEnabled());
+        setter.accept(swaggerUiConfig);
+        assertTrue(swaggerUiConfig.isQueryConfigEnabled());
+        assertFalse(swaggerUiConfig.getConfigParameters().isEmpty());
+    }
+}
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/SwaggerUiConfigurationQueryConfigTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/SwaggerUiConfigurationQueryConfigTest.java
index ddb8175d68..fc5aee0ad8 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/SwaggerUiConfigurationQueryConfigTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/SwaggerUiConfigurationQueryConfigTest.java
@@ -40,6 +40,7 @@ import org.junit.Test;
 import static org.hamcrest.CoreMatchers.containsString;
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 public class SwaggerUiConfigurationQueryConfigTest extends AbstractBusClientServerTestBase {
@@ -56,7 +57,7 @@ public class SwaggerUiConfigurationQueryConfigTest extends AbstractBusClientServ
             sf.setProvider(new JacksonJsonProvider());
             final Swagger2Feature feature = new Swagger2Feature();
             feature.setRunAsFilter(false);
-            feature.setSwaggerUiConfig(new SwaggerUiConfig().url("/swagger.json").queryConfigEnabled(true));
+            feature.setSwaggerUiConfig(new SwaggerUiConfig().url("/swagger.json"));
             sf.setFeatures(Arrays.asList(feature));
             sf.setAddress("http://localhost:" + PORT + "/");
             sf.create();
@@ -98,6 +99,25 @@ public class SwaggerUiConfigurationQueryConfigTest extends AbstractBusClientServ
         }
     }
 
+    @Test
+    public void testUiRootResourceDoesNotReplaceUrl() {
+        // With query config enabled, we do not replace any values in the Swagger resource, just let
+        // Swagger UI handle the query parameters.
+        WebClient uiClient = WebClient
+                .create("http://localhost:" + getPort() + "/api-docs")
+                .path("/swagger-initializer.js")
+                .query("url", "/another-swagger.json")
+                .accept("*/*");
+
+        try (Response response = uiClient.get()) {
+            String jsCode = response.readEntity(String.class);
+            // We can only verify that the url was not replaced with the one configured, but not that
+            // the one in query is used (that would be testing the Swagger itself). The query parameter was included
+            // to demonstrate how the address might look, though.
+            assertFalse(jsCode.contains("url: \"/swagger.json\""));
+        }
+    }
+
     public static String getPort() {
         return PORT;
     }
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/SwaggerUiConfigurationTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/SwaggerUiConfigurationTest.java
index f8f1131672..07e5ea8144 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/SwaggerUiConfigurationTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/SwaggerUiConfigurationTest.java
@@ -41,6 +41,7 @@ import org.junit.Test;
 import static org.hamcrest.CoreMatchers.containsString;
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 public class SwaggerUiConfigurationTest extends AbstractBusClientServerTestBase {
@@ -57,7 +58,7 @@ public class SwaggerUiConfigurationTest extends AbstractBusClientServerTestBase
             sf.setProvider(new JacksonJsonProvider());
             final Swagger2Feature feature = new Swagger2Feature();
             feature.setRunAsFilter(false);
-            feature.setSwaggerUiConfig(new SwaggerUiConfig().url("/swagger.json"));
+            feature.setSwaggerUiConfig(new SwaggerUiConfig().url("/swagger.json").queryConfigEnabled(false));
             sf.setFeatures(Arrays.asList(feature));
             sf.setAddress("http://localhost:" + PORT + "/");
             sf.create();
@@ -89,7 +90,7 @@ public class SwaggerUiConfigurationTest extends AbstractBusClientServerTestBase
         // Test that Swagger UI resources do not interfere with
         // application-specific ones and are accessible.
         final String url = "http://localhost:" + getPort() + "/api-docs";
-
+        
         WebClient uiClient = WebClient
             .create(url)
             .accept("*/*");
@@ -105,9 +106,9 @@ public class SwaggerUiConfigurationTest extends AbstractBusClientServerTestBase
         // Test that Swagger UI resources do not interfere with
         // application-specific ones and are accessible.
         WebClient uiClient = WebClient
-            .create("http://localhost:" + getPort() + "/api-docs")
-            .query("url", "/swagger.json")
-            .accept("*/*");
+                .create("http://localhost:" + getPort() + "/api-docs")
+                .query("url", "/swagger.json")
+                .accept("*/*");
 
         try (Response response = uiClient.get()) {
             String html = response.readEntity(String.class);
@@ -132,6 +133,23 @@ public class SwaggerUiConfigurationTest extends AbstractBusClientServerTestBase
         }
     }
 
+    @Test
+    public void testUiRootResourceReplacesUrlAsConfigured() {
+        // With query config disabled or unset, we replace the url value in the Swagger resource with the one
+        // configured in SwaggerUiConfig, and ignore the one in query parameter.
+        WebClient uiClient = WebClient
+                .create("http://localhost:" + getPort() + "/api-docs")
+                .path("/swagger-initializer.js")
+                .query("another-swagger.json")
+                .accept("*/*");
+
+        try (Response response = uiClient.get()) {
+            String jsCode = response.readEntity(String.class);
+            assertTrue(jsCode.contains("url: \"/swagger.json\""));
+            assertFalse(jsCode.contains("another-swagger.json"));
+        }
+    }
+
     public static String getPort() {
         return PORT;
     }