cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a.@apache.org
Subject [1/3] cxf git commit: [CXF-6476] add a yaml test and also fix the basePath issue for swagger 2.0
Date Fri, 31 Jul 2015 16:10:34 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes a4a6beac9 -> 48f746362


[CXF-6476] add a yaml test and also fix the basePath issue for swagger 2.0


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/4f478997
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/4f478997
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/4f478997

Branch: refs/heads/3.0.x-fixes
Commit: 4f4789975683d22efb414b8be5ea03170903bd5f
Parents: a4a6bea
Author: Akitoshi Yoshida <ay@apache.org>
Authored: Sat Jul 11 16:09:10 2015 +0200
Committer: Akitoshi Yoshida <ay@apache.org>
Committed: Fri Jul 31 18:08:16 2015 +0200

----------------------------------------------------------------------
 .../jaxrs/swagger/AbstractSwaggerFeature.java   |  5 +-
 .../cxf/jaxrs/swagger/Swagger2Feature.java      | 10 +++
 .../cxf/jaxrs/swagger/SwaggerFeature.java       |  5 ++
 systests/jaxrs/pom.xml                          |  6 ++
 .../AbstractSwagger2ServiceDescriptionTest.java | 21 ++++-
 .../systest/jaxrs/description/swagger2-json.txt |  2 +-
 .../systest/jaxrs/description/swagger2-yaml.txt | 86 ++++++++++++++++++++
 7 files changed, 130 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/4f478997/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java
