Skip to content

Commit

Permalink
issue-8358**UseTagFix**JAXRS-Jersey Path issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Anandaraj.Nagarajan committed Nov 16, 2018
1 parent d15266c commit a7cce05
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,78 @@ public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) {
return objs;
}

/* (non-Javadoc)
* @see io.swagger.codegen.languages.AbstractJavaJAXRSServerCodegen#postProcessOperations(java.util.Map)
*/
@Override
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
objs = super.postProcessOperations(objs);

if (useTags) {
super.addOperationToGroup(tag, resourcePath, operation, co, operations);
@SuppressWarnings("unchecked")
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
if (operations != null) {

// collect paths
List<String> allPaths = new ArrayList<>();
@SuppressWarnings("unchecked")
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
for (CodegenOperation operation: ops) {
String path = operation.path;
if (path.startsWith("/")) {
path = path.substring(1);
}
allPaths.add(path);
}

if (!allPaths.isEmpty()) {
// find common prefix
StringBuilder basePathSB = new StringBuilder();
String firstPath = allPaths.remove(0);
String[] parts = firstPath.split("/");
partsLoop:
for (String part : parts) {
for (String path : allPaths) {
if (!path.startsWith(basePathSB.toString() + part)) {
break partsLoop;
}
}
basePathSB.append(part).append("/");
}
String basePath = basePathSB.toString();
if (basePath.endsWith("/")) {
basePath = basePath.substring(0, basePath.length() - 1);
}

if (basePath.length() > 0) {
// update operations
for (CodegenOperation operation: ops) {
operation.path = operation.path.substring(basePath.length() + (operation.path.startsWith("/") ? 1 : 0));
operation.baseName = basePath;
operation.subresourceOperation = !operation.path.isEmpty();
}

// save base path in objects
objs.put("apiBasePath", basePath);
}
}
}
}
else {

return objs;
}

@Override
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
if (useTags) {
// only add operations to group; base path extraction is done in postProcessOperations
List<CodegenOperation> opList = operations.get(tag);
if (opList == null) {
opList = new ArrayList<CodegenOperation>();
operations.put(tag, opList);
}
opList.add(co);
} else {
String basePath = resourcePath;
if (basePath.startsWith("/")) {
basePath = basePath.substring(1);
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/mustache/JavaJaxRS/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import javax.ws.rs.*;
import javax.validation.constraints.*;
{{/useBeanValidation}}

@Path("/{{{baseName}}}")
{{#apiBasePath}}@Path("/{{{apiBasePath}}}"){{/apiBasePath}}
{{^apiBasePath}}@Path("/{{{baseName}}}"){{/apiBasePath}}
{{#hasConsumes}}@Consumes({ {{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }){{/hasConsumes}}
{{#hasProduces}}@Produces({ {{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }){{/hasProduces}}
@io.swagger.annotations.Api(description = "the {{{baseName}}} API")
Expand Down

0 comments on commit a7cce05

Please sign in to comment.