cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject [cxf] branch master updated: Fixed overrides of the OpenAPI properties from OpenApiCustomizer. Added more test cases, fixed a couple of issues with OpenApiParseUtils
Date Thu, 17 May 2018 02:11:51 GMT
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 ea00423  Fixed overrides of the OpenAPI properties from OpenApiCustomizer. Added
more test cases, fixed a couple of issues with OpenApiParseUtils
ea00423 is described below

commit ea0042333d2047cccb647c7d44ca7ebaf4d9bcd7
Author: reta <drreta@gmail.com>
AuthorDate: Wed May 16 22:07:15 2018 -0400

    Fixed overrides of the OpenAPI properties from OpenApiCustomizer. Added more test cases,
fixed a couple of issues with OpenApiParseUtils
---
 .../jaxrs/openapi/OpenApiCustomizedResource.java   |  45 ++++++-
 .../cxf/jaxrs/openapi/parse/OpenApiParseUtils.java |  13 +-
 .../AbstractOpenApiServiceDescriptionTest.java     |  41 ++++++-
 .../description/openapi/BookStoreOpenApi.java      |   2 +-
 .../openapi/OpenApiCustomizerSubclassTest.java     | 131 +++++++++++++++++++++
 5 files changed, 217 insertions(+), 15 deletions(-)

diff --git a/rt/rs/description-openapi-v3/src/main/java/org/apache/cxf/jaxrs/openapi/OpenApiCustomizedResource.java
b/rt/rs/description-openapi-v3/src/main/java/org/apache/cxf/jaxrs/openapi/OpenApiCustomizedResource.java
index 97931ae..a626c24 100644
--- a/rt/rs/description-openapi-v3/src/main/java/org/apache/cxf/jaxrs/openapi/OpenApiCustomizedResource.java
+++ b/rt/rs/description-openapi-v3/src/main/java/org/apache/cxf/jaxrs/openapi/OpenApiCustomizedResource.java
@@ -18,6 +18,8 @@
  */
 package org.apache.cxf.jaxrs.openapi;
 
+import java.util.Objects;
+
 import javax.servlet.ServletConfig;
 import javax.ws.rs.GET;
 import javax.ws.rs.PathParam;
@@ -35,6 +37,7 @@ import io.swagger.v3.oas.integration.GenericOpenApiContext;
 import io.swagger.v3.oas.integration.OpenApiContextLocator;
 import io.swagger.v3.oas.integration.api.OpenAPIConfiguration;
 import io.swagger.v3.oas.integration.api.OpenApiContext;
+import io.swagger.v3.oas.models.OpenAPI;
 
 public class OpenApiCustomizedResource extends OpenApiResource {
 
@@ -58,14 +61,50 @@ public class OpenApiCustomizedResource extends OpenApiResource {
             // changes won't be taken into account (due to the deep copying rather than reference

             // passing). In order to reflect any changes which customization may do, we have
to 
             // update reader's configuration directly.
-            final String ctxId = ServletConfigContextUtils.getContextIdFromServletConfig(config);
-            final OpenApiContext ctx = OpenApiContextLocator.getInstance().getOpenApiContext(ctxId);
+            OpenApiContext ctx = getOpenApiContext(config);
+            if (ctx == null) {
+                // If there is no context associated with the servlet config, let us
+                // try to fallback to default one. 
+                ctx = getOpenApiContext(null);
+            }
+            
             if (ctx instanceof GenericOpenApiContext<?>) {
                 ((GenericOpenApiContext<?>) ctx).getOpenApiReader().setConfiguration(configuration);
-                customizer.customize(ctx.read());
+                
+                final OpenAPI oas = ctx.read();
+                customizer.customize(oas);
+                
+                if (!Objects.equals(configuration.getOpenAPI().getInfo(), oas.getInfo()))
{
+                    configuration.getOpenAPI().setInfo(oas.getInfo());
+                }
+                
+                if (!Objects.equals(configuration.getOpenAPI().getComponents(), oas.getComponents()))
{
+                    configuration.getOpenAPI().setComponents(oas.getComponents());
+                }
+                
+                if (!Objects.equals(configuration.getOpenAPI().getExternalDocs(), oas.getExternalDocs()))
{
+                    configuration.getOpenAPI().setExternalDocs(oas.getExternalDocs());
+                }
+                
+                if (!Objects.equals(configuration.getOpenAPI().getPaths(), oas.getPaths()))
{
+                    configuration.getOpenAPI().setPaths(oas.getPaths());
+                }
+                
+                if (!Objects.equals(configuration.getOpenAPI().getTags(), oas.getTags()))
{
+                    configuration.getOpenAPI().setTags(oas.getTags());
+                }
+                
+                if (!Objects.equals(configuration.getOpenAPI().getExtensions(), oas.getExtensions()))
{
+                    configuration.getOpenAPI().setExtensions(oas.getExtensions());
+                }
             }
         }
 
         return super.getOpenApi(headers, uriInfo, type);
     }
