You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by ff...@apache.org on 2021/10/13 18:29:23 UTC

[cxf] branch master updated: CXF-8607 OpenApiFeature: OpenApiCustomizer doesn´t work with runAsFilter

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

ffang 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 0c02781  CXF-8607 OpenApiFeature: OpenApiCustomizer doesn´t work with runAsFilter
     new b2731a6  Merge pull request #864 from DevFlorian/master
0c02781 is described below

commit 0c02781a0df19ab29e871f16a0943755cd2afb9f
Author: FWermelskirchen <fw...@eitco.de>
AuthorDate: Wed Oct 13 14:08:13 2021 +0200

    CXF-8607 OpenApiFeature: OpenApiCustomizer doesn´t work with runAsFilter
---
 .../apache/cxf/jaxrs/openapi/OpenApiFeature.java   | 56 ++++++++++++++++++++--
 1 file changed, 53 insertions(+), 3 deletions(-)

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 4f51d87..ddac359 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
@@ -496,7 +496,7 @@ public class OpenApiFeature extends DelegatingFeature<OpenApiFeature.Portable>
                                 .getUserDefinedOptions());
                 registerOpenApiResources(sfb, packages, context.getOpenApiConfiguration());
                 registerSwaggerUiResources(sfb, combine(swaggerProps, userProperties), factory, bus);
-                registerSwaggerContainerRequestFilter(factory, application);
+                registerSwaggerContainerRequestFilter(factory, application, context.getOpenApiConfiguration());
 
                 if (useContextBasedConfig) {
                     registerServletConfigProvider(factory);
@@ -512,10 +512,13 @@ public class OpenApiFeature extends DelegatingFeature<OpenApiFeature.Portable>
             }
         }
 
-        private void registerSwaggerContainerRequestFilter(ServerProviderFactory factory, Application application) {
+        private void registerSwaggerContainerRequestFilter(ServerProviderFactory factory, Application application,
+                                                           OpenAPIConfiguration config) {
             if (isRunAsFilter()) {
                 List<Object> providers = new ArrayList<>();
-                providers.add(new SwaggerContainerRequestFilter(application));
+                BaseOpenApiResource filter = createOpenApiRequestFilter(application).openApiConfiguration(config)
+                        .configLocation(configLocation);
+                providers.add(filter);
                 factory.setUserProviders(providers);
             }
             
@@ -914,6 +917,11 @@ public class OpenApiFeature extends DelegatingFeature<OpenApiFeature.Portable>
         private BaseOpenApiResource createOpenApiResource() {
             return (customizer == null) ? new OpenApiResource() : new OpenApiCustomizedResource(customizer);
         }
+
+        private BaseOpenApiResource createOpenApiRequestFilter(Application application) {
+            return (customizer == null) ? new SwaggerContainerRequestFilter(application)
+                    : new CustomizedSwaggerContainerRequestFilter(application, customizer);
+        }
     }
     
     @PreMatching
@@ -955,4 +963,46 @@ public class OpenApiFeature extends DelegatingFeature<OpenApiFeature.Portable>
             }
         }
     }
+
+    @PreMatching
+    protected static class CustomizedSwaggerContainerRequestFilter extends OpenApiCustomizedResource
+            implements ContainerRequestFilter {
+
+        protected static final String APIDOCS_LISTING_PATH_JSON = "openapi.json";
+        protected static final String APIDOCS_LISTING_PATH_YAML = "openapi.yaml";
+
+
+        @Context
+        protected MessageContext mc;
+
+        private Application app;
+        public CustomizedSwaggerContainerRequestFilter(Application app, OpenApiCustomizer customizer) {
+            super(customizer);
+            this.app = app;
+        }
+
+        @Override
+        public void filter(ContainerRequestContext requestContext) throws IOException {
+            UriInfo ui = mc.getUriInfo();
+
+            Response response = null;
+            if (ui.getPath().endsWith(APIDOCS_LISTING_PATH_JSON)) {
+                try {
+                    response = super.getOpenApi(app, mc.getServletConfig(), mc.getHttpHeaders(), ui, "json");
+                } catch (Exception ex) {
+                    throw new IOException(ex);
+                }
+            } else if (ui.getPath().endsWith(APIDOCS_LISTING_PATH_YAML)) {
+                try {
+                    response = super.getOpenApi(app, mc.getServletConfig(), mc.getHttpHeaders(), ui, "yaml");
+                } catch (Exception ex) {
+                    throw new IOException(ex);
+                }
+            }
+
+            if (response != null) {
+                requestContext.abortWith(response);
+            }
+        }
+    }
 }