Skip to content

Commit d671fe2

Browse files
authored
better null check to avoid NPE (#16588)
1 parent e75e5a2 commit d671fe2

4 files changed

Lines changed: 65 additions & 21 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,11 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
478478
for (Map.Entry<String, ModelsMap> entry : objs.entrySet()) {
479479
CodegenModel model = ModelUtils.getModelByName(entry.getKey(), objs);
480480

481+
if (model == null) {
482+
LOGGER.warn("Null model found in postProcessAllModels: {}", entry.getKey());
483+
continue;
484+
}
485+
481486
// add the model to the discriminator's mapping so templates have access to more than just the string to string mapping
482487
if (model.discriminator != null && model.discriminator.getMappedModels() != null) {
483488
for (CodegenDiscriminator.MappedModel mappedModel : model.discriminator.getMappedModels()) {
@@ -5977,6 +5982,10 @@ Map<String, String> getAllAliases(Map<String, Schema> schemas) {
59775982
}
59785983

59795984
private static Boolean isAliasOfSimpleTypes(Schema schema) {
5985+
if (schema == null) {
5986+
return false;
5987+
}
5988+
59805989
return (!ModelUtils.isObjectSchema(schema)
59815990
&& !ModelUtils.isArraySchema(schema)
59825991
&& !ModelUtils.isMapSchema(schema)

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,8 +1334,10 @@ private ModelsMap processModels(CodegenConfig config, Map<String, Schema> defini
13341334
for (Map.Entry<String, Schema> definitionsEntry : definitions.entrySet()) {
13351335
String key = definitionsEntry.getKey();
13361336
Schema schema = definitionsEntry.getValue();
1337-
if (schema == null)
1338-
throw new RuntimeException("schema cannot be null in processModels");
1337+
if (schema == null) {
1338+
LOGGER.warn("Schema {} cannot be null in processModels", key);
1339+
continue;
1340+
}
13391341
CodegenModel cm = config.fromModel(key, schema);
13401342
ModelMap mo = new ModelMap();
13411343
mo.setModel(cm);

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

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,11 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
286286
if (props != null) {
287287
for (String propName : props.keySet()) {
288288
Schema prop = props.get(propName);
289+
290+
if (prop == null) {
291+
continue;
292+
}
293+
289294
String schemaName = resolveModelName(prop.getTitle(), modelPrefix + "_" + propName);
290295
// Recurse to create $refs for inner models
291296
gatherInlineModels(prop, schemaName);
@@ -308,13 +313,15 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
308313
if (schema.getAdditionalProperties() != null) {
309314
if (schema.getAdditionalProperties() instanceof Schema) {
310315
Schema inner = (Schema) schema.getAdditionalProperties();
311-
String schemaName = resolveModelName(schema.getTitle(), modelPrefix + this.inlineSchemaOptions.get("MAP_ITEM_SUFFIX"));
312-
// Recurse to create $refs for inner models
313-
gatherInlineModels(inner, schemaName);
314-
if (isModelNeeded(inner)) {
315-
// If this schema should be split into its own model, do so
316-
Schema refSchema = this.makeSchemaInComponents(schemaName, inner);
317-
schema.setAdditionalProperties(refSchema);
316+
if (inner != null) {
317+
String schemaName = resolveModelName(schema.getTitle(), modelPrefix + this.inlineSchemaOptions.get("MAP_ITEM_SUFFIX"));
318+
// Recurse to create $refs for inner models
319+
gatherInlineModels(inner, schemaName);
320+
if (isModelNeeded(inner)) {
321+
// If this schema should be split into its own model, do so
322+
Schema refSchema = this.makeSchemaInComponents(schemaName, inner);
323+
schema.setAdditionalProperties(refSchema);
324+
}
318325
}
319326
}
320327
}
@@ -334,10 +341,6 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
334341
if (schema instanceof ArraySchema) {
335342
ArraySchema array = (ArraySchema) schema;
336343
Schema items = array.getItems();
337-
/*if (items.getTitle() != null) {
338-
LOGGER.info("schema title {}", items);
339-
throw new RuntimeException("getTitle for array item is not null");
340-
}*/
341344
if (items == null) {
342345
LOGGER.error("Illegal schema found with array type but no items," +
343346
" items must be defined for array schemas:\n " + schema.toString());
@@ -360,6 +363,9 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
360363
List<Schema> newAllOf = new ArrayList<Schema>();
361364
boolean atLeastOneModel = false;
362365
for (Object inner : schema.getAllOf()) {
366+
if (inner == null) {
367+
continue;
368+
}
363369
String schemaName = resolveModelName(((Schema) inner).getTitle(), modelPrefix + "_allOf");
364370
// Recurse to create $refs for inner models
365371
gatherInlineModels((Schema) inner, schemaName);
@@ -392,6 +398,9 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
392398
if (schema.getAnyOf() != null) {
393399
List<Schema> newAnyOf = new ArrayList<Schema>();
394400
for (Object inner : schema.getAnyOf()) {
401+
if (inner == null) {
402+
continue;
403+
}
395404
String schemaName = resolveModelName(((Schema) inner).getTitle(), modelPrefix + "_anyOf");
396405
// Recurse to create $refs for inner models
397406
gatherInlineModels((Schema) inner, schemaName);
@@ -407,6 +416,9 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
407416
if (schema.getOneOf() != null) {
408417
List<Schema> newOneOf = new ArrayList<Schema>();
409418
for (Object inner : schema.getOneOf()) {
419+
if (inner == null) {
420+
continue;
421+
}
410422
String schemaName = resolveModelName(((Schema) inner).getTitle(), modelPrefix + "_oneOf");
411423
// Recurse to create $refs for inner models
412424
gatherInlineModels((Schema) inner, schemaName);
@@ -423,12 +435,14 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
423435
// Check not schema
424436
if (schema.getNot() != null) {
425437
Schema not = schema.getNot();
426-
String schemaName = resolveModelName(schema.getTitle(), modelPrefix + "_not");
427-
// Recurse to create $refs for inner models
428-
gatherInlineModels(not, schemaName);
429-
if (isModelNeeded(not)) {
430-
Schema refSchema = this.makeSchemaInComponents(schemaName, not);
431-
schema.setNot(refSchema);
438+
if (not != null) {
439+
String schemaName = resolveModelName(schema.getTitle(), modelPrefix + "_not");
440+
// Recurse to create $refs for inner models
441+
gatherInlineModels(not, schemaName);
442+
if (isModelNeeded(not)) {
443+
Schema refSchema = this.makeSchemaInComponents(schemaName, not);
444+
schema.setNot(refSchema);
445+
}
432446
}
433447
}
434448
}
@@ -629,6 +643,9 @@ private void flattenComponents() {
629643
List<String> modelNames = new ArrayList<String>(models.keySet());
630644
for (String modelName : modelNames) {
631645
Schema model = models.get(modelName);
646+
if (model == null) {
647+
continue;
648+
}
632649
if (ModelUtils.isAnyOf(model)) { // contains anyOf only
633650
gatherInlineModels(model, modelName);
634651
} else if (ModelUtils.isOneOf(model)) { // contains oneOf only

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,10 @@ public static boolean isTypeObjectSchema(Schema schema) {
438438
* @return true if the specified schema is an Object schema.
439439
*/
440440
public static boolean isObjectSchema(Schema schema) {
441+
if (schema == null) {
442+
return false;
443+
}
444+
441445
return (schema instanceof ObjectSchema) ||
442446
// must not be a map
443447
(SchemaTypeUtil.OBJECT_TYPE.equals(schema.getType()) && !(schema instanceof MapSchema)) ||
@@ -549,6 +553,10 @@ public static boolean isComplexComposedSchema(Schema schema) {
549553
* @return true if the specified schema is a Map schema.
550554
*/
551555
public static boolean isMapSchema(Schema schema) {
556+
if (schema == null) {
557+
return false;
558+
}
559+
552560
// additionalProperties explicitly set to false
553561
if (schema.getAdditionalProperties() instanceof Boolean && Boolean.FALSE.equals(schema.getAdditionalProperties())) {
554562
return false;
@@ -1858,6 +1866,10 @@ public static boolean isAllOfWithProperties(Schema schema) {
18581866
* @return true if the schema contains oneOf but no properties/allOf/anyOf defined.
18591867
*/
18601868
public static boolean isOneOf(Schema schema) {
1869+
if (schema == null) {
1870+
return false;
1871+
}
1872+
18611873
if (hasOneOf(schema) && (schema.getProperties() == null || schema.getProperties().isEmpty()) &&
18621874
(schema.getAllOf() == null || schema.getAllOf().isEmpty()) &&
18631875
(schema.getAnyOf() == null || schema.getAnyOf().isEmpty())) {
@@ -1875,7 +1887,7 @@ public static boolean isOneOf(Schema schema) {
18751887
* @return true if allOf is not empty
18761888
*/
18771889
public static boolean hasOneOf(Schema schema) {
1878-
if (schema.getOneOf() != null && !schema.getOneOf().isEmpty()) {
1890+
if (schema != null && schema.getOneOf() != null && !schema.getOneOf().isEmpty()) {
18791891
return true;
18801892
}
18811893

@@ -1890,6 +1902,10 @@ public static boolean hasOneOf(Schema schema) {
18901902
* @return true if the schema contains oneOf but no properties/allOf/anyOf defined.
18911903
*/
18921904
public static boolean isAnyOf(Schema schema) {
1905+
if (schema == null) {
1906+
return false;
1907+
}
1908+
18931909
if (hasAnyOf(schema) && (schema.getProperties() == null || schema.getProperties().isEmpty()) &&
18941910
(schema.getAllOf() == null || schema.getAllOf().isEmpty()) &&
18951911
(schema.getOneOf() == null || schema.getOneOf().isEmpty())) {
@@ -1907,7 +1923,7 @@ public static boolean isAnyOf(Schema schema) {
19071923
* @return true if anyOf is not empty
19081924
*/
19091925
public static boolean hasAnyOf(Schema schema) {
1910-
if (schema.getAnyOf() != null && !schema.getAnyOf().isEmpty()) {
1926+
if (schema != null && schema.getAnyOf() != null && !schema.getAnyOf().isEmpty()) {
19111927
return true;
19121928
}
19131929

0 commit comments

Comments
 (0)