Description
PR #5004 added a cycle guard (typesBeingResolved, resolveArraySchemaWithCycleGuard) to prevent StackOverflowError on recursive models. When the guard triggers, AnnotationsUtils.getArraySchema(...) is invoked with processSchemaImplementation = false and the setItems(...)
branch is skipped:
if (arraySchema.schema() != null) {
if (arraySchema.schema().implementation().equals(Void.class)) {
// setItems called
} else if (processSchemaImplementation) {
// setItems called
}
// else: items is never set
}
The resulting schema is missing its items (and degrades to type: object); in deeper cycles the property is dropped from the schema entirely. The referenced type is already registered in the ModelConverterContext at that point, so a $ref could safely be emitted.
Swagger-core version
- Broken:
2.2.41 – 2.2.49 (verified against master)
- Last working:
2.2.40
How to reproduce
class A { B b; }
class B { List<A> children; }
var schemas = ModelConverters.getInstance().readAll(B.class);
System.out.println(Json.pretty(schemas.get("B")));
Expected (≤ 2.2.40):
"children": {
"type": "array",
"items": { "$ref": "#/components/schemas/A" }
}
Actual (≥ 2.2.41):
"children": { "type": "object" }
Suggested fix
When the guard prevents setItems, fall back to a $ref to the item type already registered in the ModelConverterContext, instead of leaving items unset. This preserves the StackOverflow fix from #5004 while emitting a valid schema.
Related
Description
PR #5004 added a cycle guard (
typesBeingResolved,resolveArraySchemaWithCycleGuard) to preventStackOverflowErroron recursive models. When the guard triggers,AnnotationsUtils.getArraySchema(...)is invoked withprocessSchemaImplementation = falseand thesetItems(...)branch is skipped:
The resulting schema is missing its
items(and degrades totype: object); in deeper cycles the property is dropped from the schema entirely. The referenced type is already registered in theModelConverterContextat that point, so a$refcould safely be emitted.Swagger-core version
2.2.41–2.2.49(verified againstmaster)2.2.40How to reproduce
Expected (≤ 2.2.40):
Actual (≥ 2.2.41):
Suggested fix
When the guard prevents
setItems, fall back to a$refto the item type already registered in theModelConverterContext, instead of leavingitemsunset. This preserves the StackOverflow fix from #5004 while emitting a valid schema.Related