diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 3c58a95deeae..e0e409926dd1 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -478,6 +478,11 @@ public Map postProcessAllModels(Map objs) for (Map.Entry entry : objs.entrySet()) { CodegenModel model = ModelUtils.getModelByName(entry.getKey(), objs); + if (model == null) { + LOGGER.warn("Null model found in postProcessAllModels: {}", entry.getKey()); + continue; + } + // add the model to the discriminator's mapping so templates have access to more than just the string to string mapping if (model.discriminator != null && model.discriminator.getMappedModels() != null) { for (CodegenDiscriminator.MappedModel mappedModel : model.discriminator.getMappedModels()) { @@ -5977,6 +5982,10 @@ Map getAllAliases(Map schemas) { } private static Boolean isAliasOfSimpleTypes(Schema schema) { + if (schema == null) { + return false; + } + return (!ModelUtils.isObjectSchema(schema) && !ModelUtils.isArraySchema(schema) && !ModelUtils.isMapSchema(schema) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java index 471b0364fe0c..be8aeeae5472 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java @@ -1334,8 +1334,10 @@ private ModelsMap processModels(CodegenConfig config, Map defini for (Map.Entry definitionsEntry : definitions.entrySet()) { String key = definitionsEntry.getKey(); Schema schema = definitionsEntry.getValue(); - if (schema == null) - throw new RuntimeException("schema cannot be null in processModels"); + if (schema == null) { + LOGGER.warn("Schema {} cannot be null in processModels", key); + continue; + } CodegenModel cm = config.fromModel(key, schema); ModelMap mo = new ModelMap(); mo.setModel(cm); diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java index e395be243324..20ce9f3ad403 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java @@ -286,6 +286,11 @@ private void gatherInlineModels(Schema schema, String modelPrefix) { if (props != null) { for (String propName : props.keySet()) { Schema prop = props.get(propName); + + if (prop == null) { + continue; + } + String schemaName = resolveModelName(prop.getTitle(), modelPrefix + "_" + propName); // Recurse to create $refs for inner models gatherInlineModels(prop, schemaName); @@ -308,13 +313,15 @@ private void gatherInlineModels(Schema schema, String modelPrefix) { if (schema.getAdditionalProperties() != null) { if (schema.getAdditionalProperties() instanceof Schema) { Schema inner = (Schema) schema.getAdditionalProperties(); - String schemaName = resolveModelName(schema.getTitle(), modelPrefix + this.inlineSchemaOptions.get("MAP_ITEM_SUFFIX")); - // Recurse to create $refs for inner models - gatherInlineModels(inner, schemaName); - if (isModelNeeded(inner)) { - // If this schema should be split into its own model, do so - Schema refSchema = this.makeSchemaInComponents(schemaName, inner); - schema.setAdditionalProperties(refSchema); + if (inner != null) { + String schemaName = resolveModelName(schema.getTitle(), modelPrefix + this.inlineSchemaOptions.get("MAP_ITEM_SUFFIX")); + // Recurse to create $refs for inner models + gatherInlineModels(inner, schemaName); + if (isModelNeeded(inner)) { + // If this schema should be split into its own model, do so + Schema refSchema = this.makeSchemaInComponents(schemaName, inner); + schema.setAdditionalProperties(refSchema); + } } } } @@ -334,10 +341,6 @@ private void gatherInlineModels(Schema schema, String modelPrefix) { if (schema instanceof ArraySchema) { ArraySchema array = (ArraySchema) schema; Schema items = array.getItems(); - /*if (items.getTitle() != null) { - LOGGER.info("schema title {}", items); - throw new RuntimeException("getTitle for array item is not null"); - }*/ if (items == null) { LOGGER.error("Illegal schema found with array type but no items," + " items must be defined for array schemas:\n " + schema.toString()); @@ -360,6 +363,9 @@ private void gatherInlineModels(Schema schema, String modelPrefix) { List newAllOf = new ArrayList(); boolean atLeastOneModel = false; for (Object inner : schema.getAllOf()) { + if (inner == null) { + continue; + } String schemaName = resolveModelName(((Schema) inner).getTitle(), modelPrefix + "_allOf"); // Recurse to create $refs for inner models gatherInlineModels((Schema) inner, schemaName); @@ -392,6 +398,9 @@ private void gatherInlineModels(Schema schema, String modelPrefix) { if (schema.getAnyOf() != null) { List newAnyOf = new ArrayList(); for (Object inner : schema.getAnyOf()) { + if (inner == null) { + continue; + } String schemaName = resolveModelName(((Schema) inner).getTitle(), modelPrefix + "_anyOf"); // Recurse to create $refs for inner models gatherInlineModels((Schema) inner, schemaName); @@ -407,6 +416,9 @@ private void gatherInlineModels(Schema schema, String modelPrefix) { if (schema.getOneOf() != null) { List newOneOf = new ArrayList(); for (Object inner : schema.getOneOf()) { + if (inner == null) { + continue; + } String schemaName = resolveModelName(((Schema) inner).getTitle(), modelPrefix + "_oneOf"); // Recurse to create $refs for inner models gatherInlineModels((Schema) inner, schemaName); @@ -423,12 +435,14 @@ private void gatherInlineModels(Schema schema, String modelPrefix) { // Check not schema if (schema.getNot() != null) { Schema not = schema.getNot(); - String schemaName = resolveModelName(schema.getTitle(), modelPrefix + "_not"); - // Recurse to create $refs for inner models - gatherInlineModels(not, schemaName); - if (isModelNeeded(not)) { - Schema refSchema = this.makeSchemaInComponents(schemaName, not); - schema.setNot(refSchema); + if (not != null) { + String schemaName = resolveModelName(schema.getTitle(), modelPrefix + "_not"); + // Recurse to create $refs for inner models + gatherInlineModels(not, schemaName); + if (isModelNeeded(not)) { + Schema refSchema = this.makeSchemaInComponents(schemaName, not); + schema.setNot(refSchema); + } } } } @@ -629,6 +643,9 @@ private void flattenComponents() { List modelNames = new ArrayList(models.keySet()); for (String modelName : modelNames) { Schema model = models.get(modelName); + if (model == null) { + continue; + } if (ModelUtils.isAnyOf(model)) { // contains anyOf only gatherInlineModels(model, modelName); } else if (ModelUtils.isOneOf(model)) { // contains oneOf only diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index e3edb79ff39b..838df6ce5e09 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -438,6 +438,10 @@ public static boolean isTypeObjectSchema(Schema schema) { * @return true if the specified schema is an Object schema. */ public static boolean isObjectSchema(Schema schema) { + if (schema == null) { + return false; + } + return (schema instanceof ObjectSchema) || // must not be a map (SchemaTypeUtil.OBJECT_TYPE.equals(schema.getType()) && !(schema instanceof MapSchema)) || @@ -549,6 +553,10 @@ public static boolean isComplexComposedSchema(Schema schema) { * @return true if the specified schema is a Map schema. */ public static boolean isMapSchema(Schema schema) { + if (schema == null) { + return false; + } + // additionalProperties explicitly set to false if (schema.getAdditionalProperties() instanceof Boolean && Boolean.FALSE.equals(schema.getAdditionalProperties())) { return false; @@ -1858,6 +1866,10 @@ public static boolean isAllOfWithProperties(Schema schema) { * @return true if the schema contains oneOf but no properties/allOf/anyOf defined. */ public static boolean isOneOf(Schema schema) { + if (schema == null) { + return false; + } + if (hasOneOf(schema) && (schema.getProperties() == null || schema.getProperties().isEmpty()) && (schema.getAllOf() == null || schema.getAllOf().isEmpty()) && (schema.getAnyOf() == null || schema.getAnyOf().isEmpty())) { @@ -1875,7 +1887,7 @@ public static boolean isOneOf(Schema schema) { * @return true if allOf is not empty */ public static boolean hasOneOf(Schema schema) { - if (schema.getOneOf() != null && !schema.getOneOf().isEmpty()) { + if (schema != null && schema.getOneOf() != null && !schema.getOneOf().isEmpty()) { return true; } @@ -1890,6 +1902,10 @@ public static boolean hasOneOf(Schema schema) { * @return true if the schema contains oneOf but no properties/allOf/anyOf defined. */ public static boolean isAnyOf(Schema schema) { + if (schema == null) { + return false; + } + if (hasAnyOf(schema) && (schema.getProperties() == null || schema.getProperties().isEmpty()) && (schema.getAllOf() == null || schema.getAllOf().isEmpty()) && (schema.getOneOf() == null || schema.getOneOf().isEmpty())) { @@ -1907,7 +1923,7 @@ public static boolean isAnyOf(Schema schema) { * @return true if anyOf is not empty */ public static boolean hasAnyOf(Schema schema) { - if (schema.getAnyOf() != null && !schema.getAnyOf().isEmpty()) { + if (schema != null && schema.getAnyOf() != null && !schema.getAnyOf().isEmpty()) { return true; }