forked from loafle/openapi-generator-original
Merge branch 'master' into java-local-var
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package io.swagger.codegen.config;
|
||||
|
||||
import io.swagger.codegen.utils.OptionUtils;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Contains shared logic for applying key-value pairs and CSV strings
|
||||
* to specific settings in CodegenConfigurator.
|
||||
*
|
||||
* <p>
|
||||
* This class exists to facilitate testing. These methods could be applied
|
||||
* to CodegenConfigurator, but this complicates things when mocking CodegenConfigurator.
|
||||
* </p>
|
||||
*/
|
||||
public final class CodegenConfiguratorUtils {
|
||||
|
||||
public static void applySystemPropertiesKvp(String systemProperties, CodegenConfigurator configurator) {
|
||||
final Map<String, String> map = createMapFromKeyValuePairs(systemProperties);
|
||||
for (Map.Entry<String, String> entry : map.entrySet()) {
|
||||
configurator.addSystemProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public static void applyInstantiationTypesKvp(String instantiationTypes, CodegenConfigurator configurator) {
|
||||
final Map<String, String> map = createMapFromKeyValuePairs(instantiationTypes);
|
||||
for (Map.Entry<String, String> entry : map.entrySet()) {
|
||||
configurator.addInstantiationType(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public static void applyImportMappingsKvp(String importMappings, CodegenConfigurator configurator) {
|
||||
final Map<String, String> map = createMapFromKeyValuePairs(importMappings);
|
||||
for (Map.Entry<String, String> entry : map.entrySet()) {
|
||||
configurator.addImportMapping(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public static void applyTypeMappingsKvp(String typeMappings, CodegenConfigurator configurator) {
|
||||
final Map<String, String> map = createMapFromKeyValuePairs(typeMappings);
|
||||
for (Map.Entry<String, String> entry : map.entrySet()) {
|
||||
configurator.addTypeMapping(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public static void applyAdditionalPropertiesKvp(String additionalProperties, CodegenConfigurator configurator) {
|
||||
final Map<String, String> map = createMapFromKeyValuePairs(additionalProperties);
|
||||
for (Map.Entry<String, String> entry : map.entrySet()) {
|
||||
configurator.addAdditionalProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public static void applyLanguageSpecificPrimitivesCsv(String languageSpecificPrimitives, CodegenConfigurator configurator) {
|
||||
final Set<String> set = createSetFromCsvList(languageSpecificPrimitives);
|
||||
for (String item : set) {
|
||||
configurator.addLanguageSpecificPrimitive(item);
|
||||
}
|
||||
}
|
||||
|
||||
private static Set<String> createSetFromCsvList(String csvProperty) {
|
||||
final List<String> values = OptionUtils.splitCommaSeparatedList(csvProperty);
|
||||
return new HashSet<String>(values);
|
||||
}
|
||||
|
||||
private static Map<String, String> createMapFromKeyValuePairs(String commaSeparatedKVPairs) {
|
||||
final List<Pair<String, String>> pairs = OptionUtils.parseCommaSeparatedTuples(commaSeparatedKVPairs);
|
||||
|
||||
Map<String, String> result = new HashMap<String, String>();
|
||||
|
||||
for (Pair<String, String> pair : pairs) {
|
||||
result.put(pair.getLeft(), pair.getRight());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -17,12 +17,13 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
|
||||
supportsInheritance = true;
|
||||
setReservedWordsLowerCase(Arrays.asList(
|
||||
// local variable names used in API methods (endpoints)
|
||||
"path", "queryParameters", "headerParams", "formParams", "useFormData", "deferred",
|
||||
"varLocalPath", "queryParameters", "headerParams", "formParams", "useFormData", "varLocalDeferred",
|
||||
"requestOptions",
|
||||
// Typescript reserved words
|
||||
"abstract", "await", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "debugger", "default", "delete", "do", "double", "else", "enum", "export", "extends", "false", "final", "finally", "float", "for", "function", "goto", "if", "implements", "import", "in", "instanceof", "int", "interface", "let", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "super", "switch", "synchronized", "this", "throw", "transient", "true", "try", "typeof", "var", "void", "volatile", "while", "with", "yield"));
|
||||
|
||||
languageSpecificPrimitives = new HashSet<String>(Arrays.asList(
|
||||
"string",
|
||||
"String",
|
||||
"boolean",
|
||||
"Boolean",
|
||||
@@ -30,7 +31,12 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
|
||||
"Integer",
|
||||
"Long",
|
||||
"Float",
|
||||
"Object"));
|
||||
"Object",
|
||||
"Array",
|
||||
"Date",
|
||||
"number",
|
||||
"any"
|
||||
));
|
||||
instantiationTypes.put("array", "Array");
|
||||
|
||||
typeMapping = new HashMap<String, String>();
|
||||
@@ -116,10 +122,22 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
|
||||
|
||||
@Override
|
||||
public String toModelName(String name) {
|
||||
// model name cannot use reserved keyword, e.g. return
|
||||
if (isReservedWord(name))
|
||||
throw new RuntimeException(name
|
||||
+ " (reserved word) cannot be used as a model name");
|
||||
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
|
||||
|
||||
if (!StringUtils.isEmpty(modelNamePrefix)) {
|
||||
name = modelNamePrefix + "_" + name;
|
||||
}
|
||||
|
||||
if (!StringUtils.isEmpty(modelNameSuffix)) {
|
||||
name = name + "_" + modelNameSuffix;
|
||||
}
|
||||
|
||||
// model name cannot use reserved keyword, e.g. return
|
||||
if (isReservedWord(name)) {
|
||||
String modelName = camelize("object_" + name);
|
||||
LOGGER.warn(name + " (reserved word) cannot be used as model name. Renamed to " + modelName);
|
||||
return modelName;
|
||||
}
|
||||
|
||||
// camelize the model name
|
||||
// phone_number => PhoneNumber
|
||||
@@ -158,7 +176,7 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
|
||||
return type;
|
||||
} else
|
||||
type = swaggerType;
|
||||
return type;
|
||||
return toModelName(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -103,7 +103,7 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.6");
|
||||
supportedLibraries.put("okhttp-gson", "HTTP client: OkHttp 2.4.0. JSON processing: Gson 2.3.1");
|
||||
supportedLibraries.put(RETROFIT_1, "HTTP client: OkHttp 2.4.0. JSON processing: Gson 2.3.1 (Retrofit 1.9.0)");
|
||||
supportedLibraries.put(RETROFIT_2, "HTTP client: OkHttp 2.5.0. JSON processing: Gson 2.4 (Retrofit 2.0.0-beta2). Enable the RxJava adapter using '-DuseRxJava=true'.");
|
||||
supportedLibraries.put(RETROFIT_2, "HTTP client: OkHttp 2.5.0. JSON processing: Gson 2.4 (Retrofit 2.0.0-beta4). Enable the RxJava adapter using '-DuseRxJava=true'.");
|
||||
|
||||
CliOption library = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use");
|
||||
library.setDefault(DEFAULT_LIBRARY);
|
||||
|
||||
@@ -55,9 +55,6 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
modelTestTemplateFiles.put("model_test.mustache", ".rb");
|
||||
apiTestTemplateFiles.put("api_test.mustache", ".rb");
|
||||
|
||||
typeMapping.clear();
|
||||
languageSpecificPrimitives.clear();
|
||||
|
||||
setReservedWordsLowerCase(
|
||||
Arrays.asList(
|
||||
// local variable names used in API methods (endpoints)
|
||||
@@ -71,11 +68,24 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
"if", "not", "return", "undef", "yield")
|
||||
);
|
||||
|
||||
typeMapping.clear();
|
||||
languageSpecificPrimitives.clear();
|
||||
|
||||
// primitives in ruby lang
|
||||
languageSpecificPrimitives.add("int");
|
||||
languageSpecificPrimitives.add("array");
|
||||
languageSpecificPrimitives.add("map");
|
||||
languageSpecificPrimitives.add("string");
|
||||
// primitives in the typeMapping
|
||||
languageSpecificPrimitives.add("String");
|
||||
languageSpecificPrimitives.add("Integer");
|
||||
languageSpecificPrimitives.add("Float");
|
||||
languageSpecificPrimitives.add("Date");
|
||||
languageSpecificPrimitives.add("DateTime");
|
||||
languageSpecificPrimitives.add("BOOLEAN");
|
||||
languageSpecificPrimitives.add("Array");
|
||||
languageSpecificPrimitives.add("Hash");
|
||||
languageSpecificPrimitives.add("File");
|
||||
|
||||
typeMapping.put("string", "String");
|
||||
typeMapping.put("char", "String");
|
||||
@@ -317,7 +327,7 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
String type = null;
|
||||
if (typeMapping.containsKey(swaggerType)) {
|
||||
type = typeMapping.get(swaggerType);
|
||||
if (languageSpecificPrimitives.contains(type)) {
|
||||
if (languageSpecificPrimitives.contains(type)) {
|
||||
return type;
|
||||
}
|
||||
} else {
|
||||
@@ -333,7 +343,6 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
public String toVarName(String name) {
|
||||
// sanitize name
|
||||
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
|
||||
|
||||
// if it's all uppper case, convert to lower case
|
||||
if (name.matches("^[A-Z_]*$")) {
|
||||
name = name.toLowerCase();
|
||||
@@ -361,6 +370,14 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
public String toModelName(String name) {
|
||||
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
|
||||
|
||||
if (!StringUtils.isEmpty(modelNamePrefix)) {
|
||||
name = modelNamePrefix + "_" + name;
|
||||
}
|
||||
|
||||
if (!StringUtils.isEmpty(modelNameSuffix)) {
|
||||
name = name + "_" + modelNameSuffix;
|
||||
}
|
||||
|
||||
// model name cannot use reserved keyword, e.g. return
|
||||
if (isReservedWord(name)) {
|
||||
String modelName = camelize("object_" + name);
|
||||
@@ -375,6 +392,15 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
|
||||
@Override
|
||||
public String toModelFilename(String name) {
|
||||
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
|
||||
|
||||
if (!StringUtils.isEmpty(modelNamePrefix)) {
|
||||
name = modelNamePrefix + "_" + name;
|
||||
}
|
||||
|
||||
if (!StringUtils.isEmpty(modelNameSuffix)) {
|
||||
name = name + "_" + modelNameSuffix;
|
||||
}
|
||||
// model name cannot use reserved keyword, e.g. return
|
||||
if (isReservedWord(name)) {
|
||||
String filename = underscore("object_" + name);
|
||||
@@ -398,12 +424,12 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
|
||||
@Override
|
||||
public String toApiTestFilename(String name) {
|
||||
return toApiName(name) + "_spec";
|
||||
return toApiFilename(name) + "_spec";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toModelTestFilename(String name) {
|
||||
return toModelName(name) + "_spec";
|
||||
return toModelFilename(name) + "_spec";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user