Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 committed Aug 16, 2021
2 parents 2d8ee32 + 849fec2 commit d10880c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
// 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;
Expand Down Expand Up @@ -2322,7 +2325,13 @@ public String toApiName(String name) {
*/
@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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -1035,6 +1038,13 @@ public String toModelName(String name) {
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;
}
Expand All @@ -1058,9 +1068,12 @@ public String toModelName(String name) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -312,8 +315,14 @@ public String toApiDocFilename(String name) {

@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) {
Expand Down

0 comments on commit d10880c

Please sign in to comment.