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 2018/10/09 02:02:32 UTC

[cxf] branch 3.2.x-fixes updated: CXF-7853: Spring Boot autoconfiguration should automatically set OpenApiCustomizer if OpenApiFeature is configured

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

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


The following commit(s) were added to refs/heads/3.2.x-fixes by this push:
     new 651497d  CXF-7853: Spring Boot autoconfiguration should automatically set OpenApiCustomizer if OpenApiFeature is configured
651497d is described below

commit 651497d6e2a1413bf3c3fe693b9840496c6d3b0b
Author: reta <dr...@gmail.com>
AuthorDate: Mon Oct 8 21:24:14 2018 -0400

    CXF-7853: Spring Boot autoconfiguration should automatically set OpenApiCustomizer if OpenApiFeature is configured
---
 .../sample/rs/service/SampleRestApplication.java   |  7 +--
 integration/spring-boot/autoconfigure/pom.xml      |  6 +++
 .../openapi/OpenApiAutoConfiguration.java          | 52 +++++++++++++++++++
 .../openapi/OpenApiFeatureBeanPostProcessor.java   | 60 ++++++++++++++++++++++
 .../src/main/resources/META-INF/spring.factories   |  3 +-
 5 files changed, 122 insertions(+), 6 deletions(-)

diff --git a/distribution/src/main/release/samples/jax_rs/spring_boot/src/main/java/sample/rs/service/SampleRestApplication.java b/distribution/src/main/release/samples/jax_rs/spring_boot/src/main/java/sample/rs/service/SampleRestApplication.java
index f2c3449..6f1cef3 100644
--- a/distribution/src/main/release/samples/jax_rs/spring_boot/src/main/java/sample/rs/service/SampleRestApplication.java
+++ b/distribution/src/main/release/samples/jax_rs/spring_boot/src/main/java/sample/rs/service/SampleRestApplication.java
@@ -23,7 +23,6 @@ import org.apache.cxf.Bus;
 import org.apache.cxf.endpoint.Server;
 import org.apache.cxf.ext.logging.LoggingFeature;
 import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
-import org.apache.cxf.jaxrs.openapi.OpenApiCustomizer;
 import org.apache.cxf.jaxrs.openapi.OpenApiFeature;
 import org.apache.cxf.jaxrs.swagger.ui.SwaggerUiConfig;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -53,12 +52,10 @@ public class SampleRestApplication {
         return endpoint.create();
     }
 
