Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better array handling in 3.1 spec #18313

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
addOneOfNameExtension(s, n);
}
} else if (ModelUtils.isArraySchema(s)) {
Schema items = ((ArraySchema) s).getItems();
Schema items = ModelUtils.getSchemaItems(s);
if (ModelUtils.isComposedSchema(items)) {
addOneOfNameExtension(items, nOneOf);
addOneOfInterfaceModel(items, nOneOf);
Expand Down Expand Up @@ -2065,8 +2065,7 @@ public String toInstantiationType(Schema schema) {
}
return inner;
} else if (ModelUtils.isArraySchema(schema)) {
ArraySchema arraySchema = (ArraySchema) schema;
String inner = getSchemaType(getSchemaItems(arraySchema));
String inner = getSchemaType(ModelUtils.getSchemaItems(schema));
String parentType;
if (ModelUtils.isSet(schema)) {
parentType = "set";
Expand Down Expand Up @@ -2372,15 +2371,7 @@ public String getSchemaType(Schema schema) {

}

protected Schema<?> getSchemaItems(ArraySchema schema) {
Schema<?> items = schema.getItems();
if (items == null) {
LOGGER.error("Undefined array inner type for `{}`. Default to String.", schema.getName());
items = new StringSchema().description("TODO default missing array inner type to string");
schema.setItems(items);
}
return items;
}


protected Schema<?> getSchemaAdditionalProperties(Schema schema) {
Schema<?> inner = ModelUtils.getAdditionalProperties(schema);
Expand Down Expand Up @@ -3983,7 +3974,7 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
LOGGER.error("Undefined property/schema for `{}`. Default to type:string.", name);
return null;
}
LOGGER.debug("debugging fromProperty for {} : {}", name, p);
LOGGER.debug("debugging fromProperty for {}: {}", name, p);
NamedSchema ns = new NamedSchema(name, p, required, schemaIsFromAdditionalProperties);
CodegenProperty cpc = schemaCodegenPropertyCache.get(ns);
if (cpc != null) {
Expand Down Expand Up @@ -4190,8 +4181,7 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo

// handle inner property
String itemName = getItemsName(p, name);
ArraySchema arraySchema = (ArraySchema) p;
Schema innerSchema = unaliasSchema(getSchemaItems(arraySchema));
Schema innerSchema = unaliasSchema(ModelUtils.getSchemaItems(p));
CodegenProperty cp = fromProperty(itemName, innerSchema, false);
updatePropertyForArray(property, cp);
} else if (ModelUtils.isTypeObjectSchema(p)) {
Expand Down Expand Up @@ -4501,8 +4491,7 @@ protected void handleMethodResponse(Operation operation,
CodegenProperty cm = fromProperty("response", responseSchema, false);

if (ModelUtils.isArraySchema(responseSchema)) {
ArraySchema as = (ArraySchema) responseSchema;
CodegenProperty innerProperty = fromProperty("response", getSchemaItems(as), false);
CodegenProperty innerProperty = fromProperty("response", ModelUtils.getSchemaItems(responseSchema), false);
op.returnBaseType = innerProperty.baseType;
} else if (ModelUtils.isMapSchema(responseSchema)) {
CodegenProperty innerProperty = fromProperty("response", ModelUtils.getAdditionalProperties(responseSchema), false);
Expand Down Expand Up @@ -5027,8 +5016,7 @@ public CodegenResponse fromResponse(String responseCode, ApiResponse response) {
r.isArray = true;
r.containerType = cp.containerType;
r.containerTypeMapped = typeMapping.get(cp.containerType);
ArraySchema as = (ArraySchema) responseSchema;
CodegenProperty items = fromProperty("response", getSchemaItems(as), false);
CodegenProperty items = fromProperty("response", ModelUtils.getSchemaItems(responseSchema), false);
r.setItems(items);
CodegenProperty innerCp = items;

Expand Down Expand Up @@ -5404,8 +5392,7 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
}
addVarsRequiredVarsAdditionalProps(parameterSchema, codegenParameter);
} else if (ModelUtils.isArraySchema(parameterSchema)) {
final ArraySchema arraySchema = (ArraySchema) parameterSchema;
Schema inner = getSchemaItems(arraySchema);
Schema inner = ModelUtils.getSchemaItems(parameterSchema);

collectionFormat = getCollectionFormat(parameter);
// default to csv:
Expand Down Expand Up @@ -7291,7 +7278,7 @@ public CodegenParameter fromFormProperty(String name, Schema propertySchema, Set
} else if (ModelUtils.isAnyType(ps)) {
// any schema with no type set, composed schemas often do this
} else if (ModelUtils.isArraySchema(ps)) {
Schema inner = getSchemaItems((ArraySchema) ps);
Schema inner = ModelUtils.getSchemaItems(ps);
CodegenProperty arrayInnerProperty = fromProperty("inner", inner, false);
codegenParameter.items = arrayInnerProperty;
codegenParameter.mostInnerItems = arrayInnerProperty.mostInnerItems;
Expand Down Expand Up @@ -7572,11 +7559,10 @@ protected void updateRequestBodyForArray(CodegenParameter codegenParameter, Sche
if (ModelUtils.isGenerateAliasAsModel(schema) && StringUtils.isNotBlank(name)) {
this.addBodyModelSchema(codegenParameter, name, schema, imports, bodyParameterName, true);
} else {
final ArraySchema arraySchema = (ArraySchema) schema;
Schema inner = getSchemaItems(arraySchema);
CodegenProperty codegenProperty = fromProperty("property", arraySchema, false);
Schema inner = ModelUtils.getSchemaItems(schema);
CodegenProperty codegenProperty = fromProperty("property", schema, false);
if (codegenProperty == null) {
throw new RuntimeException("CodegenProperty cannot be null. arraySchema for debugging: " + arraySchema);
throw new RuntimeException("CodegenProperty cannot be null. arraySchema for debugging: " + schema);
}

imports.add(codegenProperty.baseType);
Expand Down Expand Up @@ -7609,7 +7595,7 @@ protected void updateRequestBodyForArray(CodegenParameter codegenParameter, Sche

codegenParameter.items = codegenProperty.items;
codegenParameter.mostInnerItems = codegenProperty.mostInnerItems;
codegenParameter.dataType = getTypeDeclaration(arraySchema);
codegenParameter.dataType = getTypeDeclaration(schema);
codegenParameter.baseType = getSchemaType(inner);
codegenParameter.isContainer = Boolean.TRUE;
codegenParameter.isNullable = codegenProperty.isNullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,9 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
return;
}
// Check array items
if (schema instanceof ArraySchema) {
ArraySchema array = (ArraySchema) schema;
Schema items = array.getItems();
if (items == null && array.getPrefixItems() == null) {
if (ModelUtils.isArraySchema(schema)) {
Schema items = ModelUtils.getSchemaItems(schema);
if (items == null && schema.getPrefixItems() == null) {
LOGGER.debug("Incorrect array schema with no items, prefixItems: {}", schema.toString());
return;
}
Expand All @@ -357,7 +356,7 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {

if (isModelNeeded(items)) {
// If this schema should be split into its own model, do so
array.setItems(this.makeSchemaInComponents(schemaName, items));
schema.setItems(this.makeSchemaInComponents(schemaName, items));
}
}
// Check allOf, anyOf, oneOf for inline models
Expand Down Expand Up @@ -788,9 +787,8 @@ private void flattenProperties(OpenAPI openAPI, Map<String, Schema> properties,
propsToUpdate.put(key, schema);
modelsToAdd.put(modelName, model);
}
} else if (property instanceof ArraySchema) {
ArraySchema ap = (ArraySchema) property;
Schema inner = ap.getItems();
} else if (ModelUtils.isArraySchema(property)) {
Schema inner = ModelUtils.getSchemaItems(property);
if (inner instanceof ObjectSchema) {
ObjectSchema op = (ObjectSchema) inner;
if (op.getProperties() != null && op.getProperties().size() > 0) {
Expand All @@ -801,12 +799,12 @@ private void flattenProperties(OpenAPI openAPI, Map<String, Schema> properties,
if (existing != null) {
Schema schema = new Schema().$ref(existing);
schema.setRequired(op.getRequired());
ap.setItems(schema);
property.setItems(schema);
} else {
modelName = addSchemas(modelName, innerModel);
Schema schema = new Schema().$ref(modelName);
schema.setRequired(op.getRequired());
ap.setItems(schema);
property.setItems(schema);
}
}
} else if (ModelUtils.isComposedSchema(inner)) {
Expand All @@ -815,7 +813,7 @@ private void flattenProperties(OpenAPI openAPI, Map<String, Schema> properties,
innerModelName = addSchemas(innerModelName, inner);
Schema schema = new Schema().$ref(innerModelName);
schema.setRequired(inner.getRequired());
ap.setItems(schema);
property.setItems(schema);
} else {
LOGGER.debug("Schema not yet handled in model resolver: {}", inner);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,10 @@ public Schema normalizeSchema(Schema schema, Set<Schema> visitedSchemas) {
visitedSchemas.add(schema);
}

if (schema instanceof ArraySchema) { // array
normalizeArraySchema(schema);
normalizeSchema(schema.getItems(), visitedSchemas);
if (ModelUtils.isArraySchema(schema)) { // array
Schema result = normalizeArraySchema(schema);
normalizeSchema(result.getItems(), visitedSchemas);
return result;
} else if (schema.getAdditionalProperties() instanceof Schema) { // map
normalizeMapSchema(schema);
normalizeSchema((Schema) schema.getAdditionalProperties(), visitedSchemas);
Expand Down Expand Up @@ -566,7 +567,8 @@ public Schema normalizeSchema(Schema schema, Set<Schema> visitedSchemas) {
}

private Schema normalizeArraySchema(Schema schema) {
return processSetArraytoNullable(schema);
Schema result = processNormalize31Spec(schema, new HashSet<>());
return processSetArraytoNullable(result);
}

private Schema normalizeMapSchema(Schema schema) {
Expand Down Expand Up @@ -1222,7 +1224,7 @@ private Schema processNormalize31Spec(Schema schema, Set<Schema> visitedSchemas)
// only one item (type) left
if (schema.getTypes().size() == 1) {
String type = String.valueOf(schema.getTypes().iterator().next());
if ("array".equals(type)) {
if (ModelUtils.isArraySchema(schema)) {
ArraySchema as = new ArraySchema();
as.setDescription(schema.getDescription());
as.setDefault(schema.getDefault());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@ private List<Map<String, String>> generateFromResponseSchema(Schema responseSche
}

if (ModelUtils.isArraySchema(responseSchema)) { // array of schema
ArraySchema as = (ArraySchema) responseSchema;
if (as.getItems() != null) { // array of primitive types
if (ModelUtils.getSchemaItems(responseSchema) != null) { // array of primitive types
return generate((Map<String, Object>) responseSchema.getExample(),
new ArrayList<>(producesInfo), as);
new ArrayList<>(producesInfo), responseSchema);
} else {
// TODO log warning message as such case is not handled at the moment
return null;
Expand Down Expand Up @@ -248,9 +247,9 @@ private Object resolvePropertyToExample(String propertyName, String mediaType, S
}
return Boolean.TRUE;
} else if (ModelUtils.isArraySchema(property)) {
Schema innerType = ((ArraySchema) property).getItems();
Schema innerType = ModelUtils.getSchemaItems(property);
if (innerType != null) {
int arrayLength = null == ((ArraySchema) property).getMaxItems() ? 2 : ((ArraySchema) property).getMaxItems();
int arrayLength = null == property.getMaxItems() ? 2 : property.getMaxItems();
// avoid memory issues by limiting to max. 5 items
arrayLength = Math.min(arrayLength, 5);
Object[] objectProperties = new Object[arrayLength];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ protected String toXml(String name, Schema schema, int indent, Collection<String
StringBuilder sb = new StringBuilder();

if (ModelUtils.isArraySchema(schema)) {
ArraySchema as = (ArraySchema) schema;
Schema inner = as.getItems();
Schema inner = ModelUtils.getSchemaItems(schema);
boolean wrapped = false;
if (schema.getXml() != null && schema.getXml().getWrapped() != null && schema.getXml().getWrapped()) {
wrapped = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,7 @@ public String getTypeDeclaration(Schema p) {
}

if (ModelUtils.isArraySchema(p)) {
ArraySchema ap = (ArraySchema) p;
Schema inner = ap.getItems();
Schema inner = ModelUtils.getSchemaItems(p);
String itemType = getTypeDeclaration(inner);
if (itemType.startsWith("OpenAPI.")) {
return itemType + "_Vector";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,9 @@ public String toModelFilename(String name) {
@Override
public String getTypeDeclaration(Schema p) {
if (ModelUtils.isArraySchema(p)) {
ArraySchema ap = (ArraySchema) p;
Schema inner = ap.getItems();
Schema inner = ModelUtils.getSchemaItems(p);
if (inner == null) {
LOGGER.warn("{}(array property) does not have a proper inner type defined", ap.getName());
LOGGER.warn("{}(array property) does not have a proper inner type defined", p.getName());
// TODO maybe better defaulting to StringProperty than returning null
return null;
}
Expand Down Expand Up @@ -220,13 +219,12 @@ public String getAlias(String name) {
@Override
public String toDefaultValue(Schema p) {
if (ModelUtils.isArraySchema(p)) {
final ArraySchema ap = (ArraySchema) p;
final String pattern = "new ArrayList<%s>()";
if (ap.getItems() == null) {
if (ModelUtils.getSchemaItems(p) == null) {
return null;
}

return String.format(Locale.ROOT, pattern, getTypeDeclaration(ap.getItems()));
return String.format(Locale.ROOT, pattern, getTypeDeclaration(ModelUtils.getSchemaItems(p)));
} else if (ModelUtils.isMapSchema(p)) {
final MapSchema ap = (MapSchema) p;
final String pattern = "new HashMap<%s>()";
Expand Down Expand Up @@ -318,7 +316,7 @@ public String toExampleValue(Schema p) {

if (ModelUtils.isArraySchema(p)) {
example = "new " + getTypeDeclaration(p) + "{" + toExampleValue(
((ArraySchema) p).getItems()) + "}";
ModelUtils.getSchemaItems(p)) + "}";
} else if (ModelUtils.isBooleanSchema(p)) {
example = String.valueOf(!"false".equals(example));
} else if (ModelUtils.isByteArraySchema(p)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1433,12 +1433,12 @@ public String getSchemaType(Schema p) {
* @param arr The input array property
* @return The type declaration when the type is an array of arrays.
*/
private String getArrayTypeDeclaration(ArraySchema arr) {
private String getArrayTypeDeclaration(Schema arr) {
// TODO: collection type here should be fully qualified namespace to avoid model conflicts
// This supports arrays of arrays.
String arrayType = typeMapping.get("array");
StringBuilder instantiationType = new StringBuilder(arrayType);
Schema items = arr.getItems();
Schema items = ModelUtils.getSchemaItems(arr);
String nestedType = getTypeDeclaration(items);
// TODO: We may want to differentiate here between generics and primitive arrays.
instantiationType.append("<").append(nestedType).append(">");
Expand All @@ -1448,15 +1448,15 @@ private String getArrayTypeDeclaration(ArraySchema arr) {
@Override
public String toInstantiationType(Schema p) {
if (ModelUtils.isArraySchema(p)) {
return getArrayTypeDeclaration((ArraySchema) p);
return getArrayTypeDeclaration(p);
}
return super.toInstantiationType(p);
}

@Override
public String getTypeDeclaration(Schema p) {
if (ModelUtils.isArraySchema(p)) {
return getArrayTypeDeclaration((ArraySchema) p);
return getArrayTypeDeclaration(p);
} else if (ModelUtils.isMapSchema(p)) {
// Should we also support maps of maps?
Schema inner = ModelUtils.getAdditionalProperties(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ public String getTypeDeclaration(Schema p) {
Schema<?> schema = unaliasSchema(p);
Schema<?> target = ModelUtils.isGenerateAliasAsModel() ? p : schema;
if (ModelUtils.isArraySchema(target)) {
Schema<?> items = getSchemaItems((ArraySchema) schema);
Schema<?> items = ModelUtils.getSchemaItems(schema);
return getSchemaType(target) + "<" + getTypeDeclaration(items) + ">";
}
if (ModelUtils.isMapSchema(target)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,7 @@ public String toApiDocFilename(String name) {
@Override
public String getTypeDeclaration(Schema p) {
if (ModelUtils.isArraySchema(p)) {
ArraySchema ap = (ArraySchema) p;
Schema inner = ap.getItems();
Schema inner = ModelUtils.getSchemaItems(p);
return "LIST [" + getTypeDeclaration(inner) + "]";
} else if (ModelUtils.isMapSchema(p)) {
Schema inner = ModelUtils.getAdditionalProperties(p);
Expand Down Expand Up @@ -578,8 +577,7 @@ public String toInstantiationType(Schema p) {
// String inner = toModelName(getSchemaType(additionalProperties2));
// return instantiationTypes.get("map") + " [" + inner + "]";
// } else if (ModelUtils.isArraySchema(p)) {
// ArraySchema ap = (ArraySchema) p;
// String inner = toModelName(getSchemaType(ap.getItems()));
// String inner = toModelName(getSchemaType(ModelUtils.getSchemaItems(p)));
// return instantiationTypes.get("array") + " [" + inner + "]";
// } else {
// return null;
Expand Down
Loading
Loading