Heuristic:APIJSON APIJSON Query Depth Limit
| Knowledge Sources | |
|---|---|
| Domains | Request_Parsing, Security, Optimization |
| Last Updated | 2026-10-11 00:00 GMT |
Overview
Maximum query depth enforcement (default 5 levels) that prevents excessively nested APIJSON requests from causing runaway recursive parsing and unbounded SQL generation.
Description
APIJSON allows clients to nest table queries inside other table queries (e.g., Moment: { Comment: { ... } }), enabling rich relational fetches in a single request. Without a depth limit, a malicious or malformed request could create arbitrarily deep nesting, causing stack overflow, excessive SQL generation, or denial-of-service.
The AbstractParser tracks the current queryDepth as it recursively descends into onObjectParse() and onArrayParse(). At each level, it compares queryDepth against MAX_QUERY_DEPTH (default 5). If the depth exceeds the limit, it throws an IllegalArgumentException with a message indicating the current depth and the maximum allowed.
Additional count limits work alongside depth:
- MAX_OBJECT_COUNT = 5 (max concurrent objects per level)
- MAX_ARRAY_COUNT = 5 (max array sub-queries per level)
- MAX_SQL_COUNT = 200 (max total SQL statements per request)
- MAX_QUERY_COUNT = 100 (max rows per query)
- MAX_QUERY_PAGE = 100 (max page number)
- MAX_UPDATE_COUNT = 10 (max rows per update)
Usage
Use this heuristic when:
- Tuning APIJSON for production deployment with untrusted clients
- Experiencing performance issues from deeply nested requests
- Setting resource limits to prevent denial-of-service via complex queries
The Insight (Rule of Thumb)
- Action: Set AbstractParser.MAX_QUERY_DEPTH to control the maximum nesting depth of APIJSON requests.
- Value: Default is 5. Increase for complex analytical queries; decrease (to 2-3) for public-facing APIs with untrusted clients.
- Trade-off: Lower values restrict legitimate complex queries; higher values increase resource consumption risk.
- Related limits: Also tune MAX_SQL_COUNT (default 200) and MAX_OBJECT_COUNT (default 5) together — depth alone is not sufficient.
Reasoning
Each nesting level in an APIJSON request can generate one or more SQL queries (plus JOINs). A request with depth 5 could generate up to 5 × MAX_OBJECT_COUNT × MAX_ARRAY_COUNT = 125 sub-queries, each potentially returning up to MAX_QUERY_COUNT = 100 rows. The depth limit is the primary gate; the count limits are secondary gates.
The queryDepth is tracked incrementally: it is set to the maximum depth reached during parsing, not the current depth. This means a request that explores one branch deeply and then a shallow branch retains the maximum depth value.
Code Evidence
Depth limit constant in `AbstractParser.java:83`:
public static int MAX_QUERY_DEPTH = 5;
Depth tracking and enforcement in `AbstractParser.java:1165-1169`:
if (queryDepth < d) {
queryDepth = d;
int maxQueryDepth = getMaxQueryDepth();
if (queryDepth > maxQueryDepth) {
throw new IllegalArgumentException(parentPath + "/" + name + ":{} 的深度(或者说层级) 为 " + queryDepth + " 已超限,必须在 1-" + maxQueryDepth + " 内 !");
}
}
All count limits in `AbstractParser.java:76-83`:
public static int MAX_QUERY_PAGE = 100;
public static int DEFAULT_QUERY_COUNT = 10;
public static int MAX_QUERY_COUNT = 100;
public static int MAX_UPDATE_COUNT = 10;
public static int MAX_SQL_COUNT = 200;
public static int MAX_OBJECT_COUNT = 5;
public static int MAX_ARRAY_COUNT = 5;
public static int MAX_QUERY_DEPTH = 5;
Diagnostic output in `AbstractParser.java:619`:
res.put("depth:count|max", queryDepth + "|" + getMaxQueryDepth());