Skip to content

Commit

Permalink
Merge pull request #3385 from maggu2810/1.5-path-buildling
Browse files Browse the repository at this point in the history
swagger-jaxrs: fix path building
  • Loading branch information
frantuma authored Mar 16, 2020
2 parents 0d11f6d + 3de641c commit a8a9416
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 41 deletions.
42 changes: 1 addition & 41 deletions modules/swagger-jaxrs/src/main/java/io/swagger/jaxrs/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ private Swagger read(Class<?> cls, String parentPath, String parentMethod, boole
}
javax.ws.rs.Path methodPath = ReflectionUtils.getAnnotation(method, javax.ws.rs.Path.class);

String operationPath = getPath(apiPath, methodPath, parentPath, isSubresource);
String operationPath = ReaderUtils.getPath(apiPath, methodPath, parentPath, isSubresource);
Map<String, String> regexMap = new LinkedHashMap<String, String>();
operationPath = PathUtils.parsePath(operationPath, regexMap);
if (operationPath != null) {
Expand Down Expand Up @@ -735,46 +735,6 @@ protected Set<String> extractTags(Api api) {
return output;
}

private String getPath(javax.ws.rs.Path classLevelPath, javax.ws.rs.Path methodLevelPath, String parentPath,
boolean isSubResource) {
if (classLevelPath == null && methodLevelPath == null && StringUtils.isEmpty(parentPath)) {
return null;
}
StringBuilder b = new StringBuilder();
if (parentPath != null && !"".equals(parentPath) && !"/".equals(parentPath)) {
if (!parentPath.startsWith("/")) {
parentPath = "/" + parentPath;
}
if (parentPath.endsWith("/")) {
parentPath = parentPath.substring(0, parentPath.length() - 1);
}

b.append(parentPath);
}
if (classLevelPath != null && !isSubResource) {
b.append(classLevelPath.value());
}
if (methodLevelPath != null && !"/".equals(methodLevelPath.value())) {
String methodPath = methodLevelPath.value();
if (!methodPath.startsWith("/") && !b.toString().endsWith("/")) {
b.append("/");
}
if (methodPath.endsWith("/")) {
methodPath = methodPath.substring(0, methodPath.length() - 1);
}
b.append(methodPath);
}
String output = b.toString();
if (!output.startsWith("/")) {
output = "/" + output;
}
if (output.endsWith("/") && output.length() > 1) {
return output.substring(0, output.length() - 1);
} else {
return output;
}
}

private Map<String, Property> parseResponseHeaders(ResponseHeader[] headers, JsonView jsonView) {
Map<String, Property> responseHeaders = null;
if (headers != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import javax.ws.rs.core.Context;

import org.apache.commons.lang3.StringUtils;

public class ReaderUtils {

/**
Expand Down Expand Up @@ -133,4 +135,43 @@ private static boolean isContext(List<Annotation> annotations) {
}
return false;
}

/**
* appends a path component string to a StringBuilder
* guarantees:
* <ul>
* <li>nulls, empty strings and "/" are nops</li>
* <li>output will always start with "/" and never end with "/"</li>
* </ul>
* @param component component to be added
* @param to output
*/
private static void appendPathComponent(String component, StringBuilder to) {
if (component == null || component.isEmpty() || "/".equals(component)) {
return;
}
if (!component.startsWith("/") && (to.length() == 0 || '/' != to.charAt(to.length() - 1))) {
to.append("/");
}
if (component.endsWith("/")) {
to.append(component, 0, component.length() - 1);
} else {
to.append(component);
}
}

public static String getPath(javax.ws.rs.Path classLevelPath, javax.ws.rs.Path methodLevelPath, String parentPath, boolean isSubresource) {
if (classLevelPath == null && methodLevelPath == null && StringUtils.isEmpty(parentPath)) {
return null;
}
StringBuilder b = new StringBuilder();
appendPathComponent(parentPath, b);
if (classLevelPath != null && !isSubresource) {
appendPathComponent(classLevelPath.value(), b);
}
if (methodLevelPath != null) {
appendPathComponent(methodLevelPath.value(), b);
}
return b.length() == 0 ? "/" : b.toString();
}
}

0 comments on commit a8a9416

Please sign in to comment.