Skip to content

Commit a86832a

Browse files
committed
Server:支持自动补全 Comment:[] 这种每个项都自定义的 批量新增/修改 的请求配置,将这种请求返回的结构 Table[]:{} 改为和单个新增/修改 一样的结构 Table:{}
1 parent 8b3ee34 commit a86832a

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

APIJSON-Java-Server/APIJSONFramework/src/main/java/apijson/framework/StructureUtil.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,21 @@ public static JSONObject init(boolean shutdownWhenServerError, ParserCreator<Lon
138138
JSONObject target = null;
139139

140140
if (structure != null) {
141-
if (apijson.JSONObject.isTableKey(tag) && structure.containsKey(tag) == false) {//tag是table名
142-
target = new JSONObject(true);
143-
target.put(tag, structure);
144-
} else {
145-
target = structure;
141+
target = structure;
142+
if (structure.containsKey(tag) == false) { //tag 是 Table 名或 Table[]
143+
144+
boolean isArrayKey = tag.endsWith(":[]"); // JSONRequest.isArrayKey(tag);
145+
String key = isArrayKey ? tag.substring(0, tag.length() - 3) : tag;
146+
147+
if (apijson.JSONObject.isTableKey(key)) {
148+
if (isArrayKey) { //自动为 tag = Comment:[] 的 { ... } 新增键值对 "Comment[]":[] 为 { "Comment[]":[], ... }
149+
target.put(key + "[]", new JSONArray());
150+
}
151+
else { //自动为 tag = Comment 的 { ... } 包一层为 { "Comment": { ... } }
152+
target = new JSONObject(true);
153+
target.put(tag, structure);
154+
}
155+
}
146156
}
147157
}
148158

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,19 +469,20 @@ public JSON onChildParse(int index, String key, JSONObject value) throws Excepti
469469
child = parser.onArrayParse(value, path, key, isSubquery);
470470
isEmpty = child == null || ((JSONArray) child).isEmpty();
471471
}
472-
else {//APIJSON Object
472+
else { //APIJSON Object
473473
boolean isTableKey = JSONRequest.isTableKey(Pair.parseEntry(key, true).getKey());
474474
if (type == TYPE_ITEM && isTableKey == false) {
475475
throw new IllegalArgumentException(parentPath + "/" + key + ":{} 不合法!"
476476
+ "数组 []:{} 中每个 key:{} 都必须是表 TableKey:{} 或 数组 arrayKey[]:{} !");
477477
}
478478

479-
if (//避免使用 "test":{"Test":{}} 绕过限制,实现查询爆炸 isTableKey &&
479+
if ( //避免使用 "test":{"Test":{}} 绕过限制,实现查询爆炸 isTableKey &&
480480
(arrayConfig == null || arrayConfig.getPosition() == 0)) {
481481
objectCount ++;
482482
int maxObjectCount = parser.getMaxObjectCount();
483-
if (objectCount > maxObjectCount) {
484-
throw new IllegalArgumentException(path + " 内截至 " + key + ":{} 时对象 key:{} 的数量达到 " + objectCount + " 已超限,必须在 0-" + maxObjectCount + " 内 !");
483+
if (objectCount > maxObjectCount) { //TODO 这里判断是批量新增/修改,然后上限为 maxUpdateCount
484+
throw new IllegalArgumentException(path + " 内截至 " + key + ":{} 时对象"
485+
+ " key:{} 的数量达到 " + objectCount + " 已超限,必须在 0-" + maxObjectCount + " 内 !");
485486
}
486487
}
487488

@@ -606,7 +607,7 @@ public void onTableArrayParse(String key, JSONArray value) throws Exception {
606607
allResult.put(JSONResponse.KEY_COUNT, allCount);
607608
allResult.put(idKey + "[]", ids);
608609

609-
response.put(key, allResult); //不按原样返回,避免数据量过大
610+
response.put(childKey, allResult); //不按原样返回,避免数据量过大
610611
}
611612

612613

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ public JSONObject parseCorrectRequest(RequestMethod method, String tag, int vers
464464
}
465465

466466
if (StringUtil.isEmpty(tag, true)) {
467-
throw new IllegalArgumentException("请在最外层设置 tag !一般是 Table 名,例如 \"tag\": \"User\" ");
467+
throw new IllegalArgumentException("请在最外层传 tag !一般是 Table 名,例如 \"tag\": \"User\" ");
468468
}
469469

470470
//获取指定的JSON结构 <<<<<<<<<<<<
@@ -475,17 +475,28 @@ public JSONObject parseCorrectRequest(RequestMethod method, String tag, int vers
475475
} catch (Exception e) {
476476
error = e.getMessage();
477477
}
478-
if (object == null) {//empty表示随意操作 || object.isEmpty()) {
479-
throw new UnsupportedOperationException("非开放请求必须是后端 Request 表中校验规则允许的操作!\n " + error);
478+
if (object == null) { //empty表示随意操作 || object.isEmpty()) {
479+
throw new UnsupportedOperationException("找不到 version: " + version + ", method: " + method.name() + ", tag: " + tag + " 对应的 structure !"
480+
+ "非开放请求必须是后端 Request 表中校验规则允许的操作!\n " + error + "\n如果需要则在 Request 表中新增配置!");
480481
}
481482

482-
JSONObject target = null;
483-
if (apijson.JSONObject.isTableKey(tag) && object.containsKey(tag) == false) {//tag是table名
484-
target = new JSONObject(true);
485-
target.put(tag, object);
486-
} else {
487-
target = object;
483+
JSONObject target = object;
484+
if (object.containsKey(tag) == false) { //tag 是 Table 名或 Table[]
485+
486+
boolean isArrayKey = tag.endsWith(":[]"); // JSONRequest.isArrayKey(tag);
487+
String key = isArrayKey ? tag.substring(0, tag.length() - 3) : tag;
488+
489+
if (apijson.JSONObject.isTableKey(key)) {
490+
if (isArrayKey) { //自动为 tag = Comment:[] 的 { ... } 新增键值对 "Comment[]":[] 为 { "Comment[]":[], ... }
491+
target.put(key + "[]", new JSONArray());
492+
}
493+
else { //自动为 tag = Comment 的 { ... } 包一层为 { "Comment": { ... } }
494+
target = new JSONObject(true);
495+
target.put(tag, object);
496+
}
497+
}
488498
}
499+
489500
//获取指定的JSON结构 >>>>>>>>>>>>>>
490501

491502
//JSONObject clone 浅拷贝没用,Structure.parse 会导致 structure 里面被清空,第二次从缓存里取到的就是 {}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,13 @@ public JSONObject onParseJSONObject(String key, JSONObject tobj, JSONObject robj
143143
protected JSONArray onParseJSONArray(String key, JSONArray tarray, JSONArray rarray) throws Exception {
144144
if ((method == RequestMethod.POST || method == RequestMethod.PUT) && JSONRequest.isArrayKey(key)) {
145145
if (rarray == null || rarray.isEmpty()) {
146-
throw new IllegalArgumentException(method + "请求,请在 " + name + " 内传 " + key + ":[{}] ,批量新增 Table[]:value 中 value 必须是包含表对象的非空数组!");
146+
throw new IllegalArgumentException(method + "请求,请在 " + name + " 内传 " + key + ":[{ ... }] "
147+
+ ",批量新增 Table[]:value 中 value 必须是包含表对象的非空数组!其中每个子项 { ... } 都是"
148+
+ " tag:" + key.substring(0, key.length() - 2) + " 对应单个新增的 structure !");
147149
}
148150
if (rarray.size() > maxUpdateCount) {
149151
throw new IllegalArgumentException(method + "请求," + name + "/" + key
150-
+ " 里面的 " + key + ":[{}] 中[]的长度不能超过 " + maxUpdateCount + " !");
152+
+ " 里面的 " + key + ":[{ ... }] 中 [] 的长度不能超过 " + maxUpdateCount + " !");
151153
}
152154
}
153155
return super.onParseJSONArray(key, tarray, rarray);

0 commit comments

Comments
 (0)