better handling of undefined inner property in array (#2635)

This commit is contained in:
William Cheng 2019-04-11 19:06:48 +08:00 committed by GitHub
parent b797662eaf
commit b426bab85e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 10 deletions

View File

@ -1878,7 +1878,7 @@ public class DefaultCodegen implements CodegenConfig {
*/ */
public CodegenProperty fromProperty(String name, Schema p) { public CodegenProperty fromProperty(String name, Schema p) {
if (p == null) { if (p == null) {
LOGGER.error("Unexpected missing property for name " + name); LOGGER.error("Undefined property/schema for `{}`. Default to type:string.", name);
return null; return null;
} }
LOGGER.debug("debugging fromProperty for " + name + " : " + p); LOGGER.debug("debugging fromProperty for " + name + " : " + p);
@ -2022,6 +2022,21 @@ public class DefaultCodegen implements CodegenConfig {
} else if (ModelUtils.isFreeFormObject(p)) { } else if (ModelUtils.isFreeFormObject(p)) {
property.isFreeFormObject = true; property.isFreeFormObject = true;
} else if (ModelUtils.isArraySchema(p)) {
// default to string if inner item is undefined
Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, ((ArraySchema) p).getItems());
if (innerSchema == null) {
LOGGER.error("Undefined array inner type for `{}`. Default to String.", p.getName());
innerSchema = new StringSchema().description("//TODO automatically added by openapi-generator due to undefined type");
((ArraySchema)p).setItems(innerSchema);
}
} else if (ModelUtils.isMapSchema(p)) {
Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, ModelUtils.getAdditionalProperties(p));
if (innerSchema == null) {
LOGGER.error("Undefined map inner type for `{}`. Default to String.", p.getName());
innerSchema = new StringSchema().description("//TODO automatically added by openapi-generator due to undefined type");
p.setAdditionalProperties(innerSchema);
}
} }
//Inline enum case: //Inline enum case:
@ -2092,6 +2107,11 @@ public class DefaultCodegen implements CodegenConfig {
itemName = property.name; itemName = property.name;
} }
Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, ((ArraySchema) p).getItems()); Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, ((ArraySchema) p).getItems());
if (innerSchema == null) {
LOGGER.error("Undefined array inner type for `{}`. Default to String.", p.getName());
innerSchema = new StringSchema().description("//TODO automatically added by openapi-generator due to undefined type");
((ArraySchema)p).setItems(innerSchema);
}
CodegenProperty cp = fromProperty(itemName, innerSchema); CodegenProperty cp = fromProperty(itemName, innerSchema);
updatePropertyForArray(property, cp); updatePropertyForArray(property, cp);
} else if (ModelUtils.isMapSchema(p)) { } else if (ModelUtils.isMapSchema(p)) {
@ -2104,6 +2124,11 @@ public class DefaultCodegen implements CodegenConfig {
// handle inner property // handle inner property
Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, ModelUtils.getAdditionalProperties(p)); Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, ModelUtils.getAdditionalProperties(p));
if (innerSchema == null) {
LOGGER.error("Undefined map inner type for `{}`. Default to String.", p.getName());
innerSchema = new StringSchema().description("//TODO automatically added by openapi-generator due to undefined type");
p.setAdditionalProperties(innerSchema);
}
CodegenProperty cp = fromProperty("inner", innerSchema); CodegenProperty cp = fromProperty("inner", innerSchema);
updatePropertyForMap(property, cp); updatePropertyForMap(property, cp);
} else if (ModelUtils.isFreeFormObject(p)) { } else if (ModelUtils.isFreeFormObject(p)) {
@ -4359,8 +4384,8 @@ public class DefaultCodegen implements CodegenConfig {
final ArraySchema arraySchema = (ArraySchema) s; final ArraySchema arraySchema = (ArraySchema) s;
Schema inner = arraySchema.getItems(); Schema inner = arraySchema.getItems();
if (inner == null) { if (inner == null) {
LOGGER.warn("warning! No inner type supplied for array parameter \"" + s.getName() + "\", using String"); LOGGER.error("No inner type supplied for array parameter `{}`. Default to type:string", s.getName());
inner = new StringSchema().description("//TODO automatically added by openapi-generator due to missing iner type definition in the spec"); inner = new StringSchema().description("//TODO automatically added by openapi-generator due to missing inner type definition in the spec");
arraySchema.setItems(inner); arraySchema.setItems(inner);
} }
@ -4373,7 +4398,7 @@ public class DefaultCodegen implements CodegenConfig {
codegenParameter.isContainer = true; codegenParameter.isContainer = true;
codegenParameter.isListContainer = true; codegenParameter.isListContainer = true;
codegenParameter.description = escapeText(s.getDescription()); codegenParameter.description = escapeText(s.getDescription());
codegenParameter.dataType = getTypeDeclaration(s); codegenParameter.dataType = getTypeDeclaration(arraySchema);
if (codegenParameter.baseType != null && codegenParameter.enumName != null) { if (codegenParameter.baseType != null && codegenParameter.enumName != null) {
codegenParameter.datatypeWithEnum = codegenParameter.dataType.replace(codegenParameter.baseType, codegenParameter.enumName); codegenParameter.datatypeWithEnum = codegenParameter.dataType.replace(codegenParameter.baseType, codegenParameter.enumName);
} else { } else {
@ -4522,6 +4547,7 @@ public class DefaultCodegen implements CodegenConfig {
if (ModelUtils.isMapSchema(schema)) { if (ModelUtils.isMapSchema(schema)) {
Schema inner = ModelUtils.getAdditionalProperties(schema); Schema inner = ModelUtils.getAdditionalProperties(schema);
if (inner == null) { if (inner == null) {
LOGGER.error("No inner type supplied for map parameter `{}`. Default to type:string", schema.getName());
inner = new StringSchema().description("//TODO automatically added by openapi-generator"); inner = new StringSchema().description("//TODO automatically added by openapi-generator");
schema.setAdditionalProperties(inner); schema.setAdditionalProperties(inner);
} }
@ -4550,10 +4576,11 @@ public class DefaultCodegen implements CodegenConfig {
final ArraySchema arraySchema = (ArraySchema) schema; final ArraySchema arraySchema = (ArraySchema) schema;
Schema inner = arraySchema.getItems(); Schema inner = arraySchema.getItems();
if (inner == null) { if (inner == null) {
inner = new StringSchema().description("//TODO automatically added by openapi-generator"); LOGGER.error("No inner type supplied for array parameter `{}`. Default to type:string", schema.getName());
inner = new StringSchema().description("//TODO automatically added by openapi-generator due to undefined type");
arraySchema.setItems(inner); arraySchema.setItems(inner);
} }
CodegenProperty codegenProperty = fromProperty("property", schema); CodegenProperty codegenProperty = fromProperty("property", arraySchema);
imports.add(codegenProperty.baseType); imports.add(codegenProperty.baseType);
CodegenProperty innerCp = codegenProperty; CodegenProperty innerCp = codegenProperty;
CodegenProperty mostInnerItem = innerCp; CodegenProperty mostInnerItem = innerCp;

View File

@ -708,15 +708,17 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
ArraySchema ap = (ArraySchema) p; ArraySchema ap = (ArraySchema) p;
Schema inner = ap.getItems(); Schema inner = ap.getItems();
if (inner == null) { if (inner == null) {
LOGGER.warn(ap.getName() + "(array property) does not have a proper inner type defined.Default to string"); LOGGER.error("`{}` (array property) does not have a proper inner type defined. Default to type:string", ap.getName());
inner = new StringSchema().description("TODO default missing array inner type to string"); inner = new StringSchema().description("TODO default missing array inner type to string");
ap.setItems(inner);
} }
return getSchemaType(p) + "<" + getTypeDeclaration(inner) + ">"; return getSchemaType(p) + "<" + getTypeDeclaration(inner) + ">";
} else if (ModelUtils.isMapSchema(p)) { } else if (ModelUtils.isMapSchema(p)) {
Schema inner = ModelUtils.getAdditionalProperties(p); Schema inner = ModelUtils.getAdditionalProperties(p);
if (inner == null) { if (inner == null) {
LOGGER.warn(p.getName() + "(map property) does not have a proper inner type defined. Default to string"); LOGGER.error("`{}` (map property) does not have a proper inner type defined. Default to type:string", p.getName());
inner = new StringSchema().description("TODO default missing array inner type to string"); inner = new StringSchema().description("TODO default missing map inner type to string");
p.setAdditionalProperties(inner);
} }
return getSchemaType(p) + "<String, " + getTypeDeclaration(inner) + ">"; return getSchemaType(p) + "<String, " + getTypeDeclaration(inner) + ">";
} }
@ -742,8 +744,11 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
} else { } else {
pattern = "new ArrayList<%s>()"; pattern = "new ArrayList<%s>()";
} }
if (ap.getItems() == null) { if (ap.getItems() == null) {
return null; LOGGER.error("`{}` (array property) does not have a proper inner type defined. Default to type:string", ap.getName());
Schema inner = new StringSchema().description("TODO default missing array inner type to string");
ap.setItems(inner);
} }
String typeDeclaration = getTypeDeclaration(ap.getItems()); String typeDeclaration = getTypeDeclaration(ap.getItems());