This commit is contained in:
William Cheng 2021-08-16 17:28:31 +08:00
commit d10880cf51
3 changed files with 34 additions and 3 deletions

View File

@ -256,6 +256,9 @@ public class DefaultCodegen implements CodegenConfig {
// A cache to efficiently lookup a Schema instance based on the return value of `toModelName()`.
private Map<String, Schema> modelNameToSchemaCache;
// A cache to efficiently lookup schema `toModelName()` based on the schema Key
private Map<String, String> schemaKeyToModelNameCache = new HashMap<>();
@Override
public List<CliOption> cliOptions() {
return cliOptions;
@ -2322,7 +2325,13 @@ public class DefaultCodegen implements CodegenConfig {
*/
@Override
public String toModelName(final String name) {
return camelize(modelNamePrefix + "_" + name + "_" + modelNameSuffix);
if (schemaKeyToModelNameCache.containsKey(name)) {
return schemaKeyToModelNameCache.get(name);
}
String camelizedName = camelize(modelNamePrefix + "_" + name + "_" + modelNameSuffix);
schemaKeyToModelNameCache.put(name, camelizedName);
return camelizedName;
}
private static class NamedSchema {

View File

@ -90,6 +90,9 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
// special property keywords not allowed as these are the function names in the model files
protected Set<String> propertySpecialKeywords = new HashSet<>(Arrays.asList("ToString", "ToJson", "GetHashCode", "Equals", "ShouldSerializeToString"));
// A cache to efficiently lookup schema `toModelName()` based on the schema Key
private Map<String, String> schemaKeyToModelNameCache = new HashMap<>();
public AbstractCSharpCodegen() {
super();
@ -1035,6 +1038,13 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
if (importMapping.containsKey(name)) {
return importMapping.get(name);
}
// memoization
String origName = name;
if (schemaKeyToModelNameCache.containsKey(origName)) {
return schemaKeyToModelNameCache.get(origName);
}
if (!StringUtils.isEmpty(modelNamePrefix)) {
name = modelNamePrefix + "_" + name;
}
@ -1058,9 +1068,12 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
name = "model_" + name; // e.g. 200Response => Model200Response (after camelize)
}
String camelizedName = camelize(name);
schemaKeyToModelNameCache.put(origName, camelizedName);
// camelize the model name
// phone_number => PhoneNumber
return camelize(name);
return camelizedName;
}
@Override

View File

@ -49,6 +49,9 @@ public class GoClientCodegen extends AbstractGoCodegen {
protected boolean isGoSubmodule = false;
protected boolean useOneOfDiscriminatorLookup = false; // use oneOf discriminator's mapping for model lookup
// A cache to efficiently lookup schema `toModelName()` based on the schema Key
private Map<String, String> schemaKeyToModelNameCache = new HashMap<>();
public GoClientCodegen() {
super();
@ -312,8 +315,14 @@ public class GoClientCodegen extends AbstractGoCodegen {
@Override
public String toModelName(String name) {
if (schemaKeyToModelNameCache.containsKey(name)) {
return schemaKeyToModelNameCache.get(name);
}
// underscoring would also lowercase the whole name, thus losing acronyms which are in capitals
return camelize(toModel(name, false));
String camelizedName = camelize(toModel(name, false));
schemaKeyToModelNameCache.put(name, camelizedName);
return camelizedName;
}
public String escapeReservedWord(String name) {