Skip to content

Commit 0b9172e

Browse files
ivanlebivan_leb
andauthored
add cache to efficiently lookup CodegenModel in csharp codegen (#22094)
Co-authored-by: ivan_leb <[email protected]>
1 parent b199901 commit 0b9172e

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen {
117117
// A cache to efficiently lookup schema `toModelName()` based on the schema Key
118118
private final Map<String, String> schemaKeyToModelNameCache = new HashMap<>();
119119

120+
// A cache to efficiently lookup CodegenModel `fromModel(codegenModelName, parentModelSchema)` based on the pair of model name and schema
121+
private final Map<Map.Entry<String, Schema>, CodegenModel> codegenModelNameAndSchemaKeyToCodegenModelCache = new HashMap<>();
122+
120123
public AbstractCSharpCodegen() {
121124
super();
122125

@@ -1704,6 +1707,17 @@ public String toModelTestFilename(String name) {
17041707
return toModelName(name) + "Tests";
17051708
}
17061709

1710+
protected CodegenModel getCodegenModel(String codegenModelName, Schema schema){
1711+
var key = new AbstractMap.SimpleEntry<>(codegenModelName, schema);
1712+
if(codegenModelNameAndSchemaKeyToCodegenModelCache.containsKey(key)){
1713+
return codegenModelNameAndSchemaKeyToCodegenModelCache.get(key);
1714+
}
1715+
1716+
CodegenModel model = super.fromModel(codegenModelName, schema);
1717+
codegenModelNameAndSchemaKeyToCodegenModelCache.put(key, model);
1718+
return model;
1719+
}
1720+
17071721
public void setNullableReferenceTypes(final Boolean nullReferenceTypesFlag) {
17081722
this.nullReferenceTypesFlag = nullReferenceTypesFlag;
17091723
additionalProperties.put("nullableReferenceTypes", nullReferenceTypesFlag);

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpClientCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ public CodegenModel fromModel(String name, Schema model) {
437437
if (allDefinitions != null && codegenModel != null && codegenModel.parent != null) {
438438
final Schema<?> parentModel = allDefinitions.get(toModelName(codegenModel.parent));
439439
if (parentModel != null) {
440-
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel);
440+
final CodegenModel parentCodegenModel = getCodegenModel(codegenModel.parent, parentModel);
441441
if (codegenModel.hasEnums) {
442442
codegenModel = this.reconcileInlineEnums(codegenModel, parentCodegenModel);
443443
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpReducedClientCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public CodegenModel fromModel(String name, Schema model) {
381381
if (allDefinitions != null && codegenModel != null && codegenModel.parent != null) {
382382
final Schema parentModel = allDefinitions.get(toModelName(codegenModel.parent));
383383
if (parentModel != null) {
384-
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel);
384+
final CodegenModel parentCodegenModel = getCodegenModel(codegenModel.parent, parentModel);
385385
if (codegenModel.hasEnums) {
386386
codegenModel = this.reconcileInlineEnums(codegenModel, parentCodegenModel);
387387
}

0 commit comments

Comments
 (0)