forked from loafle/openapi-generator-original
This PR adds memoization to Java **toModelName()** method. (#12909)
### PR checklist - [x] Read the [contribution guidelines](https://github.com/openapitools/openapi-generator/blob/master/CONTRIBUTING.md). - [x] Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community. - [x] Run the following to [build the project](https://github.com/OpenAPITools/openapi-generator#14---build-projects) and update samples: ``` ./mvnw clean package ./bin/generate-samples.sh ./bin/utils/export_docs_generators.sh ``` Commit all changed files. This is important, as CI jobs will verify _all_ generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example `./bin/generate-samples.sh bin/configs/java*`. For Windows users, please run the script in [Git BASH](https://gitforwindows.org/). - [x] File the PR against the [correct branch](https://github.com/OpenAPITools/openapi-generator/wiki/Git-Branches): `master` (6.1.0) (minor release - breaking changes with fallbacks), `7.0.x` (breaking changes without fallbacks) - [x] If your PR is targeting a particular programming language, @mention the [technical committee](https://github.com/openapitools/openapi-generator/#62---openapi-generator-technical-committee) members, so they are more likely to review the pull request. This PR greatly reduces the time taken for Java code generation. **toModelName()** method is invoked numerous times and it was accumulating a lot of time. This has reduced the Java SDK generation time for our rather large open API spec from ~3h30m hours to 4 mins. The generated code is the same as before. spec: https://cdn.intersight.com/components/an-apidocs/1.0.11-7546/model/intersight-openapi-v3-1.0.11-7546.yaml @wing328
This commit is contained in:
parent
b88666b87a
commit
259221ab25
@ -130,6 +130,8 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
||||
protected boolean implicitHeaders = false;
|
||||
protected String implicitHeadersRegex = null;
|
||||
|
||||
private Map<String, String> schemaKeyToModelNameCache = new HashMap<>();
|
||||
|
||||
public AbstractJavaCodegen() {
|
||||
super();
|
||||
|
||||
@ -830,6 +832,12 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
||||
return schemaMapping.get(name);
|
||||
}
|
||||
|
||||
// memoization
|
||||
String origName = name;
|
||||
if (schemaKeyToModelNameCache.containsKey(origName)) {
|
||||
return schemaKeyToModelNameCache.get(origName);
|
||||
}
|
||||
|
||||
final String sanitizedName = sanitizeName(name);
|
||||
|
||||
String nameWithPrefixSuffix = sanitizedName;
|
||||
@ -850,6 +858,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
||||
// model name cannot use reserved keyword, e.g. return
|
||||
if (isReservedWord(camelizedName)) {
|
||||
final String modelName = "Model" + camelizedName;
|
||||
schemaKeyToModelNameCache.put(origName, modelName);
|
||||
LOGGER.warn("{} (reserved word) cannot be used as model name. Renamed to {}", camelizedName, modelName);
|
||||
return modelName;
|
||||
}
|
||||
@ -857,11 +866,14 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
||||
// model name starts with number
|
||||
if (camelizedName.matches("^\\d.*")) {
|
||||
final String modelName = "Model" + camelizedName; // e.g. 200Response => Model200Response (after camelize)
|
||||
schemaKeyToModelNameCache.put(origName, modelName);
|
||||
LOGGER.warn("{} (model name starts with number) cannot be used as model name. Renamed to {}", name,
|
||||
modelName);
|
||||
return modelName;
|
||||
}
|
||||
|
||||
schemaKeyToModelNameCache.put(origName, camelizedName);
|
||||
|
||||
return camelizedName;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user