diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java index a2e7f597c43..11c3d30fb06 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultGenerator.java @@ -210,6 +210,24 @@ public List generate() { String basePath = config.escapeText(hostBuilder.toString()); String basePathWithoutHost = config.escapeText(swagger.getBasePath()); + //when the basepath in spec looks like this: context/basePath + //normally, the first part indicates the context root, the second is the relative path. + String strippedBasePath=trimSlash(contextPath); + + String contextRoot=strippedBasePath; + String relativeBasePath=""; + + int slashPos = strippedBasePath.indexOf('/'); + if (slashPos != -1) + { + contextRoot = strippedBasePath.substring(0, slashPos); + contextRoot = trimSlash(contextRoot); + + relativeBasePath = strippedBasePath.substring(slashPos); + relativeBasePath = "/"+trimSlash(relativeBasePath); + } + + // resolve inline models InlineModelResolver inlineModelResolver = new InlineModelResolver(); inlineModelResolver.flatten(swagger); @@ -399,6 +417,11 @@ public int compare(CodegenOperation one, CodegenOperation another) { operation.put("basePath", basePath); operation.put("basePathWithoutHost", basePathWithoutHost); operation.put("contextPath", contextPath); + + // + operation.put("contextRoot", contextRoot); + operation.put("relativeBasePath", relativeBasePath); + operation.put("baseName", tag); operation.put("modelPackage", config.modelPackage()); operation.putAll(config.additionalProperties()); @@ -648,6 +671,11 @@ public Reader getTemplate(String name) { return files; } + private String trimSlash(String contextPath) + { + return contextPath.replaceAll("(^/+|/+$)", ""); + } + private File processTemplateToFile(Map templateData, String templateName, String outputFilename) throws IOException { if(ignoreProcessor.allowsFile(new File(outputFilename.replaceAll("//", "/")))) { String templateFile = getFullTemplateFile(config, templateName); diff --git a/modules/swagger-codegen/src/main/resources/JavaSpring/api.mustache b/modules/swagger-codegen/src/main/resources/JavaSpring/api.mustache index 4a97c5d5f33..3b6e4af411d 100644 --- a/modules/swagger-codegen/src/main/resources/JavaSpring/api.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaSpring/api.mustache @@ -37,7 +37,7 @@ public interface {{classname}} { }{{/hasAuthMethods}}, tags={ {{#vendorExtensions.x-tags}}"{{tag}}",{{/vendorExtensions.x-tags}} }) @ApiResponses(value = { {{#responses}} @ApiResponse(code = {{{code}}}, message = "{{{message}}}", response = {{{returnType}}}.class){{#hasMore}},{{/hasMore}}{{/responses}} }) - @RequestMapping(value = "{{{path}}}",{{#singleContentTypes}} + @RequestMapping(value = "{{{relativeBasePath}}}{{{path}}}",{{#singleContentTypes}} produces = "{{{vendorExtensions.x-accepts}}}", consumes = "{{{vendorExtensions.x-contentType}}}",{{/singleContentTypes}}{{^singleContentTypes}}{{#hasProduces}} produces = { {{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }, {{/hasProduces}}{{#hasConsumes}}