Skip to content

Commit 2f9c6bb

Browse files
committed
Server:新增支持重写方法 onMissingKey4Combine 来自定义 @combine:value 中 value 的 key 在 Request 中的 value 不存在或为 null
1 parent a86832a commit 2f9c6bb

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/boot/DemoSQLConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static apijson.framework.APIJSONConstant.USER_;
2020
import static apijson.framework.APIJSONConstant.USER_ID;
2121

22+
import com.alibaba.fastjson.JSONObject;
23+
2224
import apijson.RequestMethod;
2325
import apijson.framework.APIJSONSQLConfig;
2426
import apijson.orm.AbstractSQLConfig;
@@ -72,6 +74,11 @@ public String getUserIdKey(String database, String schema, String table) {
7274
// public Object newId(RequestMethod method, String database, String schema, String table) {
7375
// return null; // return null 则不生成 id,一般用于数据库自增 id
7476
// }
77+
78+
@Override
79+
public void onMissingKey4Combine(String name, JSONObject request, String combine, String item, String key) throws Exception {
80+
// super.onMissingKey4Combine(name, request, combine, item, key);
81+
}
7582
};
7683

7784
// 自定义where条件拼接

APIJSON-Java-Server/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2589,7 +2589,7 @@ else if (id instanceof Subquery) {}
25892589
String[] ws = StringUtil.split(combine);
25902590
if (ws != null) {
25912591
if (method == DELETE || method == GETS || method == HEADS) {
2592-
throw new IllegalArgumentException("DELETE,GETS,HEADS 请求不允许传 @combine:\"conditons\" !");
2592+
throw new IllegalArgumentException("DELETE,GETS,HEADS 请求不允许传 @combine:value !");
25932593
}
25942594
whereList = new ArrayList<>();
25952595

@@ -2633,8 +2633,11 @@ else if (w.startsWith("!")) {
26332633

26342634
whereList.add(w);
26352635
}
2636-
if (request.containsKey(w) == false) {
2637-
throw new IllegalArgumentException(table + ":{} 里的 @combine:value 中的value里 " + ws[i] + " 对应的 " + w + " 不在它里面!");
2636+
2637+
// 可重写回调方法自定义处理 // 动态设置的场景似乎很少,而且去掉后不方便用户排错!//去掉判断,有时候不在没关系,如果是对增删改等非开放请求强制要求传对应的条件,可以用 Operation.NECESSARY
2638+
if (request.containsKey(w) == false) { //和 request.get(w) == null 没区别,前面 Parser 已经过滤了 null
2639+
// throw new IllegalArgumentException(table + ":{} 里的 @combine:value 中的value里 " + ws[i] + " 对应的 " + w + " 不在它里面!");
2640+
callback.onMissingKey4Combine(table, request, combine, ws[i], w);
26382641
}
26392642
}
26402643

@@ -2931,7 +2934,6 @@ public static interface Callback {
29312934
*/
29322935
SQLConfig getSQLConfig(RequestMethod method, String database, String schema, String table);
29332936

2934-
29352937
/**为 post 请求新建 id, 只能是 Long 或 String
29362938
* @param method
29372939
* @param database
@@ -2956,6 +2958,13 @@ public static interface Callback {
29562958
* @return
29572959
*/
29582960
String getUserIdKey(String database, String schema, String table);
2961+
2962+
/**combine 里的 key 在 request 中 value 为 null 或不存在,即 request 中缺少用来作为 combine 条件的 key: value
2963+
* @param combine
2964+
* @param key
2965+
* @param request
2966+
*/
2967+
public void onMissingKey4Combine(String name, JSONObject request, String combine, String item, String key) throws Exception;
29592968
}
29602969

29612970
public static abstract class SimpleCallback implements Callback {
@@ -2975,6 +2984,11 @@ public String getIdKey(String database, String schema, String table) {
29752984
public String getUserIdKey(String database, String schema, String table) {
29762985
return KEY_USER_ID;
29772986
}
2987+
2988+
@Override
2989+
public void onMissingKey4Combine(String name, JSONObject request, String combine, String item, String key) throws Exception {
2990+
throw new IllegalArgumentException(name + ":{} 里的 @combine:value 中的value里 " + item + " 对应的条件 " + key + ":value 中 value 不能为 null!");
2991+
}
29782992

29792993
}
29802994

APIJSON-Java-Server/APIJSONORM/src/main/java/apijson/orm/Operation.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ public enum Operation {
2222
/**
2323
* 不允许传的字段,结构是
2424
* "key0,key1,key2..."
25+
* TODO 改成 MUST 减少长度 ?
2526
*/
2627
DISALLOW,
2728

2829
/**
2930
* 必须传的字段,结构是
3031
* "key0,key1,key2..."
32+
* TODO 改成 REFUSE 减少长度 ?
3133
*/
3234
NECESSARY,
3335

0 commit comments

Comments
 (0)