[java-jaxrs] Fix paths when useTags=true is used (#437)

* Add test case for the existing implementation
* Introduce {{commonPath}}
* Update samples
This commit is contained in:
Jérémie Bresson
2018-07-03 15:31:26 +02:00
committed by GitHub
parent 0137763997
commit 3d64bd0c49
16 changed files with 397 additions and 32 deletions

View File

@@ -32,8 +32,8 @@ import org.openapitools.codegen.utils.URLPathUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URL;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -154,6 +154,8 @@ public abstract class AbstractJavaJAXRSServerCodegen extends AbstractJavaCodegen
@SuppressWarnings("unchecked")
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
if (operations != null) {
String commonBaseName = null;
boolean baseNameEquals = true;
@SuppressWarnings("unchecked")
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
for (CodegenOperation operation : ops) {
@@ -219,6 +221,23 @@ public abstract class AbstractJavaJAXRSServerCodegen extends AbstractJavaCodegen
} else if ("map".equals(operation.returnContainer)) {
operation.returnContainer = "Map";
}
if(commonBaseName == null) {
commonBaseName = operation.baseName;
} else if(!commonBaseName.equals(operation.baseName)) {
baseNameEquals = false;
}
}
if(baseNameEquals) {
objs.put("commonPath", commonBaseName);
} else {
for (CodegenOperation operation : ops) {
if(operation.baseName != null) {
operation.path = "/" + operation.baseName + operation.path;
operation.baseName = null;
}
}
objs.put("commonPath", null);
}
}
return objs;

View File

@@ -159,7 +159,7 @@ public class JavaJerseyServerCodegen extends AbstractJavaJAXRSServerCodegen {
@Override
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
if (useTags) {
String basePath = resourcePath;
String basePath = tag.toLowerCase();
if (basePath.startsWith("/")) {
basePath = basePath.substring(1);
}
@@ -168,11 +168,17 @@ public class JavaJerseyServerCodegen extends AbstractJavaJAXRSServerCodegen {
basePath = basePath.substring(0, pos);
}
if (co.path.startsWith("/" + basePath)) {
boolean pathStartsWithBasePath = co.path.startsWith("/" + basePath);
if (pathStartsWithBasePath) {
co.path = co.path.substring(("/" + basePath).length());
}
co.subresourceOperation = !co.path.isEmpty();
super.addOperationToGroup(tag, resourcePath, operation, co, operations);
if (pathStartsWithBasePath) {
co.baseName = basePath;
} else {
co.baseName = null;
}
} else {
String basePath = resourcePath;
if (basePath.startsWith("/")) {