Remove some characters from param name and operation id

(since swagger-doc emits improper characters)
This commit is contained in:
kubo_takaichi 2015-05-13 22:47:46 +09:00
parent 6a336f2e1e
commit f627c81399

View File

@ -1,5 +1,7 @@
package com.wordnik.swagger.codegen;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.wordnik.swagger.codegen.examples.ExampleGenerator;
import com.wordnik.swagger.models.*;
import com.wordnik.swagger.models.auth.ApiKeyAuthDefinition;
@ -15,6 +17,7 @@ import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import java.io.File;
import java.util.*;
@ -160,6 +163,7 @@ public class DefaultCodegen {
}
public String toParamName(String name) {
name = removeNonNameElementToCamelCase(name);
if(reservedWords.contains(name)) {
return escapeReservedWord(name);
}
@ -680,6 +684,7 @@ public class DefaultCodegen {
operationId = builder.toString();
LOGGER.warn("generated operationId " + operationId);
}
operationId = removeNonNameElementToCamelCase(operationId);
op.path = path;
op.operationId = toOperationId(operationId);
op.summary = escapeText(operation.getSummary());
@ -1108,6 +1113,26 @@ public class DefaultCodegen {
return word;
}
/**
* Remove characters not suitable for variable or method name from the input and camelize it
* @param name
* @return
*/
public String removeNonNameElementToCamelCase(String name) {
String nonNameElementPattern = "[-_:;#]";
name = StringUtils.join(Lists.transform(Lists.newArrayList(name.split(nonNameElementPattern)), new Function<String, String>() {
@Nullable
@Override
public String apply(String input) {
return StringUtils.capitalize(input);
}
}), "");
if (name.length() > 0) {
name = name.substring(0, 1).toLowerCase() + name.substring(1);
}
return name;
}
public static String camelize(String word) {
return camelize(word, false);
}