Tencent/APIJSON · critical · UnsupportedOperationException

写操作请求必须带条件!!!

Error message

写操作请求必须带条件!!!

What it means

getWhereString() builds the WHERE clause from the where map + @combine; if the result is empty AND the request method is not a query method (isQueryMethod covers GET/HEAD and similar), it throws UnsupportedOperationException. APIJSON deliberately forbids unconditional writes/updates/deletes — an UPDATE or DELETE with no WHERE would affect every row in the table.

Source

Thrown at APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java:3396

			return getWhereString(hasPrefix, getMethod(), getWhere(), getCombineMap(), getJoinList(), ! isTest());
		}
		return getWhereString(hasPrefix, getMethod(), getWhere(), combineExpr, getJoinList(), ! isTest());
	}
	/**获取WHERE
	 * @param method
	 * @param where
	 * @return
	 * @throws Exception
	 */
	public String getWhereString(boolean hasPrefix, RequestMethod method, Map<String, Object> where
			, String combine, List<Join<T, M, L>> joinList, boolean verifyName) throws Exception {
		String whereString = parseCombineExpression(method, getQuote(), getTable(), getAlias()
				, where, combine, verifyName, false, false);
		whereString = concatJoinWhereString(whereString);
		String result = StringUtil.isEmpty(whereString, true) ? "" : (hasPrefix ? " WHERE " : "") + whereString;

		if (result.isEmpty() && RequestMethod.isQueryMethod(method) == false) {
			throw new UnsupportedOperationException("写操作请求必须带条件!!!");
		}

		return result;
	}

	/**解析 @combine 条件 key 组合的与或非+括号的逻辑运算表达式为具体的完整条件组合
	 * @param method
	 * @param quote
	 * @param table
	 * @param alias
	 * @param conditionMap  where 或 having 对应条件的 Map
	 * @param combine
	 * @param verifyName
	 * @param containRaw
	 * @param isHaving
	 * @return
	 * @throws Exception
	 */

View on GitHub (pinned to 5284052872)

Solutions

  1. Add an explicit condition key to the write request, almost always the primary key: {"User":{"id":1,"name":"x"}} for PUT or {"User":{"id":1}} for DELETE.
  2. If a genuinely global operation is required, do it with a raw/ADMIN-authorized endpoint or a stored procedure — do not try to bypass this guard in ORM code.
  3. When building requests programmatically, assert the condition map is non-empty before sending (see validation code).
  4. Check that your conditions are not silently dropped (e.g. misspelled keys filtered by request structure) leaving an empty WHERE.

Example fix

// before (DELETE everything)
{"User":{}}
// after (DELETE one row)
{"User":{"id":1}}
Defensive patterns

Strategy: try-catch

Validate before calling

RequestMethod m = parser.getMethod();
if (!RequestMethod.isQueryMethod(m)) {
    JSONObject table = request.getJSONObject("User");
    boolean hasCondition = table.keySet().stream().anyMatch(k -> !k.startsWith("@"));
    if (!hasCondition) throw new IllegalStateException("write request needs a where key (e.g. id)");
}

Type guard

function hasWriteCondition(req: Record<string, any>): boolean {
  return Object.keys(req).some(k => !k.startsWith('@')); // at least one condition key present
}

Try / catch

try { parser.execute(...); } catch (UnsupportedOperationException e) { if (e.getMessage().contains("写操作请求必须带条件")) { /* surface 'missing WHERE' to user, never retry blindly */ } throw e; }

Prevention

When it happens

Trigger: A PUT/DELETE request (per APIJSON method mapping) whose table object contains no condition keys at all, e.g. {"User":{"name":"a"}} on DELETE where the keys are all consumed as SET columns, or conditions that all resolve to empty (e.g. @combine referencing no effective keys). RequestMethod.isQueryMethod(method) == false is what activates the check at line 3395.

Common situations: Forgetting the id in an update/delete request; building the request dynamically so the condition object ends up empty; migrating a GET-style request to PUT without adding a where key; test scripts that mass-update by intent and hit the safety guard.

Related errors


AI-assisted analysis of Tencent/APIJSON@5284052872 (2026-08-14). Data as JSON: /api/errors/4e78e016bb3530d4. Report an issue: GitHub.