Skip to content

Commit e03a028

Browse files
committed
解决其它 key 被误当成 @post 等关键词导致 RequestMethod.valueOf 异常
1 parent bd5161f commit e03a028

File tree

1 file changed

+89
-78
lines changed

1 file changed

+89
-78
lines changed

APIJSONORM/src/main/java/apijson/orm/AbstractParser.java

Lines changed: 89 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
import apijson.orm.exception.UnsupportedDataTypeException;
2929

3030
import static apijson.JSON.*;
31-
import static apijson.JSONMap.KEY_COMBINE;
32-
import static apijson.JSONMap.KEY_EXPLAIN;
31+
import static apijson.JSONMap.*;
3332
import static apijson.JSONRequest.KEY_TAG;
3433
import static apijson.RequestMethod.CRUD;
3534
import static apijson.RequestMethod.GET;
@@ -532,32 +531,32 @@ public M parseResponse(M request) {
532531
//必须在parseCorrectRequest后面,因为parseCorrectRequest可能会添加 @role
533532
if (isNeedVerifyRole() && globalRole == null) {
534533
try {
535-
setGlobalRole(getString(requestObject, JSONMap.KEY_ROLE));
536-
requestObject.remove(JSONMap.KEY_ROLE);
534+
setGlobalRole(getString(requestObject, KEY_ROLE));
535+
requestObject.remove(KEY_ROLE);
537536
} catch (Exception e) {
538537
return extendErrorResult(requestObject, e, requestMethod, getRequestURL(), isRoot);
539538
}
540539
}
541540

542541
try {
543-
setGlobalDatabase(getString(requestObject, JSONMap.KEY_DATABASE));
544-
setGlobalDatasource(getString(requestObject, JSONMap.KEY_DATASOURCE));
545-
setGlobalNamespace(getString(requestObject, JSONMap.KEY_NAMESPACE));
546-
setGlobalCatalog(getString(requestObject, JSONMap.KEY_CATALOG));
547-
setGlobalSchema(getString(requestObject, JSONMap.KEY_SCHEMA));
548-
549-
setGlobalExplain(getBoolean(requestObject, JSONMap.KEY_EXPLAIN));
550-
setGlobalCache(getString(requestObject, JSONMap.KEY_CACHE));
542+
setGlobalDatabase(getString(requestObject, KEY_DATABASE));
543+
setGlobalDatasource(getString(requestObject, KEY_DATASOURCE));
544+
setGlobalNamespace(getString(requestObject, KEY_NAMESPACE));
545+
setGlobalCatalog(getString(requestObject, KEY_CATALOG));
546+
setGlobalSchema(getString(requestObject, KEY_SCHEMA));
547+
548+
setGlobalExplain(getBoolean(requestObject, KEY_EXPLAIN));
549+
setGlobalCache(getString(requestObject, KEY_CACHE));
551550
setGlobalFormat(getBoolean(requestObject, apijson.JSONRequest.KEY_FORMAT));
552551

553-
requestObject.remove(JSONMap.KEY_DATABASE);
554-
requestObject.remove(JSONMap.KEY_DATASOURCE);
555-
requestObject.remove(JSONMap.KEY_NAMESPACE);
556-
requestObject.remove(JSONMap.KEY_CATALOG);
557-
requestObject.remove(JSONMap.KEY_SCHEMA);
552+
requestObject.remove(KEY_DATABASE);
553+
requestObject.remove(KEY_DATASOURCE);
554+
requestObject.remove(KEY_NAMESPACE);
555+
requestObject.remove(KEY_CATALOG);
556+
requestObject.remove(KEY_SCHEMA);
558557

559-
requestObject.remove(JSONMap.KEY_EXPLAIN);
560-
requestObject.remove(JSONMap.KEY_CACHE);
558+
requestObject.remove(KEY_EXPLAIN);
559+
requestObject.remove(KEY_CACHE);
561560
requestObject.remove(apijson.JSONRequest.KEY_FORMAT);
562561
} catch (Exception e) {
563562
return extendErrorResult(requestObject, e, requestMethod, getRequestURL(), isRoot);
@@ -695,11 +694,11 @@ public M wrapRequest(RequestMethod method, String tag, M object, boolean isStruc
695694
}
696695

697696
boolean isDiffArrayKey = tag.endsWith(":[]");
698-
boolean isArrayKey = isDiffArrayKey || JSONMap.isArrayKey(tag);
697+
boolean isArrayKey = isDiffArrayKey || isArrayKey(tag);
699698
String key = isArrayKey ? tag.substring(0, tag.length() - (isDiffArrayKey ? 3 : 2)) : tag;
700699

701700
M target = object;
702-
if (JSONMap.isTableKey(key)) {
701+
if (isTableKey(key)) {
703702
if (isDiffArrayKey) { //自动为 tag = Comment:[] 的 { ... } 新增键值对为 { "Comment[]":[], "TYPE": { "Comment[]": "OBJECT[]" } ... }
704703
if (isStructure && (method == RequestMethod.POST || method == RequestMethod.PUT)) {
705704
String arrKey = key + "[]";
@@ -1145,7 +1144,7 @@ public M onObjectParse(final M request, String parentPath, String name
11451144
String table = entry.getKey(); //Comment
11461145
// String alias = entry.getValue(); //to
11471146

1148-
boolean isTable = JSONMap.isTableKey(table);
1147+
boolean isTable = isTableKey(table);
11491148
boolean isArrayMainTable = isSubquery == false && isTable && type == SQLConfig.TYPE_ITEM_CHILD_0 && arrayConfig != null && RequestMethod.isGetMethod(arrayConfig.getMethod(), true);
11501149
boolean isReuse = isArrayMainTable && position > 0;
11511150

@@ -1294,7 +1293,7 @@ public L onArrayParse(M request, String parentPath, String name, boolean isSubqu
12941293
}
12951294

12961295
//不能允许GETS,否则会被通过"[]":{"@role":"ADMIN"},"Table":{},"tag":"Table"绕过权限并能批量查询
1297-
RequestMethod _method = request.get(JSONMap.KEY_METHOD) == null ? requestMethod : RequestMethod.valueOf(getString(request, JSONMap.KEY_METHOD));
1296+
RequestMethod _method = request.get(KEY_METHOD) == null ? requestMethod : RequestMethod.valueOf(getString(request, KEY_METHOD));
12981297
if (isSubquery == false && RequestMethod.isGetMethod(_method, true) == false) {
12991298
throw new UnsupportedOperationException("key[]:{} 只支持 GET, GETS 方法!其它方法不允许传 " + name + ":{} 等这种 key[]:{} 格式!");
13001299
}
@@ -1378,7 +1377,7 @@ public L onArrayParse(M request, String parentPath, String name, boolean isSubqu
13781377
if (childKeys == null || childKeys.length <= 0 || request.containsKey(childKeys[0]) == false) {
13791378
childKeys = null;
13801379
}
1381-
else if (childKeys.length == 1 && JSONMap.isTableKey(childKeys[0])) { // 可能无需提取,直接返回 rawList 即可
1380+
else if (childKeys.length == 1 && isTableKey(childKeys[0])) { // 可能无需提取,直接返回 rawList 即可
13821381
arrTableKey = childKeys[0];
13831382
}
13841383

@@ -1484,26 +1483,26 @@ else if (childKeys.length == 1 && JSONMap.isTableKey(childKeys[0])) { // 可能
14841483
private static final List<String> JOIN_COPY_KEY_LIST;
14851484
static { // TODO 不全
14861485
JOIN_COPY_KEY_LIST = new ArrayList<String>();
1487-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_ROLE);
1488-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_DATABASE);
1489-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_NAMESPACE);
1490-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_CATALOG);
1491-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_SCHEMA);
1492-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_DATASOURCE);
1493-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_COLUMN);
1494-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_NULL);
1495-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_CAST);
1496-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_COMBINE);
1497-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_GROUP);
1498-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_HAVING);
1499-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_HAVING_AND);
1500-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_SAMPLE);
1501-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_LATEST);
1502-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_PARTITION);
1503-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_FILL);
1504-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_ORDER);
1505-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_KEY);
1506-
JOIN_COPY_KEY_LIST.add(JSONMap.KEY_RAW);
1486+
JOIN_COPY_KEY_LIST.add(KEY_ROLE);
1487+
JOIN_COPY_KEY_LIST.add(KEY_DATABASE);
1488+
JOIN_COPY_KEY_LIST.add(KEY_NAMESPACE);
1489+
JOIN_COPY_KEY_LIST.add(KEY_CATALOG);
1490+
JOIN_COPY_KEY_LIST.add(KEY_SCHEMA);
1491+
JOIN_COPY_KEY_LIST.add(KEY_DATASOURCE);
1492+
JOIN_COPY_KEY_LIST.add(KEY_COLUMN);
1493+
JOIN_COPY_KEY_LIST.add(KEY_NULL);
1494+
JOIN_COPY_KEY_LIST.add(KEY_CAST);
1495+
JOIN_COPY_KEY_LIST.add(KEY_COMBINE);
1496+
JOIN_COPY_KEY_LIST.add(KEY_GROUP);
1497+
JOIN_COPY_KEY_LIST.add(KEY_HAVING);
1498+
JOIN_COPY_KEY_LIST.add(KEY_HAVING_AND);
1499+
JOIN_COPY_KEY_LIST.add(KEY_SAMPLE);
1500+
JOIN_COPY_KEY_LIST.add(KEY_LATEST);
1501+
JOIN_COPY_KEY_LIST.add(KEY_PARTITION);
1502+
JOIN_COPY_KEY_LIST.add(KEY_FILL);
1503+
JOIN_COPY_KEY_LIST.add(KEY_ORDER);
1504+
JOIN_COPY_KEY_LIST.add(KEY_KEY);
1505+
JOIN_COPY_KEY_LIST.add(KEY_RAW);
15071506
}
15081507

