Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for relative path in specs files for SpringMVC. #4179

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,24 @@ public List<File> 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);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -648,6 +671,11 @@ public Reader getTemplate(String name) {
return files;
}

private String trimSlash(String contextPath)
{
return contextPath.replaceAll("(^/+|/+$)", "");
}

private File processTemplateToFile(Map<String, Object> templateData, String templateName, String outputFilename) throws IOException {
if(ignoreProcessor.allowsFile(new File(outputFilename.replaceAll("//", "/")))) {
String templateFile = getFullTemplateFile(config, templateName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down