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() {
for (CodegenProperty cp : allVars) {
// detect self import
if (cp.dataType.equalsIgnoreCase(this.classname) ||
(cp.isContainer && cp.items.dataType.equalsIgnoreCase(this.classname))) {
this.imports.remove(this.classname); // remove self import
cp.isSelfReference = true;
if (cp == null) {
// TODO cp shouldn't be null. Show a warning message instead
} else {
// detect self import
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) {
// detect self import
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
cp.isSelfReference = true;
}
@ -1507,10 +1507,16 @@ public class DefaultCodegen implements CodegenConfig {
* @return a string presentation of the property type
*/
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);
if (typeMapping.containsKey(oasType)) {
return typeMapping.get(oasType);
}
return oasType;
}

View File

@ -329,12 +329,19 @@ public class ModelUtils {
if (schema instanceof MapSchema) {
return true;
}
if (schema == null) {
return false;
}
if (schema.getAdditionalProperties() instanceof Schema) {
return true;
}
if (schema.getAdditionalProperties() instanceof Boolean && (Boolean) schema.getAdditionalProperties()) {
return true;
}
return false;
}
@ -343,7 +350,7 @@ public class ModelUtils {
return true;
}
// 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 false;