You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2017/12/07 10:41:01 UTC

[cxf] branch master updated: [CXF-7525] Changing the package for open api converters, starting with a system test to be completed shortly

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

sergeyb 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 2b9c671  [CXF-7525] Changing the package for open api converters, starting with a system test to be completed shortly
2b9c671 is described below

commit 2b9c671a1aca1c8514f3c4935ec06145ea6e7cef
Author: Sergey Beryozkin <sb...@gmail.com>
AuthorDate: Wed Dec 6 17:05:35 2017 +0000

    [CXF-7525] Changing the package for open api converters, starting with a system test to be completed shortly
---
 .../swagger/openapi/SwaggerOpenApiFilter.java      |  56 +++++++++++
 .../{parse => openapi}/SwaggerOpenApiUtils.java    |   4 +-
 .../SwaggerOpenApiUtilsTest.java                   |   2 +-
 .../description/OpenApiServiceDescriptionTest.java | 108 +++++++++++++++++++++
 4 files changed, 167 insertions(+), 3 deletions(-)

diff --git a/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/openapi/SwaggerOpenApiFilter.java b/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/openapi/SwaggerOpenApiFilter.java
new file mode 100644
index 0000000..8a60e8a
--- /dev/null
+++ b/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/openapi/SwaggerOpenApiFilter.java
@@ -0,0 +1,56 @@
+/**
+ * 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.openapi;
+
+import java.io.IOException;
+import java.net.URI;
+
+import javax.ws.rs.container.ContainerRequestContext;
+import javax.ws.rs.container.ContainerRequestFilter;
+import javax.ws.rs.container.ContainerResponseContext;
+import javax.ws.rs.container.ContainerResponseFilter;
+import javax.ws.rs.container.PreMatching;
+import javax.ws.rs.ext.Provider;
+
+@Provider
+@PreMatching
+public final class SwaggerOpenApiFilter implements ContainerRequestFilter, ContainerResponseFilter {
+
+    private static final String SWAGGER_PATH = "swagger.json";
+    private static final String OPEN_API_PATH = "openapi.json";
+    private static final String OPEN_API_PROPERTY = "openapi";
+    @Override
+    public void filter(ContainerRequestContext reqCtx) throws IOException {
+        String path = reqCtx.getUriInfo().getPath();
+        if (path.endsWith(OPEN_API_PATH)) {
+            reqCtx.setRequestUri(URI.create(SWAGGER_PATH));
+            reqCtx.setProperty(OPEN_API_PROPERTY, Boolean.TRUE);
+        }
+        
+    }
+    
+    @Override
+    public void filter(ContainerRequestContext reqCtx, ContainerResponseContext respCtx) throws IOException {
+        if (Boolean.TRUE == reqCtx.getProperty(OPEN_API_PROPERTY)) {
+            String swaggerJson = (String)respCtx.getEntity();
+            String openApiJson = SwaggerOpenApiUtils.getOpenApiFromSwaggerJson(swaggerJson);
+            respCtx.setEntity(openApiJson);
+        }
+    }
+}
diff --git a/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/parse/SwaggerOpenApiUtils.java b/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/openapi/SwaggerOpenApiUtils.java
similarity index 99%
rename from rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/parse/SwaggerOpenApiUtils.java
rename to rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/openapi/SwaggerOpenApiUtils.java
index 3619499..13d2ced 100644
--- a/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/parse/SwaggerOpenApiUtils.java
+++ b/rt/rs/description-swagger/src/main/java/org/apache/cxf/jaxrs/swagger/openapi/SwaggerOpenApiUtils.java
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.cxf.jaxrs.swagger.parse;
+package org.apache.cxf.jaxrs.swagger.openapi;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -42,7 +42,7 @@ public final class SwaggerOpenApiUtils {
     private static final Logger LOG = LogUtils.getL7dLogger(SwaggerOpenApiUtils.class);
     
     private static final List<String> SIMPLE_TYPE_RELATED_PROPS =
-        Arrays.asList("format", "minimum", "maximum"); 
+        Arrays.asList("format", "minimum", "maximum", "default"); 
     
     private SwaggerOpenApiUtils() {
         
diff --git a/rt/rs/description-swagger/src/test/java/org/apache/cxf/jaxrs/swagger/parse/SwaggerOpenApiUtilsTest.java b/rt/rs/description-swagger/src/test/java/org/apache/cxf/jaxrs/swagger/openapi/SwaggerOpenApiUtilsTest.java
similarity index 99%
rename from rt/rs/description-swagger/src/test/java/org/apache/cxf/jaxrs/swagger/parse/SwaggerOpenApiUtilsTest.java
rename to rt/rs/description-swagger/src/test/java/org/apache/cxf/jaxrs/swagger/openapi/SwaggerOpenApiUtilsTest.java
index 15cfb0f..a23c2a3 100644
--- a/rt/rs/description-swagger/src/test/java/org/apache/cxf/jaxrs/swagger/parse/SwaggerOpenApiUtilsTest.java
+++ b/rt/rs/description-swagger/src/test/java/org/apache/cxf/jaxrs/swagger/openapi/SwaggerOpenApiUtilsTest.java
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.cxf.jaxrs.swagger.parse;
+package org.apache.cxf.jaxrs.swagger.openapi;
 
 import java.util.List;
 import java.util.Map;
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/OpenApiServiceDescriptionTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/OpenApiServiceDescriptionTest.java
new file mode 100644
index 0000000..08b7d71
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/OpenApiServiceDescriptionTest.java
@@ -0,0 +1,108 @@
+/**
+ * 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;
+
+import java.util.Arrays;
+import java.util.Collections;
+
+import javax.ws.rs.core.MediaType;
+
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
+import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
+import org.apache.cxf.jaxrs.swagger.Swagger2Feature;
+import org.apache.cxf.jaxrs.swagger.openapi.SwaggerOpenApiFilter;
+import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+
+public class OpenApiServiceDescriptionTest extends AbstractBusClientServerTestBase {
+    static final String PORT = allocatePort(OpenApiServiceDescriptionTest.class);
+    static final String SECURITY_DEFINITION_NAME = "basicAuth";
+    
+    private static final String CONTACT = "cxf@apache.org";
+    private static final String TITLE = "CXF unittest";
+    private static final String DESCRIPTION = "API Description";
+    private static final String LICENSE = "API License";
+    private static final String LICENSE_URL = "API License URL";
+    
+    @Ignore
+    public static class Server extends AbstractBusTestServerBase {
+        @Override
+        protected void run() {
+            final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
+            sf.setResourceClasses(BookStoreSwagger2.class);
+            sf.setResourceProvider(BookStoreSwagger2.class,
+                new SingletonResourceProvider(new BookStoreSwagger2()));
+            sf.setProvider(new SwaggerOpenApiFilter());
+            final Swagger2Feature feature = createSwagger2Feature();
+            sf.setFeatures(Arrays.asList(feature));
+            sf.setAddress("http://localhost:" + PORT + "/");
+            sf.create();
+        }
+        
+        protected Swagger2Feature createSwagger2Feature() {
+            final Swagger2Feature feature = new Swagger2Feature();
+            feature.setContact(CONTACT);
+            feature.setTitle(TITLE);
+            feature.setDescription(DESCRIPTION);
+            feature.setLicense(LICENSE);
+            feature.setLicenseUrl(LICENSE_URL);
+            feature.setSecurityDefinitions(Collections.singletonMap(SECURITY_DEFINITION_NAME,
+               new io.swagger.models.auth.BasicAuthDefinition()));
+            return feature;
+        }
+
+        protected static void start(final Server s) {
+            try {
+                s.start();
+            } catch (Exception ex) {
+                ex.printStackTrace();
+                System.exit(-1);
+            } finally {
+                System.out.println("done!");
+            }
+        }
+    }
+
+    @BeforeClass
+    public static void startServers() throws Exception {
+        AbstractResourceInfo.clearAllMaps();
+        assertTrue("server did not launch correctly",
+                   launchServer(Server.class, true));
+        createStaticBus();
+    }
+
+   
+    @Test
+    public void testOpenApiJSON() throws Exception {    
+        final WebClient client = createWebClient("/openapi.json");
+        client.get(String.class);
+    }
+    protected WebClient createWebClient(final String url) {
+        WebClient wc = WebClient.create("http://localhost:" + PORT + url)
+            .accept(MediaType.APPLICATION_JSON);
+        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000L);
+        return wc;
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
['"commits@cxf.apache.org" <co...@cxf.apache.org>'].