----------------------------------------------------------------------
diff --git a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java
b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java
index 5b45829..44f5f08 100644
--- a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java
+++ b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/AbstractSwaggerFeature.java
@@ -47,6 +47,8 @@ abstract class AbstractSwaggerFeature extends AbstractFeature {
 
     protected abstract void addSwaggerResource(Server server);
 
+    protected abstract void setBasePathByAddress(String address);
+
     private void calculateDefaultResourcePackage(Server server) {
         JAXRSServiceFactoryBean serviceFactoryBean = 
             (JAXRSServiceFactoryBean)server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName());
@@ -61,9 +63,10 @@ abstract class AbstractSwaggerFeature extends AbstractFeature {
     private void calculateDefaultBasePath(Server server) {
         if (getBasePath() == null || getBasePath().length() == 0) {
             String address = server.getEndpoint().getEndpointInfo().getAddress();
-            setBasePath(address);
+            setBasePathByAddress(address);
         }
     }
+
     public String getResourcePackage() {
         return resourcePackage;
     }

http://git-wip-us.apache.org/repos/asf/cxf/blob/4f478997/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
----------------------------------------------------------------------
diff --git a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
index 9b4bcc9..b5d8f30 100644
--- a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
+++ b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
@@ -19,6 +19,7 @@
 package org.apache.cxf.jaxrs.swagger;
 
 import java.io.IOException;
+import java.net.URI;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -77,6 +78,15 @@ public class Swagger2Feature extends AbstractSwaggerFeature {
         beanConfig.setScan(isScan());
     }
 
+    @Override
+    protected void setBasePathByAddress(String address) {
+        if (!address.startsWith("/")) {
+            // get rid of the path
+            address = URI.create(address).getPath();
+        }
+        setBasePath(address);
+    }
+
     @PreMatching
     private static class SwaggerContainerRequestFilter extends ApiListingResource implements
ContainerRequestFilter {
         private static final String APIDOCS_LISTING_PATH_JSON = "swagger.json";

http://git-wip-us.apache.org/repos/asf/cxf/blob/4f478997/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerFeature.java
----------------------------------------------------------------------
diff --git a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerFeature.java
b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerFeature.java
index b4191e3..e008686 100644
--- a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerFeature.java
+++ b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/SwaggerFeature.java
@@ -73,6 +73,11 @@ public class SwaggerFeature extends AbstractSwaggerFeature {
         beanConfig.setScan(isScan());
     }    
 
+    @Override
+    protected void setBasePathByAddress(String address) {
+        setBasePath(address);
+    }
+
     @PreMatching
     private static class SwaggerContainerRequestFilter implements ContainerRequestFilter
{
         private static final String APIDOCS_LISTING_PATH = "api-docs";

http://git-wip-us.apache.org/repos/asf/cxf/blob/4f478997/systests/jaxrs/pom.xml
----------------------------------------------------------------------
diff --git a/systests/jaxrs/pom.xml b/systests/jaxrs/pom.xml
index 778950d..3b69e4f 100644
--- a/systests/jaxrs/pom.xml
+++ b/systests/jaxrs/pom.xml
@@ -503,6 +503,12 @@
             <scope>test</scope>
         </dependency>
         <dependency>
+           <groupId>org.yaml</groupId>
+           <artifactId>snakeyaml</artifactId>
+           <version>1.15</version>
+           <scope>test</scope>
+        </dependency>
+        <dependency>
             <!-- activate atmosphere. Its use can be disabled by setting
                  org.apache.cxf.transport.websocket.atmosphere.disabled to "true" -->
             <groupId>org.atmosphere</groupId>

http://git-wip-us.apache.org/repos/asf/cxf/blob/4f478997/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/AbstractSwagger2ServiceDescriptionTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/AbstractSwagger2ServiceDescriptionTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/AbstractSwagger2ServiceDescriptionTest.java
index 13be973..285b150 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/AbstractSwagger2ServiceDescriptionTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/AbstractSwagger2ServiceDescriptionTest.java
@@ -40,6 +40,7 @@ import org.junit.Ignore;
 import org.junit.Test;
 
 import org.skyscreamer.jsonassert.JSONAssert;
+import org.yaml.snakeyaml.Yaml;
 
 public abstract class AbstractSwagger2ServiceDescriptionTest extends AbstractBusClientServerTestBase
{
     
@@ -96,18 +97,32 @@ public abstract class AbstractSwagger2ServiceDescriptionTest extends AbstractBus
             assertEquals(Status.OK.getStatusCode(), r.getStatus());
             JSONAssert.assertEquals(
                 IOUtils.readStringFromStream((InputStream)r.getEntity()), 
-                String.format(IOUtils.readStringFromStream(getClass().getResourceAsStream("swagger2-json.txt")),

-                              getPort()),
+                IOUtils.readStringFromStream(getClass().getResourceAsStream("swagger2-json.txt")),
                 false);
         } finally {
             client.close();
         }
     }
 
+    @Test
+    public void testApiListingIsProperlyReturnedYAML() throws Exception {
+        final WebClient client = createWebClient("/swagger.yaml");
+        
+        try {
+            final Response r = client.get();
+            assertEquals(Status.OK.getStatusCode(), r.getStatus());
+            Yaml yaml = new Yaml();
+            assertEquals(yaml.load(getClass().getResourceAsStream("swagger2-yaml.txt")),
+                         yaml.load(IOUtils.readStringFromStream((InputStream)r.getEntity())));
+        } finally {
+            client.close();
+        }
+    }
+
     private WebClient createWebClient(final String url) {
         return WebClient
             .create("http://localhost:" + getPort() + url, 
                 Arrays.< Object >asList(new JacksonJsonProvider()))
-            .accept(MediaType.APPLICATION_JSON);
+            .accept(MediaType.APPLICATION_JSON).accept("application/yaml");
     }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/4f478997/systests/jaxrs/src/test/resources/org/apache/cxf/systest/jaxrs/description/swagger2-json.txt
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/resources/org/apache/cxf/systest/jaxrs/description/swagger2-json.txt
b/systests/jaxrs/src/test/resources/org/apache/cxf/systest/jaxrs/description/swagger2-json.txt
index 4377a41..8a1ee87 100644
--- a/systests/jaxrs/src/test/resources/org/apache/cxf/systest/jaxrs/description/swagger2-json.txt
+++ b/systests/jaxrs/src/test/resources/org/apache/cxf/systest/jaxrs/description/swagger2-json.txt
@@ -5,7 +5,7 @@
          "contact":{"name":"committer@apache.org"},
          "license":{"name":"Apache 2.0 License",
                     "url":"http://www.apache.org/licenses/LICENSE-2.0.html"}},
- "basePath":"/http://localhost:%s/",
+ "basePath":"/",
  "tags":[{"name":"bookstore"}],
  "paths":{"/bookstore":{"get":{"tags":["bookstore"],
                                "summary":"Get books",

http://git-wip-us.apache.org/repos/asf/cxf/blob/4f478997/systests/jaxrs/src/test/resources/org/apache/cxf/systest/jaxrs/description/swagger2-yaml.txt
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/resources/org/apache/cxf/systest/jaxrs/description/swagger2-yaml.txt
b/systests/jaxrs/src/test/resources/org/apache/cxf/systest/jaxrs/description/swagger2-yaml.txt
new file mode 100644
index 0000000..db11154
--- /dev/null
+++ b/systests/jaxrs/src/test/resources/org/apache/cxf/systest/jaxrs/description/swagger2-yaml.txt
@@ -0,0 +1,86 @@
+---
+swagger: "2.0"
+info:
+  description: "The Application"
+  version: "1.0.0"
+  title: "Sample REST Application"
+  contact:
+    name: "committer@apache.org"
+  license:
+    name: "Apache 2.0 License"
+    url: "http://www.apache.org/licenses/LICENSE-2.0.html"
+basePath: "/"
+tags:
+- name: "bookstore"
+paths:
+  /bookstore:
+    get:
+      tags:
+      - "bookstore"
+      summary: "Get books"
+      description: "Get books"
+      operationId: "getBooks"
+      produces:
+      - "application/json"
+      parameters:
+      - name: "page"
+        in: "query"
+        description: "Page to fetch"
+        required: true
+        type: "integer"
+        default: "1"
+        format: "int32"
+      responses:
+        200:
+          description: "successful operation"
+          schema:
+            type: "array"
+            items:
+              $ref: "#/definitions/Book"
+  /bookstore/{id}:
+    get:
+      tags:
+      - "bookstore"
+      summary: "Get book by Id"
+      description: "Get book by Id"
+      operationId: "getBook"
+      produces:
+      - "application/json"
+      parameters:
+      - name: "id"
+        in: "path"
+        description: "id"
+        required: true
+        type: "integer"
+        format: "int64"
+      responses:
+        200:
+          description: "successful operation"
+          schema:
+            $ref: "#/definitions/Book"
+    delete:
+      tags:
+      - "bookstore"
+      summary: "Delete book"
+      description: "Delete book"
+      operationId: "delete"
+      parameters:
+      - name: "id"
+        in: "path"
+        description: "id"
+        required: true
+        type: "string"
+      responses:
+        default:
+          description: "successful operation"
+definitions:
+  Book:
+    type: "object"
+    properties:
+      name:
+        type: "string"
+      id:
+        type: "integer"
+        format: "int64"
+    xml:
+      name: "Book"


Mime
View raw message