Skip to content

Commit 77c375b

Browse files
committed
完善请求参数在各种 method 及 tag 下的自动补全;允许 GETS 通过 key[]:{} 来查多条记录
1 parent 3a89b60 commit 77c375b

1 file changed

Lines changed: 38 additions & 9 deletions

File tree

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

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ public JSONObject parseCorrectRequest(RequestMethod method, String tag, int vers
515515
}
516516

517517
//获取指定的JSON结构 >>>>>>>>>>>>>>
518-
JSONObject target = wrapRequest(object, tag, false);
518+
JSONObject target = wrapRequest(method, tag, object, true);
519519

520520
//JSONObject clone 浅拷贝没用,Structure.parse 会导致 structure 里面被清空,第二次从缓存里取到的就是 {}
521521
return getVerifier().verifyRequest(method, name, target, request, maxUpdateCount, getGlobleDatabase(), getGlobleSchema(), creator);
@@ -527,7 +527,9 @@ public JSONObject parseCorrectRequest(RequestMethod method, String tag, int vers
527527
* @param tag
528528
* @return
529529
*/
530-
public static JSONObject wrapRequest(JSONObject object, String tag, boolean putTag) {
530+
public static JSONObject wrapRequest(RequestMethod method, String tag, JSONObject object, boolean isStructure) {
531+
boolean putTag = ! isStructure;
532+
531533
if (object == null || object.containsKey(tag)) { //tag 是 Table 名或 Table[]
532534
if (putTag) {
533535
if (object == null) {
@@ -544,12 +546,39 @@ public static JSONObject wrapRequest(JSONObject object, String tag, boolean putT
544546

545547
JSONObject target = object;
546548
if (apijson.JSONObject.isTableKey(key)) {
547-
if (isDiffArrayKey) { //自动为 tag = Comment:[] 的 { ... } 新增键值对 "Comment[]":[] 为 { "Comment[]":[], ... }
548-
target.put(key + "[]", new JSONArray());
549+
if (isDiffArrayKey) { //自动为 tag = Comment:[] 的 { ... } 新增键值对为 { "Comment[]":[], "TYPE": { "Comment[]": "OBJECT[]" } ... }
550+
if (isStructure && (method == RequestMethod.POST || method == RequestMethod.PUT)) {
551+
String arrKey = key + "[]";
552+
553+
if (target.containsKey(arrKey) == false) {
554+
target.put(arrKey, new JSONArray());
555+
}
556+
557+
try {
558+
JSONObject type = target.getJSONObject(Operation.TYPE.name());
559+
if (type == null || (type.containsKey(arrKey) == false)) {
560+
if (type == null) {
561+
type = new JSONObject(true);
562+
}
563+
564+
type.put(arrKey, "OBJECT[]");
565+
target.put(Operation.TYPE.name(), type);
566+
}
567+
}
568+
catch (Throwable e) {
569+
Log.w(TAG, "wrapRequest try { JSONObject type = target.getJSONObject(Operation.TYPE.name()); } catch (Exception e) = " + e.getMessage());
570+
}
571+
}
549572
}
550573
else { //自动为 tag = Comment 的 { ... } 包一层为 { "Comment": { ... } }
551-
target = new JSONObject(true);
552-
target.put(tag, object);
574+
if (isArrayKey == false || RequestMethod.isGetMethod(method, true)) {
575+
target = new JSONObject(true);
576+
target.put(tag, object);
577+
}
578+
else if (target.containsKey(key) == false) {
579+
target = new JSONObject(true);
580+
target.put(key, object);
581+
}
553582
}
554583
}
555584

@@ -958,10 +987,10 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
958987
}
959988

960989
//不能允许GETS,否则会被通过"[]":{"@role":"ADMIN"},"Table":{},"tag":"Table"绕过权限并能批量查询
961-
if (isSubquery == false && RequestMethod.isGetMethod(requestMethod, false) == false) {
962-
throw new UnsupportedOperationException("key[]:{}只支持GET方法!不允许传 " + name + ":{} !");
990+
if (isSubquery == false && RequestMethod.isGetMethod(requestMethod, true) == false) {
991+
throw new UnsupportedOperationException("key[]:{} 只支持 GET, GETS 方法!其它方法不允许传 " + name + ":{} 等这种 key[]:{} 格式!");
963992
}
964-
if (request == null || request.isEmpty()) {//jsonKey-jsonValue条件
993+
if (request == null || request.isEmpty()) { // jsonKey-jsonValue 条件
965994
return null;
966995
}
967996
String path = getAbsPath(parentPath, name);

0 commit comments

Comments
 (0)