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

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


The following commit(s) were added to refs/heads/master by this push:
     new d30a3f6  CXF-7728: Scan for OpenAPI config files by default
d30a3f6 is described below

commit d30a3f665841cdc721103040520d49d73ba1a5e0
Author: reta <[email protected]>
AuthorDate: Sun May 20 09:20:24 2018 -0400

    CXF-7728: Scan for OpenAPI config files by default
---
 .../OpenApiDefaultConfigurationScanner.java        | 70 +++++++++++++++++
 .../apache/cxf/jaxrs/openapi/OpenApiFeature.java   | 20 ++++-
 .../description/openapi/OpenApiPropertiesTest.java | 87 ++++++++++++++++++++++
 .../resources/files/openapi-configuration.json     | 26 +++++++
 4 files changed, 201 insertions(+), 2 deletions(-)

diff --git 
a/rt/rs/description-openapi-v3/src/main/java/org/apache/cxf/jaxrs/openapi/OpenApiDefaultConfigurationScanner.java
 
b/rt/rs/description-openapi-v3/src/main/java/org/apache/cxf/jaxrs/openapi/OpenApiDefaultConfigurationScanner.java
new file mode 100644
index 0000000..5f50032
--- /dev/null
+++ 
b/rt/rs/description-openapi-v3/src/main/java/org/apache/cxf/jaxrs/openapi/OpenApiDefaultConfigurationScanner.java
@@ -0,0 +1,70 @@
+/**
+ * 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.openapi;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import org.apache.commons.lang3.tuple.ImmutablePair;
+
+import io.swagger.v3.oas.integration.ClasspathOpenApiConfigurationLoader;
+import io.swagger.v3.oas.integration.FileOpenApiConfigurationLoader;
+import io.swagger.v3.oas.integration.api.OpenApiConfigurationLoader;
+
+/**
+ * Scans a set of known configuration locations in order to locate the OpenAPI 
+ * configuration. 
+ */
+public final class OpenApiDefaultConfigurationScanner {
+    private static final Map<String, OpenApiConfigurationLoader> LOADERS = 
getLocationLoaders();
+    
+    private static final List<ImmutablePair<String, String>> KNOWN_LOCATIONS = 
Arrays
+        .asList(
+            new ImmutablePair<>("classpath", "openapi-configuration.yaml"),
+            new ImmutablePair<>("classpath", "openapi-configuration.json"),
+            new ImmutablePair<>("classpath", "openapi.yaml"),
+            new ImmutablePair<>("classpath", "openapi.json"),
+            new ImmutablePair<>("file", "openapi-configuration.yaml"),
+            new ImmutablePair<>("file", "openapi-configuration.json"),
+            new ImmutablePair<>("file", "openapi.yaml"),
+            new ImmutablePair<>("file", "openapi.json")
+        );
+    
+    private OpenApiDefaultConfigurationScanner() {
+    }
+
+    private static Map<String, OpenApiConfigurationLoader> 
getLocationLoaders() {
+        final Map<String, OpenApiConfigurationLoader> map = new HashMap<>();
+        map.put("classpath", new ClasspathOpenApiConfigurationLoader());
+        map.put("file", new FileOpenApiConfigurationLoader());
+        return map;
+    }
+
+    public static Optional<String> locateDefaultConfiguration() {
+        return KNOWN_LOCATIONS
+            .stream()
+            .filter(location -> 
LOADERS.get(location.left).exists(location.right))
+            .findFirst()
+            .map(ImmutablePair::getValue);
+    }
+}
diff --git 
a/rt/rs/description-openapi-v3/src/main/java/org/apache/cxf/jaxrs/openapi/OpenApiFeature.java
 
b/rt/rs/description-openapi-v3/src/main/java/org/apache/cxf/jaxrs/openapi/OpenApiFeature.java
index 68f837c..8df6c7d 100644
--- 
a/rt/rs/description-openapi-v3/src/main/java/org/apache/cxf/jaxrs/openapi/OpenApiFeature.java
+++ 
b/rt/rs/description-openapi-v3/src/main/java/org/apache/cxf/jaxrs/openapi/OpenApiFeature.java
@@ -99,6 +99,8 @@ public class OpenApiFeature extends AbstractFeature 
implements SwaggerUiSupport,
     private String configLocation;
     // Allows to pass the properties location, by default swagger.properties
     private String propertiesLocation = DEFAULT_PROPS_LOCATION;
