Skip to content

[Clojure] Improve api name for the Clojure client #2286

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

Merged
merged 1 commit into from
Mar 2, 2016
Merged
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 @@ -105,6 +105,8 @@ public interface CodegenConfig {

void processSwagger(Swagger swagger);

String sanitizeTag(String tag);

String toApiFilename(String name);

String toModelFilename(String name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2293,6 +2293,19 @@ public String sanitizeName(String name) {
return name.replaceAll("[^a-zA-Z0-9_]", "");
}

@SuppressWarnings("static-method")
public String sanitizeTag(String tag) {
// remove spaces and make strong case
String[] parts = tag.split(" ");
StringBuilder buf = new StringBuilder();
for (String part : parts) {
if (StringUtils.isNotEmpty(part)) {
buf.append(StringUtils.capitalize(part));
}
}
return buf.toString().replaceAll("[^a-zA-Z ]", "");
}

/**
* Only write if the file doesn't exist
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.io.*;
import java.util.*;

import static org.apache.commons.lang3.StringUtils.capitalize;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;

public class DefaultGenerator extends AbstractGenerator implements Generator {
Expand Down Expand Up @@ -623,8 +622,8 @@ public void processOperation(String resourcePath, String httpMethod, Operation o
try {
co = config.fromOperation(resourcePath, httpMethod, operation, swagger.getDefinitions(), swagger);
co.tags = new ArrayList<String>();
co.tags.add(sanitizeTag(tag));
config.addOperationToGroup(sanitizeTag(tag), resourcePath, operation, co, operations);
co.tags.add(config.sanitizeTag(tag));
config.addOperationToGroup(config.sanitizeTag(tag), resourcePath, operation, co, operations);

List<Map<String, List<String>>> securities = operation.getSecurity();
if (securities == null && swagger.getSecurity() != null) {
Expand Down Expand Up @@ -683,19 +682,6 @@ private static String generateParameterId(Parameter parameter) {
return parameter.getName() + ":" + parameter.getIn();
}

@SuppressWarnings("static-method")
protected String sanitizeTag(String tag) {
// remove spaces and make strong case
String[] parts = tag.split(" ");
StringBuilder buf = new StringBuilder();
for (String part : parts) {
if (isNotEmpty(part)) {
buf.append(capitalize(part));
}
}
return buf.toString().replaceAll("[^a-zA-Z ]", "");
}

@SuppressWarnings("static-method")
public Map<String, Object> processOperations(CodegenConfig config, String tag, List<CodegenOperation> ops) {
Map<String, Object> operations = new HashMap<String, Object>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ public void preprocessSwagger(Swagger swagger) {
supportingFiles.add(new SupportingFile("core.mustache", baseNamespaceFolder, "core.clj"));
}

@Override
public String sanitizeTag(String tag) {
return tag.replaceAll("[^a-zA-Z_]+", "_");
}

@Override
public String apiFileFolder() {
return outputFolder + File.separator + sourceFolder + File.separator + namespaceToFolder(apiPackage);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.swagger.codegen.languages;

import org.testng.Assert;
import org.testng.annotations.Test;

public class ClojureClientCodegenTest {
ClojureClientCodegen codegen = new ClojureClientCodegen();

@Test
public void testSanitizeTag() throws Exception {
Assert.assertEquals(codegen.sanitizeTag("users-api"), "users_api");
Assert.assertEquals(codegen.sanitizeTag("users_api"), "users_api");
Assert.assertEquals(codegen.sanitizeTag("users api"), "users_api");
Assert.assertEquals(codegen.sanitizeTag("users.api"), "users_api");
Assert.assertEquals(codegen.sanitizeTag("Users Api"), "Users_Api");
Assert.assertEquals(codegen.sanitizeTag("UsersApi"), "UsersApi");
Assert.assertEquals(codegen.sanitizeTag("usersapi"), "usersapi");
Assert.assertEquals(codegen.sanitizeTag("Usersapi"), "Usersapi");
}

@Test
public void testToApiName() throws Exception {
Assert.assertEquals(codegen.toApiName("users_api"), "users-api");
Assert.assertEquals(codegen.toApiName("Users_Api"), "users-api");
Assert.assertEquals(codegen.toApiName("UsersApi"), "users-api");
Assert.assertEquals(codegen.toApiName("usersapi"), "usersapi");
Assert.assertEquals(codegen.toApiName("Usersapi"), "usersapi");
}

@Test
public void testToApiFilename() throws Exception {
Assert.assertEquals(codegen.toApiFilename("users_api"), "users_api");
Assert.assertEquals(codegen.toApiFilename("Users_Api"), "users_api");
Assert.assertEquals(codegen.toApiFilename("UsersApi"), "users_api");
Assert.assertEquals(codegen.toApiFilename("usersapi"), "usersapi");
Assert.assertEquals(codegen.toApiFilename("Usersapi"), "usersapi");
}
}