+
+    private OpenApiContext getOpenApiContext(ServletConfig config) {
+        final String ctxId = ServletConfigContextUtils.getContextIdFromServletConfig(config);
+        return OpenApiContextLocator.getInstance().getOpenApiContext(ctxId);
+    }
 }
diff --git a/rt/rs/description-openapi-v3/src/main/java/org/apache/cxf/jaxrs/openapi/parse/OpenApiParseUtils.java
b/rt/rs/description-openapi-v3/src/main/java/org/apache/cxf/jaxrs/openapi/parse/OpenApiParseUtils.java
index 985bd53..63d5e60 100644
--- a/rt/rs/description-openapi-v3/src/main/java/org/apache/cxf/jaxrs/openapi/parse/OpenApiParseUtils.java
+++ b/rt/rs/description-openapi-v3/src/main/java/org/apache/cxf/jaxrs/openapi/parse/OpenApiParseUtils.java
@@ -203,6 +203,7 @@ public final class OpenApiParseUtils {
                     opTags = Collections.singletonList("");
                 }
                 for (String opTag : opTags) {
+                    userOpsMap.putIfAbsent(opTag, new LinkedList<UserOperation>());
                     userOpsMap.get(opTag).add(userOp);
                 }
 
@@ -212,11 +213,13 @@ public final class OpenApiParseUtils {
         List<UserResource> resources = new LinkedList<UserResource>();
 
         for (Map.Entry<String, List<UserOperation>> entry : userOpsMap.entrySet())
{
-            UserResource ur = new UserResource();
-            ur.setPath("/");
-            ur.setOperations(entry.getValue());
-            ur.setName(entry.getKey());
-            resources.add(ur);
+            if (!entry.getValue().isEmpty()) {
+                UserResource ur = new UserResource();
+                ur.setPath("/");
+                ur.setOperations(entry.getValue());
+                ur.setName(entry.getKey());
+                resources.add(ur);
+            }
         }
 
         app.setResources(resources);
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/openapi/AbstractOpenApiServiceDescriptionTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/openapi/AbstractOpenApiServiceDescriptionTest.java
index d81ff8a..43ef918 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/openapi/AbstractOpenApiServiceDescriptionTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/openapi/AbstractOpenApiServiceDescriptionTest.java
@@ -178,16 +178,45 @@ public abstract class AbstractOpenApiServiceDescriptionTest extends
AbstractBusC
             assertEquals(1, delOpParams.size());
             assertEquals(ParameterType.PATH, delOpParams.get(0).getType());
 
-            assertThat(swaggerJson, CoreMatchers.containsString(CONTACT));
-            assertThat(swaggerJson, CoreMatchers.containsString(TITLE));
-            assertThat(swaggerJson, CoreMatchers.containsString(DESCRIPTION));
-            assertThat(swaggerJson, CoreMatchers.containsString(LICENSE));
-            assertThat(swaggerJson, CoreMatchers.containsString(LICENSE_URL));
-            assertThat(swaggerJson, CoreMatchers.containsString(SECURITY_DEFINITION_NAME));
+            assertThat(swaggerJson, CoreMatchers.containsString(getContract()));
+            assertThat(swaggerJson, CoreMatchers.containsString(getTitle()));
+            assertThat(swaggerJson, CoreMatchers.containsString(getDescription()));
+            assertThat(swaggerJson, CoreMatchers.containsString(getLicense()));
+            assertThat(swaggerJson, CoreMatchers.containsString(getLicenseUrl()));
+            assertThat(swaggerJson, CoreMatchers.containsString(getSecurityDefinitionName()));
+            assertThat(swaggerJson, CoreMatchers.containsString(getTags()));
         } finally {
             client.close();
         }
     }
