forked from loafle/openapi-generator-original
[core] do not always cast to ArraySchema (#3780)
* [core] do not always cast to ArraySchema * Change ModelUtil.isArraySchema()
This commit is contained in:
committed by
William Cheng
parent
8f7e43b500
commit
026612fed7
@@ -1247,7 +1247,7 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
return instantiationTypes.get("map") + "<String, " + inner + ">";
|
||||
} else if (ModelUtils.isArraySchema(schema)) {
|
||||
ArraySchema arraySchema = (ArraySchema) schema;
|
||||
String inner = getSchemaType(arraySchema.getItems());
|
||||
String inner = getSchemaType(getSchemaItems(arraySchema));
|
||||
return instantiationTypes.get("array") + "<" + inner + ">";
|
||||
} else {
|
||||
return null;
|
||||
@@ -1462,6 +1462,15 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
|
||||
}
|
||||
|
||||
protected Schema<?> getSchemaItems(ArraySchema schema) {
|
||||
if (schema.getItems() != null) {
|
||||
return schema.getItems();
|
||||
} else {
|
||||
LOGGER.error("Undefined array inner type for `{}`. Default to String.", schema.getName());
|
||||
return new StringSchema().description("TODO default missing array inner type to string");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name of the allOf schema
|
||||
*
|
||||
@@ -2180,11 +2189,10 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
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);
|
||||
ArraySchema arraySchema = (ArraySchema) p;
|
||||
Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, getSchemaItems(arraySchema));
|
||||
if (arraySchema.getItems() == null) {
|
||||
arraySchema.setItems(innerSchema);
|
||||
}
|
||||
} else if (ModelUtils.isMapSchema(p)) {
|
||||
Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, ModelUtils.getAdditionalProperties(p));
|
||||
@@ -2262,11 +2270,10 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
if (itemName == null) {
|
||||
itemName = property.name;
|
||||
}
|
||||
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);
|
||||
ArraySchema arraySchema = (ArraySchema) p;
|
||||
Schema innerSchema = ModelUtils.unaliasSchema(this.openAPI, getSchemaItems(arraySchema));
|
||||
if (arraySchema.getItems() == null) {
|
||||
arraySchema.setItems(innerSchema);
|
||||
}
|
||||
CodegenProperty cp = fromProperty(itemName, innerSchema);
|
||||
updatePropertyForArray(property, cp);
|
||||
@@ -2584,7 +2591,7 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
|
||||
if (ModelUtils.isArraySchema(responseSchema)) {
|
||||
ArraySchema as = (ArraySchema) responseSchema;
|
||||
CodegenProperty innerProperty = fromProperty("response", as.getItems());
|
||||
CodegenProperty innerProperty = fromProperty("response", getSchemaItems(as));
|
||||
op.returnBaseType = innerProperty.baseType;
|
||||
} else if (ModelUtils.isMapSchema(responseSchema)) {
|
||||
CodegenProperty innerProperty = fromProperty("response", ModelUtils.getAdditionalProperties(responseSchema));
|
||||
@@ -2854,7 +2861,7 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
|
||||
if (ModelUtils.isArraySchema(responseSchema)) {
|
||||
ArraySchema as = (ArraySchema) responseSchema;
|
||||
CodegenProperty innerProperty = fromProperty("response", as.getItems());
|
||||
CodegenProperty innerProperty = fromProperty("response", getSchemaItems(as));
|
||||
CodegenProperty innerCp = innerProperty;
|
||||
while (innerCp != null) {
|
||||
r.baseType = innerCp.baseType;
|
||||
@@ -3062,10 +3069,8 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
String collectionFormat = null;
|
||||
if (ModelUtils.isArraySchema(parameterSchema)) { // for array parameter
|
||||
final ArraySchema arraySchema = (ArraySchema) parameterSchema;
|
||||
Schema inner = arraySchema.getItems();
|
||||
if (inner == null) {
|
||||
LOGGER.warn("warning! No inner type supplied for array parameter \"" + parameter.getName() + "\", using String");
|
||||
inner = new StringSchema().description("//TODO automatically added by openapi-generator due to missing iner type definition in the spec");
|
||||
Schema inner = getSchemaItems(arraySchema);
|
||||
if (arraySchema.getItems() == null) {
|
||||
arraySchema.setItems(inner);
|
||||
}
|
||||
|
||||
@@ -4598,10 +4603,8 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
// array of schema
|
||||
if (ModelUtils.isArraySchema(s)) {
|
||||
final ArraySchema arraySchema = (ArraySchema) s;
|
||||
Schema inner = arraySchema.getItems();
|
||||
if (inner == null) {
|
||||
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 inner type definition in the spec");
|
||||
Schema inner = getSchemaItems(arraySchema);
|
||||
if (arraySchema.getItems() == null) {
|
||||
arraySchema.setItems(inner);
|
||||
}
|
||||
|
||||
@@ -4798,10 +4801,8 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
setParameterNullable(codegenParameter, codegenProperty);
|
||||
} else if (ModelUtils.isArraySchema(schema)) {
|
||||
final ArraySchema arraySchema = (ArraySchema) schema;
|
||||
Schema inner = arraySchema.getItems();
|
||||
if (inner == null) {
|
||||
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");
|
||||
Schema inner = getSchemaItems(arraySchema);
|
||||
if (arraySchema.getItems() == null) {
|
||||
arraySchema.setItems(inner);
|
||||
}
|
||||
CodegenProperty codegenProperty = fromProperty("property", arraySchema);
|
||||
|
||||
@@ -686,14 +686,8 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
||||
@Override
|
||||
public String getTypeDeclaration(Schema p) {
|
||||
if (ModelUtils.isArraySchema(p)) {
|
||||
ArraySchema ap = (ArraySchema) p;
|
||||
Schema inner = ap.getItems();
|
||||
if (inner == null) {
|
||||
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");
|
||||
ap.setItems(inner);
|
||||
}
|
||||
return getSchemaType(p) + "<" + getTypeDeclaration(inner) + ">";
|
||||
Schema<?> items = getSchemaItems((ArraySchema) p);
|
||||
return getSchemaType(p) + "<" + getTypeDeclaration(items) + ">";
|
||||
} else if (ModelUtils.isMapSchema(p)) {
|
||||
Schema inner = ModelUtils.getAdditionalProperties(p);
|
||||
if (inner == null) {
|
||||
@@ -725,13 +719,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
|
||||
pattern = "new ArrayList<%s>()";
|
||||
}
|
||||
|
||||
Schema<?> items;
|
||||
if (p instanceof ArraySchema && ((ArraySchema) p).getItems() != null) {
|
||||
items = ((ArraySchema) p).getItems();
|
||||
} else {
|
||||
LOGGER.error("`{}` (array property) does not have a proper inner type defined. Default to type:string", p.getName());
|
||||
items = new StringSchema().description("TODO default missing array inner type to string");
|
||||
}
|
||||
Schema<?> items = getSchemaItems((ArraySchema) p);
|
||||
|
||||
String typeDeclaration = getTypeDeclaration(items);
|
||||
Object java8obj = additionalProperties.get("java8");
|
||||
|
||||
@@ -359,14 +359,7 @@ public class ModelUtils {
|
||||
}
|
||||
|
||||
public static boolean isArraySchema(Schema schema) {
|
||||
if (schema instanceof ArraySchema) {
|
||||
return true;
|
||||
}
|
||||
// assume it's an array if maxItems, minItems is set
|
||||
if (schema != null && (schema.getMaxItems() != null || schema.getMinItems() != null)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return (schema instanceof ArraySchema);
|
||||
}
|
||||
|
||||
public static boolean isStringSchema(Schema schema) {
|
||||
|
||||
@@ -310,11 +310,24 @@ public class AbstractJavaCodegenTest {
|
||||
public void toDefaultValueTest() {
|
||||
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
|
||||
|
||||
Schema schema = new ObjectSchema()
|
||||
Schema<?> schema = createObjectSchemaWithMinItems();
|
||||
String defaultValue = codegen.toDefaultValue(schema);
|
||||
Assert.assertNull(defaultValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTypeDeclarationTest() {
|
||||
final P_AbstractJavaCodegen codegen = new P_AbstractJavaCodegen();
|
||||
|
||||
Schema<?> schema = createObjectSchemaWithMinItems();
|
||||
String defaultValue = codegen.getTypeDeclaration(schema);
|
||||
Assert.assertEquals(defaultValue, "Object");
|
||||
}
|
||||
|
||||
private static Schema<?> createObjectSchemaWithMinItems() {
|
||||
return new ObjectSchema()
|
||||
.addProperties("id", new IntegerSchema().format("int32"))
|
||||
.minItems(1);
|
||||
String defaultValue = codegen.toDefaultValue(schema);
|
||||
Assert.assertEquals(defaultValue, "new ArrayList<String>()");
|
||||
}
|
||||
|
||||
private static class P_AbstractJavaCodegen extends AbstractJavaCodegen {
|
||||
|
||||
Reference in New Issue
Block a user