Skip to content

Commit

Permalink
Revert "[codegen][Python] Add model cache to speed up code generation (
Browse files Browse the repository at this point in the history
…#7220)" (#7245)

This reverts commit bee0368.
  • Loading branch information
wing328 authored Aug 19, 2020
1 parent bee0368 commit 98c606c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,6 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
// make openapi available to all methods
protected OpenAPI openAPI;

// A cache to efficiently lookup a Schema instance based on the return value of `toModelName()`.
private Map<String, Schema> modelNameToSchemaCache;

public List<CliOption> cliOptions() {
return cliOptions;
}
Expand Down Expand Up @@ -452,23 +449,6 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
return objs;
}

/**
* Return a map from model name to Schema for efficient lookup.
*
* @return map from model name to Schema.
*/
protected Map<String, Schema> getModelNameToSchemaCache() {
if (modelNameToSchemaCache == null) {
// Create a cache to efficiently lookup schema based on model name.
Map<String, Schema> m = new HashMap<String, Schema>();
ModelUtils.getSchemas(openAPI).forEach((key, schema) -> {
m.put(toModelName(key), schema);
});
modelNameToSchemaCache = Collections.unmodifiableMap(m);
}
return modelNameToSchemaCache;
}

/**
* Index all CodegenModels by model name.
*
Expand Down Expand Up @@ -1312,13 +1292,14 @@ public String toOperationId(String operationId) {
* @param name the variable name
* @return the sanitized variable name
*/
public String toVarName(final String name) {
public String toVarName(String name) {
if (reservedWords.contains(name)) {
return escapeReservedWord(name);
} else if (((CharSequence) name).chars().anyMatch(character -> specialCharReplacements.keySet().contains("" + ((char) character)))) {
return escape(name, specialCharReplacements, null, null);
} else {
return name;
}
return name;
}

/**
Expand All @@ -1328,15 +1309,14 @@ public String toVarName(final String name) {
* @param name Codegen property object
* @return the sanitized parameter name
*/
public String toParamName(final String name) {
String modifiable = removeNonNameElementToCamelCase(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
if (reservedWords.contains(modifiable)) {
return escapeReservedWord(modifiable);
} else if (((CharSequence) modifiable).chars().anyMatch(character -> specialCharReplacements.keySet().contains("" + ((char) character)))) {
return escape(modifiable, specialCharReplacements, null, null);
public String toParamName(String name) {
name = removeNonNameElementToCamelCase(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
if (reservedWords.contains(name)) {
return escapeReservedWord(name);
} else if (((CharSequence) name).chars().anyMatch(character -> specialCharReplacements.keySet().contains("" + ((char) character)))) {
return escape(name, specialCharReplacements, null, null);
}
return modifiable;

return name;
}

/**
Expand Down Expand Up @@ -2198,8 +2178,7 @@ public String toApiName(String name) {
}

/**
* Converts the OpenAPI schema name to a model name suitable for the current code generator.
* May be overriden for each programming language.
* Output the proper model name (capitalized).
* In case the name belongs to the TypeSystem it won't be renamed.
*
* @param name the name of the model
Expand All @@ -2209,32 +2188,6 @@ public String toModelName(final String name) {
return camelize(modelNamePrefix + "_" + name + "_" + modelNameSuffix);
}

private static class NamedSchema {
private NamedSchema(String name, Schema s) {
this.name = name;
this.schema = s;
}
private String name;
private Schema schema;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NamedSchema that = (NamedSchema) o;
return Objects.equals(name, that.name) &&
Objects.equals(schema, that.schema);
}

@Override
public int hashCode() {
return Objects.hash(name, schema);
}
}

Map<NamedSchema, CodegenModel> schemaCodegenModelCache = new HashMap<NamedSchema, CodegenModel>();
Map<NamedSchema, CodegenProperty> schemaCodegenPropertyCache = new HashMap<NamedSchema, CodegenProperty>();

/**
* Convert OAS Model object to Codegen Model object
*
Expand All @@ -2243,13 +2196,6 @@ public int hashCode() {
* @return Codegen Model object
*/
public CodegenModel fromModel(String name, Schema schema) {
NamedSchema ns = new NamedSchema(name, schema);
CodegenModel cm = schemaCodegenModelCache.get(ns);
if (cm != null) {
LOGGER.debug("Cached fromModel for " + name + " : " + schema.getName());
return cm;
}

Map<String, Schema> allDefinitions = ModelUtils.getSchemas(this.openAPI);
if (typeAliases == null) {
// Only do this once during first call
Expand Down Expand Up @@ -2598,7 +2544,7 @@ public int compare(CodegenProperty one, CodegenProperty another) {
postProcessModelProperty(m, prop);
}
}
schemaCodegenModelCache.put(ns, m);

return m;
}

Expand Down Expand Up @@ -3030,12 +2976,7 @@ public CodegenProperty fromProperty(String name, Schema p) {
return null;
}
LOGGER.debug("debugging fromProperty for " + name + " : " + p);
NamedSchema ns = new NamedSchema(name, p);
CodegenProperty c = schemaCodegenPropertyCache.get(ns);
if (c != null) {
LOGGER.debug("Cached fromProperty for " + name + " : " + p.getName());
return c;
}

// unalias schema
p = ModelUtils.unaliasSchema(this.openAPI, p, importMapping);

Expand Down Expand Up @@ -3337,7 +3278,6 @@ public CodegenProperty fromProperty(String name, Schema p) {
}

LOGGER.debug("debugging from property return: " + property);
schemaCodegenPropertyCache.put(ns, property);
return property;
}

Expand Down Expand Up @@ -6416,17 +6356,14 @@ protected void modifyFeatureSet(Consumer<FeatureSet.Builder> processor) {
.featureSet(builder.build()).build();
}

/**
* An map entry for cached sanitized names.
*/
private static class SanitizeNameOptions {
public SanitizeNameOptions(String name, String removeCharRegEx, List<String> exceptions) {
this.name = name;
this.removeCharRegEx = removeCharRegEx;
if (exceptions != null) {
this.exceptions = Collections.unmodifiableList(exceptions);
} else {
this.exceptions = Collections.emptyList();
this.exceptions = Collections.unmodifiableList(new ArrayList<>());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,16 +447,19 @@ public void updateCodegenPropertyEnum(CodegenProperty var) {
}

String varDataType = var.mostInnerItems != null ? var.mostInnerItems.dataType : var.dataType;
Schema referencedSchema = getModelNameToSchemaCache().get(varDataType);
String dataType = (referencedSchema != null) ? getTypeDeclaration(referencedSchema) : varDataType;
Optional<Schema> referencedSchema = ModelUtils.getSchemas(openAPI).entrySet().stream()
.filter(entry -> Objects.equals(varDataType, toModelName(entry.getKey())))
.map(Map.Entry::getValue)
.findFirst();
String dataType = (referencedSchema.isPresent()) ? getTypeDeclaration(referencedSchema.get()) : varDataType;

// put "enumVars" map into `allowableValues", including `name` and `value`
List<Map<String, Object>> enumVars = buildEnumVars(values, dataType);

// if "x-enum-varnames" or "x-enum-descriptions" defined, update varnames
Map<String, Object> extensions = var.mostInnerItems != null ? var.mostInnerItems.getVendorExtensions() : var.getVendorExtensions();
if (referencedSchema != null) {
extensions = referencedSchema.getExtensions();
if (referencedSchema.isPresent()) {
extensions = referencedSchema.get().getExtensions();
}
updateEnumVarsWithExtensions(enumVars, extensions);
allowableValues.put("enumVars", enumVars);
Expand Down
Loading

0 comments on commit 98c606c

Please sign in to comment.