forked from loafle/openapi-generator-original
Improve api name for the Clojure client
For example, when an operation's tag is "users-api", "users_api" or "UsersApi", generate the api file named users_api.clj and "users-api" as the namespace (it was "usersapi.clj" and "usersapi" before). To implement this, I have to move the "sanitizeTag" method from DefaultGenerator.java to DefaultCodegen.java so that its behaviour can be overridden in ClojureClientCodegen.java, which is needed as the default implementation would sanitize "users-api" to "usersapi" before the tag is passed to "toApiName" and "toApiFilename".
This commit is contained in:
parent
9aa59b92bc
commit
09a2bb8b0f
@ -105,6 +105,8 @@ public interface CodegenConfig {
|
||||
|
||||
void processSwagger(Swagger swagger);
|
||||
|
||||
String sanitizeTag(String tag);
|
||||
|
||||
String toApiFilename(String name);
|
||||
|
||||
String toModelFilename(String name);
|
||||
|
@ -2293,6 +2293,19 @@ public class DefaultCodegen {
|
||||
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
|
||||
*
|
||||
|
@ -15,7 +15,6 @@ import org.slf4j.LoggerFactory;
|
||||
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 {
|
||||
@ -623,8 +622,8 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
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) {
|
||||
@ -683,19 +682,6 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
||||
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>();
|
||||
|
@ -145,6 +145,11 @@ public class ClojureClientCodegen extends DefaultCodegen implements CodegenConfi
|
||||
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);
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user