+    // Allows to disable automatic scan of known configuration locations 
(enabled by default)
+    private boolean scanKnownConfigLocations = true;
 
     protected static class DefaultApplication extends Application {
 
@@ -138,7 +140,13 @@ public class OpenApiFeature extends AbstractFeature 
implements SwaggerUiSupport,
         Properties swaggerProps = null;
         GenericOpenApiContextBuilder<?> openApiConfiguration; 
         final Application application = getApplicationOrDefault(server, 
factory, sfb, bus);
-        if (StringUtils.isEmpty(getConfigLocation())) {
+        
+        String defaultConfigLocation = getConfigLocation();
+        if (scanKnownConfigLocations && 
StringUtils.isEmpty(defaultConfigLocation)) {
+            defaultConfigLocation = 
OpenApiDefaultConfigurationScanner.locateDefaultConfiguration().orElse(null);
+        }
+        
+        if (StringUtils.isEmpty(defaultConfigLocation)) {
             swaggerProps = getSwaggerProperties(propertiesLocation, bus);
             
             if (isScan()) {
@@ -163,7 +171,7 @@ public class OpenApiFeature extends AbstractFeature 
implements SwaggerUiSupport,
         } else {
             openApiConfiguration = new JaxrsOpenApiContextBuilder<>()
                 .application(application)
-                .configLocation(getConfigLocation());
+                .configLocation(defaultConfigLocation);
         }
 
         try {
@@ -385,6 +393,14 @@ public class OpenApiFeature extends AbstractFeature 
implements SwaggerUiSupport,
     public void setCustomizer(OpenApiCustomizer customizer) {
         this.customizer = customizer;
     }
+    
+    public void setScanKnownConfigLocations(boolean scanKnownConfigLocations) {
+        this.scanKnownConfigLocations = scanKnownConfigLocations;
+    }
+    
+    public boolean isScanKnownConfigLocations() {
+        return scanKnownConfigLocations;
+    }
 
     @Override
     public String findSwaggerUiRoot() {
diff --git 
a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/openapi/OpenApiPropertiesTest.java
 
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/openapi/OpenApiPropertiesTest.java
new file mode 100644
index 0000000..4f6ba43
--- /dev/null
+++ 
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/openapi/OpenApiPropertiesTest.java
@@ -0,0 +1,87 @@
+/**
+ * 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.systest.jaxrs.description.openapi;
+
+import org.apache.cxf.jaxrs.openapi.OpenApiFeature;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+
+public class OpenApiPropertiesTest extends 
AbstractOpenApiServiceDescriptionTest {
+    private static final String PORT = 
allocatePort(OpenApiPropertiesTest.class);
+
+    public static class OpenApiRegular extends Server {
+        public OpenApiRegular() {
+            super(PORT, false);
+        }
+
+        public static void main(String[] args) {
+            start(new OpenApiRegular());
+        }
+        
+        @Override
+        protected OpenApiFeature createOpenApiFeature() {
+            final OpenApiFeature feature = new OpenApiFeature();
+            feature.setRunAsFilter(runAsFilter);
+            feature.setConfigLocation("/files/openapi-configuration.json");
+            return feature;
+        }
+    }
+
+    @BeforeClass
+    public static void startServers() throws Exception {
+        startServers(OpenApiRegular.class);
+    }
+    
+    @Override
+    protected String getContract() {
+        return "[email protected]";
+    }
+    
+    @Override
+    protected String getDescription() {
+        return "A sample Books API";
+    }
+    
+    @Override
+    protected String getTitle() {
+        return "Books API";
+    }
+    
+    @Override
+    protected String getLicense() {
+        return "Apache 2.0";
+    }
+    
+    @Override
+    protected String getLicenseUrl() {
+        return "http://www.apache.org/licenses/LICENSE-2.0.html";;
+    }
+
+    @Override
+    protected String getPort() {
+        return PORT;
+    }
+
+    @Test
+    public void testApiListingIsProperlyReturnedJSON() throws Exception {
+        doTestApiListingIsProperlyReturnedJSON();
+    }
+}
diff --git a/systests/jaxrs/src/test/resources/files/openapi-configuration.json 
b/systests/jaxrs/src/test/resources/files/openapi-configuration.json
new file mode 100644
index 0000000..019eef9
--- /dev/null
+++ b/systests/jaxrs/src/test/resources/files/openapi-configuration.json
@@ -0,0 +1,26 @@
+{
+    "resourcePackages": [
+        "org.apache.cxf.systest.jaxrs.description.openapi"
+    ],
+    "prettyPrint" : true,
+    "cacheTTL": 0,
+    "openAPI": {
+        "info": {
+            "version": "1.0.0",
+            "title": "Books API",
+            "description": "A sample Books API",
+            "contact": {
+                "email": "[email protected]"
+            },
+            "license": {
+                "name": "Apache 2.0",
+                "url": "http://www.apache.org/licenses/LICENSE-2.0.html";
+            }
+        },        
+        "components": {
+            "securitySchemes" : { 
+                "basicAuth": { "type": "http" } 
+            }
+        }
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
[email protected].

Reply via email to