From be0232fa3580a00c8a7d409d28948835c6e99730 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Thu, 5 Nov 2015 22:26:49 -0800 Subject: [PATCH] moved tags to extension to avoid path collisions #1518 --- .../codegen/languages/JaxRSServerCodegen.java | 54 +++++++++++++------ .../src/main/resources/JavaJaxRS/api.mustache | 7 ++- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JaxRSServerCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JaxRSServerCodegen.java index fd83e38896a..375f5d72bff 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JaxRSServerCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JaxRSServerCodegen.java @@ -1,23 +1,12 @@ package io.swagger.codegen.languages; -import io.swagger.codegen.CodegenConfig; -import io.swagger.codegen.CodegenConstants; -import io.swagger.codegen.CodegenModel; -import io.swagger.codegen.CodegenOperation; -import io.swagger.codegen.CodegenProperty; -import io.swagger.codegen.CodegenResponse; -import io.swagger.codegen.CodegenType; -import io.swagger.codegen.SupportingFile; +import io.swagger.codegen.*; import io.swagger.models.Operation; -import io.swagger.models.properties.Property; -import io.swagger.models.properties.StringProperty; +import io.swagger.models.Path; +import io.swagger.models.Swagger; import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Map; +import java.util.*; public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConfig { protected String title = "Swagger Server"; @@ -118,6 +107,36 @@ public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConf co.baseName = basePath; } + @Override + public void preprocessSwagger(Swagger swagger) { + if(swagger != null && swagger.getPaths() != null) { + for(String pathname : swagger.getPaths().keySet()) { + Path path = swagger.getPath(pathname); + if(path.getOperations() != null) { + for(Operation operation : path.getOperations()) { + if(operation.getTags() != null) { + List> tags = new ArrayList>(); + for(String tag : operation.getTags()) { + Map value = new HashMap(); + value.put("tag", tag); + value.put("hasMore", "true"); + tags.add(value); + } + if(tags.size() > 0) { + tags.get(tags.size() - 1).remove("hasMore"); + } + if(operation.getTags().size() > 0) { + String tag = operation.getTags().get(0); + operation.setTags(Arrays.asList(tag)); + } + operation.setVendorExtension("x-tags", tags); + } + } + } + } + } + } + @Override public Map postProcessModels(Map objs) { List models = (List) objs.get("models"); @@ -195,9 +214,10 @@ public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConf result = result.substring(0, ix) + "/impl" + result.substring(ix, result.length() - 5) + "ServiceImpl.java"; String output = System.getProperty("swagger.codegen.jaxrs.impl.source"); - if (output != null) { - result = result.replace(apiFileFolder(), implFileFolder(output)); + if(output == null) { + output = "src" + File.separator + "main" + File.separator + "java"; } + result = result.replace(apiFileFolder(), implFileFolder(output)); } else if (templateName.endsWith("Factory.mustache")) { int ix = result.lastIndexOf('/'); result = result.substring(0, ix) + "/factories" + result.substring(ix, result.length() - 5) + "ServiceFactory.java"; diff --git a/modules/swagger-codegen/src/main/resources/JavaJaxRS/api.mustache b/modules/swagger-codegen/src/main/resources/JavaJaxRS/api.mustache index e919fa7ce71..ea9f2518b01 100644 --- a/modules/swagger-codegen/src/main/resources/JavaJaxRS/api.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaJaxRS/api.mustache @@ -22,14 +22,13 @@ import com.sun.jersey.multipart.FormDataParam; import javax.ws.rs.core.Response; import javax.ws.rs.*; -@Path("{{basePathWithoutHost}}/{{baseName}}") +@Path("/{{baseName}}") {{#hasConsumes}}@Consumes({ {{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }){{/hasConsumes}} {{#hasProduces}}@Produces({ {{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }){{/hasProduces}} -@io.swagger.annotations.Api(value = "/{{baseName}}", description = "the {{baseName}} API") +@io.swagger.annotations.Api(description = "the {{baseName}} API") {{>generatedAnnotation}} {{#operations}} public class {{classname}} { - private final {{classname}}Service delegate = {{classname}}ServiceFactory.get{{classname}}(); {{#operation}} @@ -43,7 +42,7 @@ public class {{classname}} { {{/hasMore}}{{/scopes}} }{{/isOAuth}}){{#hasMore}}, {{/hasMore}}{{/authMethods}} - }{{/hasAuthMethods}}) + }{{/hasAuthMethods}}, tags={ {{#vendorExtensions.x-tags}}"{{tag}}"{{#hasMore}}, {{/hasMore}}{{/vendorExtensions.x-tags}} }) @io.swagger.annotations.ApiResponses(value = { {{#responses}} @io.swagger.annotations.ApiResponse(code = {{{code}}}, message = "{{{message}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}}){{#hasMore}}, {{/hasMore}}{{/responses}} })