Skip to content

Commit f0b2dc3

Browse files
author
ivan_leb
committed
add cache to efficiently lookup CodegenModel in csharp codegen
1 parent 6b1b5cc commit f0b2dc3

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

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
@@ -116,6 +116,9 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen {
116116
// A cache to efficiently lookup schema `toModelName()` based on the schema Key
117117
private final Map<String, String> schemaKeyToModelNameCache = new HashMap<>();
118118

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

@@ -1703,6 +1706,17 @@ public String toModelTestFilename(String name) {
17031706
return toModelName(name) + "Tests";
17041707
}
17051708

1709+
protected CodegenModel getCodegenModel(String codegenModelName, Schema schema){
1710+
var key = new AbstractMap.SimpleEntry<>(codegenModelName, schema);
1711+
if(codegenModelNameAndSchemaKeyToCodegenModelCache.containsKey(key)){
1712+
return codegenModelNameAndSchemaKeyToCodegenModelCache.get(key);
1713+
}
1714+
1715+
CodegenModel model = super.fromModel(codegenModelName, schema);
1716+
codegenModelNameAndSchemaKeyToCodegenModelCache.put(key, model);
1717+
return model;
1718+
}
1719+
17061720
public void setNullableReferenceTypes(final Boolean nullReferenceTypesFlag) {
17071721
this.nullReferenceTypesFlag = nullReferenceTypesFlag;
17081722
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
@@ -436,7 +436,7 @@ public CodegenModel fromModel(String name, Schema model) {
436436
if (allDefinitions != null && codegenModel != null && codegenModel.parent != null) {
437437
final Schema<?> parentModel = allDefinitions.get(toModelName(codegenModel.parent));
438438
if (parentModel != null) {
439-
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel);
439+
final CodegenModel parentCodegenModel = getCodegenModel(codegenModel.parent, parentModel);
440440
if (codegenModel.hasEnums) {
441441
codegenModel = this.reconcileInlineEnums(codegenModel, parentCodegenModel);
442442
}

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
@@ -380,7 +380,7 @@ public CodegenModel fromModel(String name, Schema model) {
380380
if (allDefinitions != null && codegenModel != null && codegenModel.parent != null) {
381381
final Schema parentModel = allDefinitions.get(toModelName(codegenModel.parent));
382382
if (parentModel != null) {
383-
final CodegenModel parentCodegenModel = super.fromModel(codegenModel.parent, parentModel);
383+
final CodegenModel parentCodegenModel = getCodegenModel(codegenModel.parent, parentModel);
384384
if (codegenModel.hasEnums) {
385385
codegenModel = this.reconcileInlineEnums(codegenModel, parentCodegenModel);
386386
}

0 commit comments

Comments
 (0)