You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by as...@apache.org on 2015/11/04 20:25:40 UTC

cxf git commit: [CXF-6633] Normalized resource path in SwaggerSerializer

Repository: cxf
Updated Branches:
  refs/heads/master 7e5877dc4 -> 807be1795


[CXF-6633] Normalized resource path in SwaggerSerializer


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

Branch: refs/heads/master
Commit: 807be17959f36d64e4e6ec88ba0b8b1e58e04ec2
Parents: 7e5877d
Author: Andrei Shakirin <an...@gmail.com>
Authored: Wed Nov 4 20:25:32 2015 +0100
Committer: Andrei Shakirin <an...@gmail.com>
Committed: Wed Nov 4 20:25:32 2015 +0100

----------------------------------------------------------------------
 .../cxf/jaxrs/swagger/Swagger2Serializers.java  | 39 +++++++++++++-------
 1 file changed, 25 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/807be179/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Serializers.java
----------------------------------------------------------------------
diff --git a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Serializers.java b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Serializers.java
index 83ef3dc..c80cf4d 100644
--- a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Serializers.java
+++ b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Serializers.java
@@ -33,7 +33,6 @@ import javax.ws.rs.core.MultivaluedMap;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.apache.commons.lang3.tuple.Pair;
-
 import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
 import org.apache.cxf.jaxrs.model.OperationResourceInfo;
@@ -92,20 +91,11 @@ public class Swagger2Serializers extends SwaggerSerializers {
             Map<Pair<String, String>, OperationResourceInfo> methods = new HashMap<>();
             for (ClassResourceInfo cri : cris) {
                 for (OperationResourceInfo ori : cri.getMethodDispatcher().getOperationResourceInfos()) {
-                    StringBuilder fullPath = new StringBuilder().
-                            append(cri.getURITemplate().getValue()).
-                            append(ori.getURITemplate().getValue());
-                    if (fullPath.charAt(fullPath.length() - 1) == '/') {
-                        fullPath.setLength(fullPath.length() - 1);
-                    }
-                    // Adapt to Swagger's path expression
-                    if (fullPath.toString().endsWith(":.*}")) {
-                        fullPath.setLength(fullPath.length() - 4);
-                        fullPath.append('}');
-                    }
+                    String normalizedPath = getNormalizedPath(cri.getURITemplate().getValue(), ori
+                        .getURITemplate().getValue());
 
-                    operations.put(fullPath.toString(), cri);
-                    methods.put(ImmutablePair.of(ori.getHttpMethod(), fullPath.toString()), ori);
+                    operations.put(normalizedPath, cri);
+                    methods.put(ImmutablePair.of(ori.getHttpMethod(), normalizedPath), ori);
                 }
             }
 
@@ -154,4 +144,25 @@ public class Swagger2Serializers extends SwaggerSerializers {
 
         super.writeTo(data, type, genericType, annotations, mediaType, headers, out);
     }
+
+    private String getNormalizedPath(String classResourcePath, String operationResourcePath) {
+        StringBuilder path = new StringBuilder().
+            append(classResourcePath).
+            append(operationResourcePath);
+
+        StringBuilder normalizedPath = new StringBuilder();
+
+        String[] segments = StringUtils.split(path.toString(), "/");
+        for (String segment : segments) {
+            if (!StringUtils.isEmpty(segment)) {
+                normalizedPath.append("/").append(segment);
+            }
+        }
+        // Adapt to Swagger's path expression
+        if (normalizedPath.toString().endsWith(":.*}")) {
+            normalizedPath.setLength(normalizedPath.length() - 4);
+            normalizedPath.append('}');
+        }
+        return normalizedPath.toString();
+    }
 }