+    @Bean
     public OpenApiFeature createOpenApiFeature() {
-        OpenApiFeature openApiFeature = new OpenApiFeature();
+        final OpenApiFeature openApiFeature = new OpenApiFeature();
         openApiFeature.setPrettyPrint(true);
-        OpenApiCustomizer customizer = new OpenApiCustomizer();
-        customizer.setDynamicBasePath(true);
-        openApiFeature.setCustomizer(customizer);
         openApiFeature.setTitle("Spring Boot CXF REST Application");
         openApiFeature.setContactName("The Apache CXF team");
         openApiFeature.setDescription("This sample project demonstrates how to use CXF JAX-RS services"
diff --git a/integration/spring-boot/autoconfigure/pom.xml b/integration/spring-boot/autoconfigure/pom.xml
index 4552a0b..988d5e8 100644
--- a/integration/spring-boot/autoconfigure/pom.xml
+++ b/integration/spring-boot/autoconfigure/pom.xml
@@ -85,6 +85,12 @@
             <artifactId>cxf-rt-frontend-jaxrs</artifactId>
             <version>${project.version}</version>
             <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.cxf</groupId>
+            <artifactId>cxf-rt-rs-service-description-openapi-v3</artifactId>
+            <version>${project.version}</version>
+            <optional>true</optional>
         </dependency> 
         <dependency>
             <groupId>${cxf.servlet-api.group}</groupId>
diff --git a/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/openapi/OpenApiAutoConfiguration.java b/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/openapi/OpenApiAutoConfiguration.java
new file mode 100644
index 0000000..43e6d26
--- /dev/null
+++ b/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/openapi/OpenApiAutoConfiguration.java
@@ -0,0 +1,52 @@
+/**
+ * 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.spring.boot.autoconfigure.openapi;
+
+import org.apache.cxf.jaxrs.openapi.OpenApiCustomizer;
+import org.apache.cxf.jaxrs.openapi.OpenApiFeature;
+import org.apache.cxf.spring.boot.autoconfigure.CxfAutoConfiguration;
+import org.springframework.boot.autoconfigure.AutoConfigureAfter;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * Auto-configure the OpenApiCustomizer in case the Spring Boot application
+ * uses OpenApiFeature. The actual injection happens after OpenApiFeature bean's
+ * initialization phase.
+ *
+ */
+@Configuration
+@ConditionalOnBean(OpenApiFeature.class)
+@AutoConfigureAfter(CxfAutoConfiguration.class)
+public class OpenApiAutoConfiguration {
+    @Bean
+    @ConditionalOnMissingBean
+    public OpenApiCustomizer openApiCustomizer() {
+        final OpenApiCustomizer customizer = new OpenApiCustomizer();
+        customizer.setDynamicBasePath(true);
+        return customizer;
+    }
+    
+    @Bean
+    public OpenApiFeatureBeanPostProcessor openApiFeatureBeanPostProcessor() {
+        return new OpenApiFeatureBeanPostProcessor(); 
+    }
+}
diff --git a/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/openapi/OpenApiFeatureBeanPostProcessor.java b/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/openapi/OpenApiFeatureBeanPostProcessor.java
new file mode 100644
index 0000000..fe47f54
--- /dev/null
+++ b/integration/spring-boot/autoconfigure/src/main/java/org/apache/cxf/spring/boot/autoconfigure/openapi/OpenApiFeatureBeanPostProcessor.java
@@ -0,0 +1,60 @@
+/**
+ * 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.spring.boot.autoconfigure.openapi;
+
+import org.apache.cxf.jaxrs.openapi.OpenApiCustomizer;
+import org.apache.cxf.jaxrs.openapi.OpenApiFeature;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.BeanFactory;
+import org.springframework.beans.factory.BeanFactoryAware;
+import org.springframework.beans.factory.config.BeanPostProcessor;
+
+/**
+ * Post-processes all OpenApiFeature beans in order to inject the instance of
+ * OpenApiCustomizer (if it is not set yet). For Spring / Spring Boot applications
+ * it is what you would need to do manually most of the time.
+ */
+public class OpenApiFeatureBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware {
+    private BeanFactory beanFactory;
+    
+    @Override
+    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
+        return bean;
+    }
+    
+    @Override
+    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
+        if (bean instanceof OpenApiFeature) {
+            final OpenApiFeature feature = (OpenApiFeature)bean;
+            
+            if (feature.getCustomizer() == null) {
+                final OpenApiCustomizer customizer = beanFactory.getBean(OpenApiCustomizer.class);
+                feature.setCustomizer(customizer);
+            }
+            
+        }
+        
+        return bean;
+    }
+
+    @Override
+    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
+        this.beanFactory = beanFactory;
+    }
+}
diff --git a/integration/spring-boot/autoconfigure/src/main/resources/META-INF/spring.factories b/integration/spring-boot/autoconfigure/src/main/resources/META-INF/spring.factories
index 1fc5d7f..b28b7b2 100644
--- a/integration/spring-boot/autoconfigure/src/main/resources/META-INF/spring.factories
+++ b/integration/spring-boot/autoconfigure/src/main/resources/META-INF/spring.factories
@@ -1,3 +1,4 @@
 # Auto Configure
 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
-org.apache.cxf.spring.boot.autoconfigure.CxfAutoConfiguration
+org.apache.cxf.spring.boot.autoconfigure.CxfAutoConfiguration,\
+org.apache.cxf.spring.boot.autoconfigure.openapi.OpenApiAutoConfiguration