@@ -247,6 +247,9 @@ apiTemplateFiles are for API outputs only (controllers/handlers).
247
247
// make openapi available to all methods
248
248
protected OpenAPI openAPI ;
249
249
250
+ // A cache to efficiently lookup a Schema instance based on the return value of `toModelName()`.
251
+ private Map <String , Schema > modelNameToSchemaCache ;
252
+
250
253
public List <CliOption > cliOptions () {
251
254
return cliOptions ;
252
255
}
@@ -450,6 +453,23 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
450
453
return objs ;
451
454
}
452
455
456
+ /**
457
+ * Return a map from model name to Schema for efficient lookup.
458
+ *
459
+ * @return map from model name to Schema.
460
+ */
461
+ protected Map <String , Schema > getModelNameToSchemaCache () {
462
+ if (modelNameToSchemaCache == null ) {
463
+ // Create a cache to efficiently lookup schema based on model name.
464
+ Map <String , Schema > m = new HashMap <String , Schema >();
465
+ ModelUtils .getSchemas (openAPI ).forEach ((key , schema ) -> {
466
+ m .put (toModelName (key ), schema );
467
+ });
468
+ modelNameToSchemaCache = Collections .unmodifiableMap (m );
469
+ }
470
+ return modelNameToSchemaCache ;
471
+ }
472
+
453
473
/**
454
474
* Index all CodegenModels by model name.
455
475
*
@@ -1293,14 +1313,13 @@ public String toOperationId(String operationId) {
1293
1313
* @param name the variable name
1294
1314
* @return the sanitized variable name
1295
1315
*/
1296
- public String toVarName (String name ) {
1316
+ public String toVarName (final String name ) {
1297
1317
if (reservedWords .contains (name )) {
1298
1318
return escapeReservedWord (name );
1299
1319
} else if (((CharSequence ) name ).chars ().anyMatch (character -> specialCharReplacements .keySet ().contains ("" + ((char ) character )))) {
1300
1320
return escape (name , specialCharReplacements , null , null );
1301
- } else {
1302
- return name ;
1303
1321
}
1322
+ return name ;
1304
1323
}
1305
1324
1306
1325
/**
@@ -1318,6 +1337,7 @@ public String toParamName(String name) {
1318
1337
return escape (name , specialCharReplacements , null , null );
1319
1338
}
1320
1339
return name ;
1340
+
1321
1341
}
1322
1342
1323
1343
/**
@@ -2195,7 +2215,8 @@ public String toApiName(String name) {
2195
2215
}
2196
2216
2197
2217
/**
2198
- * Output the proper model name (capitalized).
2218
+ * Converts the OpenAPI schema name to a model name suitable for the current code generator.
2219
+ * May be overriden for each programming language.
2199
2220
* In case the name belongs to the TypeSystem it won't be renamed.
2200
2221
*
2201
2222
* @param name the name of the model
@@ -2205,8 +2226,33 @@ public String toModelName(final String name) {
2205
2226
return camelize (modelNamePrefix + "_" + name + "_" + modelNameSuffix );
2206
2227
}
2207
2228
2229
+ private static class NamedSchema {
2230
+ private NamedSchema (String name , Schema s ) {
2231
+ this .name = name ;
2232
+ this .schema = s ;
2233
+ }
2234
+ private String name ;
2235
+ private Schema schema ;
2236
+
2237
+ @ Override
2238
+ public boolean equals (Object o ) {
2239
+ if (this == o ) return true ;
2240
+ if (o == null || getClass () != o .getClass ()) return false ;
2241
+ NamedSchema that = (NamedSchema ) o ;
2242
+ return Objects .equals (name , that .name ) &&
2243
+ Objects .equals (schema , that .schema );
2244
+ }
2245
+
2246
+ @ Override
2247
+ public int hashCode () {
2248
+ return Objects .hash (name , schema );
2249
+ }
2250
+ }
2251
+
2252
+ Map <NamedSchema , CodegenProperty > schemaCodegenPropertyCache = new HashMap <NamedSchema , CodegenProperty >();
2253
+
2208
2254
/**
2209
- * Convert OAS Model object to Codegen Model object
2255
+ * Convert OAS Model object to Codegen Model object.
2210
2256
*
2211
2257
* @param name the name of the model
2212
2258
* @param schema OAS Model object
@@ -2565,7 +2611,6 @@ public int compare(CodegenProperty one, CodegenProperty another) {
2565
2611
postProcessModelProperty (m , prop );
2566
2612
}
2567
2613
}
2568
-
2569
2614
return m ;
2570
2615
}
2571
2616
@@ -2984,7 +3029,13 @@ public String getterAndSetterCapitalize(String name) {
2984
3029
}
2985
3030
2986
3031
/**
2987
- * Convert OAS Property object to Codegen Property object
3032
+ * Convert OAS Property object to Codegen Property object.
3033
+ *
3034
+ * The return value is cached. An internal cache is looked up to determine
3035
+ * if the CodegenProperty return value has already been instantiated for
3036
+ * the (String name, Schema p) arguments.
3037
+ * Any subsequent processing of the CodegenModel return value must be idempotent
3038
+ * for a given (String name, Schema schema).
2988
3039
*
2989
3040
* @param name name of the property
2990
3041
* @param p OAS property schema
@@ -2996,7 +3047,12 @@ public CodegenProperty fromProperty(String name, Schema p) {
2996
3047
return null ;
2997
3048
}
2998
3049
LOGGER .debug ("debugging fromProperty for " + name + " : " + p );
2999
-
3050
+ NamedSchema ns = new NamedSchema (name , p );
3051
+ CodegenProperty cpc = schemaCodegenPropertyCache .get (ns );
3052
+ if (cpc != null ) {
3053
+ LOGGER .debug ("Cached fromProperty for " + name + " : " + p .getName ());
3054
+ return cpc ;
3055
+ }
3000
3056
// unalias schema
3001
3057
p = unaliasSchema (p , importMapping );
3002
3058
@@ -3296,6 +3352,7 @@ public CodegenProperty fromProperty(String name, Schema p) {
3296
3352
}
3297
3353
3298
3354
LOGGER .debug ("debugging from property return: " + property );
3355
+ schemaCodegenPropertyCache .put (ns , property );
3299
3356
return property ;
3300
3357
}
3301
3358
@@ -4762,7 +4819,6 @@ private void addVars(CodegenModel m, List<CodegenProperty> vars, Map<String, Sch
4762
4819
4763
4820
final String key = entry .getKey ();
4764
4821
final Schema prop = entry .getValue ();
4765
-
4766
4822
if (prop == null ) {
4767
4823
LOGGER .warn ("Please report the issue. There shouldn't be null property for " + key );
4768
4824
} else {
@@ -6382,14 +6438,17 @@ protected void modifyFeatureSet(Consumer<FeatureSet.Builder> processor) {
6382
6438
.featureSet (builder .build ()).build ();
6383
6439
}
6384
6440
6441
+ /**
6442
+ * An map entry for cached sanitized names.
6443
+ */
6385
6444
private static class SanitizeNameOptions {
6386
6445
public SanitizeNameOptions (String name , String removeCharRegEx , List <String > exceptions ) {
6387
6446
this .name = name ;
6388
6447
this .removeCharRegEx = removeCharRegEx ;
6389
6448
if (exceptions != null ) {
6390
6449
this .exceptions = Collections .unmodifiableList (exceptions );
6391
6450
} else {
6392
- this .exceptions = Collections .unmodifiableList ( new ArrayList <>() );
6451
+ this .exceptions = Collections .emptyList ( );
6393
6452
}
6394
6453
}
6395
6454
0 commit comments