15091508
/**JOIN 多表同时筛选
@@ -1564,7 +1563,7 @@ else if (join != null){
15641563
String tableKey = index < 0 ? path : path.substring(0, index); // User:owner
15651564
int index2 = tableKey.lastIndexOf("/");
15661565
String arrKey = index2 < 0 ? null : tableKey.substring(0, index2);
1567-
if (arrKey != null && JSONMap.isArrayKey(arrKey) == false) {
1566+
if (arrKey != null && isArrayKey(arrKey) == false) {
15681567
throw new IllegalArgumentException(apijson.JSONRequest.KEY_JOIN + ":'" + e.getKey() + "' 对应的 " + arrKey + " 不是合法的数组 key[] !" +
15691568
"@ APP JOIN 最多允许跨 1 层,只能是子数组,且数组对象中不能有 join: value 键值对!");
15701569
}
@@ -1667,7 +1666,7 @@ else if (join != null){
16671666

16681667
apijson.orm.Entry<String, String> te = tk == null || p.substring(ind2 + 1).indexOf("/") >= 0 ? null : Pair.parseEntry(tk, true);
16691668

1670-
if (te != null && JSONMap.isTableKey(te.getKey()) && request.get(tk) instanceof Map<?, ?>) {
1669+
if (te != null && isTableKey(te.getKey()) && request.get(tk) instanceof Map<?, ?>) {
16711670
if (isAppJoin) {
16721671
if (refObj.size() >= 1) {
16731672
throw new IllegalArgumentException(apijson.JSONRequest.KEY_JOIN + ":" + e.getKey() + " 中 " + k + " 不合法!"
@@ -2218,7 +2217,7 @@ protected void onClose() {
22182217
}
22192218

22202219
private void setOpMethod(Map<String, Object> request, ObjectParser<T, M, L> op, String key) {
2221-
String _method = key == null ? null : getString(request, JSONMap.KEY_METHOD);
2220+
String _method = key == null ? null : getString(request, KEY_METHOD);
22222221
if (_method != null) {
22232222
RequestMethod method = RequestMethod.valueOf(_method); // 必须精准匹配,避免缓存命中率低
22242223
this.setMethod(method);
@@ -2242,6 +2241,18 @@ protected M getRequestStructure(RequestMethod method, String tag, int version) t
22422241
return object;
22432242
}
22442243

2244+
public static final Map<String, RequestMethod> KEY_METHOD_ENUM_MAP;
2245+
static {
2246+
KEY_METHOD_ENUM_MAP = new LinkedHashMap<>();
2247+
KEY_METHOD_ENUM_MAP.put(KEY_GET, RequestMethod.GET);
2248+
KEY_METHOD_ENUM_MAP.put(KEY_GETS, RequestMethod.GETS);
2249+
KEY_METHOD_ENUM_MAP.put(KEY_HEAD, RequestMethod.HEAD);
2250+
KEY_METHOD_ENUM_MAP.put(KEY_HEADS, RequestMethod.HEADS);
2251+
KEY_METHOD_ENUM_MAP.put(KEY_POST, RequestMethod.POST);
2252+
KEY_METHOD_ENUM_MAP.put(KEY_PUT, RequestMethod.PUT);
2253+
KEY_METHOD_ENUM_MAP.put(KEY_DELETE, RequestMethod.DELETE);
2254+
}
2255+
22452256
protected M batchVerify(RequestMethod method, String tag, int version, String name, @NotNull M request, int maxUpdateCount, SQLCreator<T, M, L> creator) throws Exception {
22462257
M correctRequest = JSON.createJSONObject();
22472258
List<String> removeTmpKeys = new ArrayList<>(); // 请求json里面的临时变量,不需要带入后面的业务中,比如 @post、@get等
@@ -2253,14 +2264,14 @@ protected M batchVerify(RequestMethod method, String tag, int version, String na
22532264

22542265
for (String key : reqSet) {
22552266
// key 重复直接抛错(xxx:alias, xxx:alias[])
2256-
if (correctRequest.containsKey(key) || correctRequest.containsKey(key + JSONMap.KEY_ARRAY)) {
2267+
if (correctRequest.containsKey(key) || correctRequest.containsKey(key + KEY_ARRAY)) {
22572268
throw new IllegalArgumentException("对象名重复,请添加别名区分 ! 重复对象名为: " + key);
22582269
}
22592270

2260-
boolean isPost = JSONMap.KEY_POST.equals(key);
2271+
boolean isPost = KEY_POST.equals(key);
22612272
// @post、@get 等 RequestMethod
22622273
try {
2263-
RequestMethod keyMethod = isPost ? RequestMethod.POST : RequestMethod.valueOf(key.substring(1).toUpperCase());
2274+
RequestMethod keyMethod = isPost ? RequestMethod.POST : KEY_METHOD_ENUM_MAP.get(key);
22642275
if (keyMethod != null) {
22652276
// 如果不匹配,异常不处理即可
22662277
removeTmpKeys.add(key);
@@ -2278,7 +2289,7 @@ protected M batchVerify(RequestMethod method, String tag, int version, String na
22782289
throw new ConflictException(key + ": value 中 " + tbl + " 已经存在,不能重复!");
22792290
}
22802291

2281-
obj.put(tbl, isPost && JSONMap.isTableArray(tbl)
2292+
obj.put(tbl, isPost && isTableArray(tbl)
22822293
? tbl.substring(0, tbl.length() - 2) + ":[]" : "");
22832294
}
22842295
}
@@ -2297,7 +2308,7 @@ protected M batchVerify(RequestMethod method, String tag, int version, String na
22972308
}
22982309

22992310
Map<String, Object> objAttrMap = new HashMap<>();
2300-
objAttrMap.put(JSONMap.KEY_METHOD, keyMethod);
2311+
objAttrMap.put(KEY_METHOD, keyMethod);
23012312
keyObjectAttributesMap.put(objKey, objAttrMap);
23022313

23032314
Object objVal = objEntry.getValue();
@@ -2321,11 +2332,11 @@ protected M batchVerify(RequestMethod method, String tag, int version, String na
23212332
}
23222333

23232334
switch (objAttrKey) {
2324-
case JSONMap.KEY_DATASOURCE:
2325-
case JSONMap.KEY_SCHEMA:
2326-
case JSONMap.KEY_DATABASE:
2335+
case KEY_DATASOURCE:
2336+
case KEY_SCHEMA:
2337+
case KEY_DATABASE:
23272338
case apijson.JSONRequest.KEY_VERSION:
2328-
case JSONMap.KEY_ROLE:
2339+
case KEY_ROLE:
23292340
objAttrMap.put(objAttrKey, entry.getValue());
23302341
break;
23312342
case KEY_TAG:
@@ -2338,7 +2349,7 @@ protected M batchVerify(RequestMethod method, String tag, int version, String na
23382349
}
23392350

23402351
if (hasTag == false) {
2341-
objAttrMap.put(KEY_TAG, isPost && JSONMap.isTableArray(objKey)
2352+
objAttrMap.put(KEY_TAG, isPost && isTableArray(objKey)
23422353
? objKey.substring(0, objKey.length() - 2) + ":[]" : objKey);
23432354
}
23442355
}
@@ -2357,33 +2368,33 @@ protected M batchVerify(RequestMethod method, String tag, int version, String na
23572368

23582369
if (attrMap == null) {
23592370
// 数组会解析为对象进行校验,做一下兼容
2360-
if (keyObjectAttributesMap.get(key + JSONMap.KEY_ARRAY) == null) {
2371+
if (keyObjectAttributesMap.get(key + KEY_ARRAY) == null) {
23612372
if (method == RequestMethod.CRUD || key.endsWith("@")) {
2362-
((Map<String, Object>) obj).put(JSONMap.KEY_METHOD, GET);
2373+
((Map<String, Object>) obj).put(KEY_METHOD, GET);
23632374
Map<String, Object> objAttrMap = new HashMap<>();
2364-
objAttrMap.put(JSONMap.KEY_METHOD, GET);
2375+
objAttrMap.put(KEY_METHOD, GET);
23652376
keyObjectAttributesMap.put(key, objAttrMap);
23662377
} else {
2367-
((Map<String, Object>) obj).put(JSONMap.KEY_METHOD, method);
2378+
((Map<String, Object>) obj).put(KEY_METHOD, method);
23682379
Map<String, Object> objAttrMap = new HashMap<>();
2369-
objAttrMap.put(JSONMap.KEY_METHOD, method);
2380+
objAttrMap.put(KEY_METHOD, method);
23702381
keyObjectAttributesMap.put(key, objAttrMap);
23712382
}
23722383
} else {
2373-
setRequestAttribute(key, true, JSONMap.KEY_METHOD, request);
2374-
setRequestAttribute(key, true, JSONMap.KEY_DATASOURCE, request);
2375-
setRequestAttribute(key, true, JSONMap.KEY_SCHEMA, request);
2376-
setRequestAttribute(key, true, JSONMap.KEY_DATABASE, request);
2384+
setRequestAttribute(key, true, KEY_METHOD, request);
2385+
setRequestAttribute(key, true, KEY_DATASOURCE, request);
2386+
setRequestAttribute(key, true, KEY_SCHEMA, request);
2387+
setRequestAttribute(key, true, KEY_DATABASE, request);
23772388
setRequestAttribute(key, true, apijson.JSONRequest.KEY_VERSION, request);
2378-
setRequestAttribute(key, true, JSONMap.KEY_ROLE, request);
2389+
setRequestAttribute(key, true, KEY_ROLE, request);
23792390
}
23802391
} else {
2381-
setRequestAttribute(key, false, JSONMap.KEY_METHOD, request);
2382-
setRequestAttribute(key, false, JSONMap.KEY_DATASOURCE, request);
2383-
setRequestAttribute(key, false, JSONMap.KEY_SCHEMA, request);
2384-
setRequestAttribute(key, false, JSONMap.KEY_DATABASE, request);
2392+
setRequestAttribute(key, false, KEY_METHOD, request);
2393+
setRequestAttribute(key, false, KEY_DATASOURCE, request);
2394+
setRequestAttribute(key, false, KEY_SCHEMA, request);
2395+
setRequestAttribute(key, false, KEY_DATABASE, request);
23852396
setRequestAttribute(key, false, apijson.JSONRequest.KEY_VERSION, request);
2386-
setRequestAttribute(key, false, JSONMap.KEY_ROLE, request);
2397+
setRequestAttribute(key, false, KEY_ROLE, request);
23872398
}
23882399
}
23892400

@@ -2396,7 +2407,7 @@ protected M batchVerify(RequestMethod method, String tag, int version, String na
23962407
RequestMethod _method;
23972408
if (obj instanceof Map<?, ?>) {
23982409
Map<String, Object> tblObj = JSON.getMap(request, key);
2399-
String mn = tblObj == null ? null : getString(tblObj, JSONMap.KEY_METHOD);
2410+
String mn = tblObj == null ? null : getString(tblObj, KEY_METHOD);
24002411
_method = mn == null ? null : RequestMethod.valueOf(mn);
24012412
String combine = _method == null ? null : getString(tblObj, KEY_COMBINE);
24022413
if (combine != null && RequestMethod.isPublicMethod(_method) == false) {
@@ -2409,16 +2420,16 @@ protected M batchVerify(RequestMethod method, String tag, int version, String na
24092420
if (method == RequestMethod.CRUD) {
24102421
_method = GET;
24112422
Map<String, Object> objAttrMap = new HashMap<>();
2412-
objAttrMap.put(JSONMap.KEY_METHOD, GET);
2423+
objAttrMap.put(KEY_METHOD, GET);
24132424
keyObjectAttributesMap.put(key, objAttrMap);
24142425
} else {
24152426
_method = method;
24162427
Map<String, Object> objAttrMap = new HashMap<>();
2417-
objAttrMap.put(JSONMap.KEY_METHOD, method);
2428+
objAttrMap.put(KEY_METHOD, method);
24182429
keyObjectAttributesMap.put(key, objAttrMap);
24192430
}
24202431
} else {
2421-
_method = (RequestMethod) attrMap.get(JSONMap.KEY_METHOD);
2432+
_method = (RequestMethod) attrMap.get(KEY_METHOD);
24222433
}
24232434
}
24242435

@@ -2479,7 +2490,7 @@ public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final Stri
24792490
}
24802491

24812492
protected void setRequestAttribute(String key, boolean isArray, String attrKey, @NotNull Map<String, Object> request) {
2482-
Map<String, Object> attrMap = keyObjectAttributesMap.get(isArray ? key + JSONMap.KEY_ARRAY : key);
2493+
Map<String, Object> attrMap = keyObjectAttributesMap.get(isArray ? key + KEY_ARRAY : key);
24832494
Object attrVal = attrMap == null ? null : attrMap.get(attrKey);
24842495
Map<String, Object> obj = attrVal == null ? null : JSON.get(request, key);
24852496

@@ -2520,7 +2531,7 @@ protected M objectVerify(RequestMethod method, String tag, int version, String n
25202531
public RequestMethod getRealMethod(RequestMethod method, String key, Object value) {
25212532
if (method == CRUD && (value instanceof Map<?, ?> || value instanceof List<?>)) {
25222533
Map<String, Object> attrMap = keyObjectAttributesMap.get(key);
2523-
Object _method = attrMap == null ? null : attrMap.get(JSONMap.KEY_METHOD);
2534+
Object _method = attrMap == null ? null : attrMap.get(KEY_METHOD);
25242535
if (_method instanceof RequestMethod) {
25252536
return (RequestMethod) _method;
25262537
}

0 commit comments

Comments
 (0)