+    
+    protected String getTags() {
+        return "";
+    }
+    
+    protected String getSecurityDefinitionName() {
+        return SECURITY_DEFINITION_NAME;
+    }
+
+    protected String getLicenseUrl() {
+        return LICENSE_URL;
+    }
+
+    protected String getLicense() {
+        return LICENSE;
+    }
+
+    protected String getDescription() {
+        return DESCRIPTION;
+    }
+
+    protected String getTitle() {
+        return TITLE;
+    }
+
+    protected String getContract() {
+        return CONTACT;
+    }
 
     @Test
     public void testNonUiResource() {
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/openapi/BookStoreOpenApi.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/openapi/BookStoreOpenApi.java
index 44d59ca..3ae4356 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/openapi/BookStoreOpenApi.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/openapi/BookStoreOpenApi.java
@@ -84,7 +84,7 @@ public class BookStoreOpenApi {
     @Path("/{id}")
     @DELETE
     @Operation(
-        description = "Get book by Id",
+        description = "Delete book by Id",
         responses = @ApiResponse(responseCode = "200")
     )
     public Response delete(@Parameter(required = true) @PathParam("id") String id) {
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/openapi/OpenApiCustomizerSubclassTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/openapi/OpenApiCustomizerSubclassTest.java
new file mode 100644
index 0000000..633dfc5
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/openapi/OpenApiCustomizerSubclassTest.java
@@ -0,0 +1,131 @@
+/**
+ * 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 java.util.Arrays;
+import java.util.Collections;
+
+import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
+
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
+import org.apache.cxf.jaxrs.model.doc.JavaDocProvider;
+import org.apache.cxf.jaxrs.openapi.OpenApiCustomizer;
+import org.apache.cxf.jaxrs.openapi.OpenApiFeature;
+
+import io.swagger.v3.oas.models.security.SecurityScheme;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class OpenApiCustomizerSubclassTest extends AbstractOpenApiServiceDescriptionTest
{
+    private static final String PORT = allocatePort(OpenApiCustomizerSubclassTest.class);
+
+    public static class OpenApiRegular extends Server {
+        public OpenApiRegular() {
+            super(PORT, false);
+        }
+
+        public static void main(String[] args) {
+            start(new OpenApiRegular());
+        }
+        
+        @Override
+        protected void run() {
+            final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
+            sf.setResourceClasses(BookStoreOpenApi.class);
+            sf.setResourceClasses(BookStoreStylesheetsOpenApi.class);
+            sf.setResourceProvider(BookStoreOpenApi.class,
+                new SingletonResourceProvider(new BookStoreOpenApi()));
+            sf.setProvider(new JacksonJsonProvider());
+            final OpenApiFeature feature = createOpenApiFeature();
+            sf.setFeatures(Arrays.asList(feature));
+            sf.setAddress("http://localhost:" + port + "/");
+            sf.create();
+        }
+
+        
+        @Override
+        protected OpenApiFeature createOpenApiFeature() {
+            final OpenApiCustomizer customizer = new OpenApiCustomizer() {
+                public void customize(io.swagger.v3.oas.models.OpenAPI oas) {
+                    super.customize(oas);
+                    oas.getInfo().setDescription("Custom Description");
+                    oas.getInfo().getLicense().setName("Custom License");
+                    oas.getComponents().getSecuritySchemes().put("openid", new SecurityScheme());
+                }
+            };
+            
+            customizer.setJavadocProvider(new JavaDocProvider());
+            customizer.setDynamicBasePath(true);
+            customizer.setReplaceTags(true);
+            
+            final OpenApiFeature feature = super.createOpenApiFeature();
+            feature.setCustomizer(customizer);
+            feature.setScan(false);
+            feature.setResourcePackages(Collections.singleton(getClass().getPackage().getName()));
+
+            return feature;
+        }
+    }
+    
+    @Override
+    protected String getDescription() {
+        return "Custom Description";
+    }
+
+    @Override
+    protected String getLicense() {
+        return "Custom License";
+    }
+    
+    @Override
+    protected String getSecurityDefinitionName() {
+        return "openid";
+    }
+
+    @Override
+    protected String getTags() {
+        return "_bookstore";
+    }
+
+    @BeforeClass
+    public static void startServers() throws Exception {
+        startServers(OpenApiRegular.class);
+    }
+
+    @Override
+    protected String getPort() {
+        return PORT;
+    }
+
+    @Override
+    protected String getBaseUrl() {
+        return "http://localhost:" + getPort() + getApplicationPath();
+    }
+
+    protected String getApplicationPath() {
+        return "";
+    }
+
+    @Test
+    public void testApiListingIsProperlyReturnedJSON() throws Exception {
+        doTestApiListingIsProperlyReturnedJSON(false, "http://localhost:" + getPort());
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
reta@apache.org.

Mime
View raw message