fix npe when array item missing (#2247)

This commit is contained in:
William Cheng 2019-03-02 21:48:00 +08:00 committed by GitHub
parent f47af5e6f0
commit 24f20941e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 7 deletions

View File

@ -621,11 +621,15 @@ public class CodegenModel {
*/ */
public void removeSelfReferenceImport() { public void removeSelfReferenceImport() {
for (CodegenProperty cp : allVars) { for (CodegenProperty cp : allVars) {
// detect self import if (cp == null) {
if (cp.dataType.equalsIgnoreCase(this.classname) || // TODO cp shouldn't be null. Show a warning message instead
(cp.isContainer && cp.items.dataType.equalsIgnoreCase(this.classname))) { } else {
this.imports.remove(this.classname); // remove self import // detect self import
cp.isSelfReference = true; if (this.classname.equalsIgnoreCase(cp.dataType) ||
(cp.isContainer && cp.items != null && this.classname.equalsIgnoreCase(cp.items.dataType))) {
this.imports.remove(this.classname); // remove self import
cp.isSelfReference = true;
}
} }
} }
} }

View File

@ -257,7 +257,7 @@ public class DefaultCodegen implements CodegenConfig {
for (CodegenProperty cp : cm.allVars) { for (CodegenProperty cp : cm.allVars) {
// detect self import // detect self import
if (cp.dataType.equalsIgnoreCase(cm.classname) || if (cp.dataType.equalsIgnoreCase(cm.classname) ||
(cp.isContainer && cp.items.dataType.equalsIgnoreCase(cm.classname))) { (cp.isContainer && cp.items != null && cp.items.dataType.equalsIgnoreCase(cm.classname))) {
cm.imports.remove(cm.classname); // remove self import cm.imports.remove(cm.classname); // remove self import
cp.isSelfReference = true; cp.isSelfReference = true;
} }
@ -1507,10 +1507,16 @@ public class DefaultCodegen implements CodegenConfig {
* @return a string presentation of the property type * @return a string presentation of the property type
*/ */
public String getTypeDeclaration(Schema schema) { public String getTypeDeclaration(Schema schema) {
if (schema == null) {
LOGGER.warn("Null schema found. Default type to `NULL_SCHMEA_ERR`");
return "NULL_SCHMEA_ERR";
}
String oasType = getSchemaType(schema); String oasType = getSchemaType(schema);
if (typeMapping.containsKey(oasType)) { if (typeMapping.containsKey(oasType)) {
return typeMapping.get(oasType); return typeMapping.get(oasType);
} }
return oasType; return oasType;
} }

View File

@ -329,12 +329,19 @@ public class ModelUtils {
if (schema instanceof MapSchema) { if (schema instanceof MapSchema) {
return true; return true;
} }
if (schema == null) {
return false;
}
if (schema.getAdditionalProperties() instanceof Schema) { if (schema.getAdditionalProperties() instanceof Schema) {
return true; return true;
} }
if (schema.getAdditionalProperties() instanceof Boolean && (Boolean) schema.getAdditionalProperties()) { if (schema.getAdditionalProperties() instanceof Boolean && (Boolean) schema.getAdditionalProperties()) {
return true; return true;
} }
return false; return false;
} }
@ -343,7 +350,7 @@ public class ModelUtils {
return true; return true;
} }
// assume it's an array if maxItems, minItems is set // assume it's an array if maxItems, minItems is set
if (schema.getMaxItems() != null || schema.getMinItems() != null) { if (schema != null && (schema.getMaxItems() != null || schema.getMinItems() != null)) {
return true; return true;
} }
return false; return false;