Skip to content

Commit 86c816d

Browse files
修复 校验模块 "tag": "Comment:[]" bug
crud支持Common:[] 新增,修改
1 parent 8efd38e commit 86c816d

File tree

1 file changed

+68
-40
lines changed

1 file changed

+68
-40
lines changed

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

Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -577,14 +577,9 @@ public JSONObject parseCorrectRequest(RequestMethod method, String tag, int vers
577577
return request;//需要指定JSON结构的get请求可以改为post请求。一般只有对安全性要求高的才会指定,而这种情况用明文的GET方式几乎肯定不安全
578578
}
579579

580-
// if (StringUtil.isEmpty(tag, true)) {
581-
// throw new IllegalArgumentException("请在最外层传 tag !一般是 Table 名,例如 \"tag\": \"User\" ");
582-
// }
583-
584580
return batchVerify(method, tag, version, name, request, maxUpdateCount, creator);
585581
}
586-
587-
582+
588583
/**自动根据 tag 是否为 TableKey 及是否被包含在 object 内来决定是否包装一层,改为 { tag: object, "tag": tag }
589584
* @param object
590585
* @param tag
@@ -884,7 +879,9 @@ public static JSONObject newErrorResult(Exception e, boolean isRoot) {
884879
*/
885880
@Override
886881
public JSONObject parseCorrectRequest() throws Exception {
887-
setTag(requestObject.getString(JSONRequest.KEY_TAG));
882+
if(this.getMethod() != RequestMethod.CRUD) {
883+
setTag(requestObject.getString(JSONRequest.KEY_TAG));
884+
}
888885
setVersion(requestObject.getIntValue(JSONRequest.KEY_VERSION));
889886
requestObject.remove(JSONRequest.KEY_TAG);
890887
requestObject.remove(JSONRequest.KEY_VERSION);
@@ -2128,6 +2125,9 @@ private JSONObject batchVerify(RequestMethod method, String tag, int version, St
21282125
case apijson.JSONObject.KEY_ROLE:
21292126
object_attributes_map.put(apijson.JSONObject.KEY_ROLE, objAttrJson.getString(objAttr));
21302127
break;
2128+
case JSONRequest.KEY_TAG:
2129+
object_attributes_map.put(JSONRequest.KEY_TAG, objAttrJson.getString(objAttr));
2130+
break;
21312131
default:
21322132
break;
21332133
}
@@ -2229,12 +2229,14 @@ private JSONObject batchVerify(RequestMethod method, String tag, int version, St
22292229
continue;
22302230
}
22312231

2232-
String _tag = buildTag(request, key);
2232+
String _tag = buildTag(request, key, method, tag);
22332233
JSONObject requestItem = new JSONObject();
2234-
requestItem.put(_tag, request.get(key));
2234+
// key 处理别名
2235+
String _key = keyDelAliase(key);
2236+
requestItem.put(_key, request.get(key));
22352237
JSONObject object = getRequestStructure(_method, _tag, version);
22362238
JSONObject ret = objectVerify(_method, _tag, version, name, requestItem, maxUpdateCount, creator, object);
2237-
jsonObject.put(key, ret.get(_tag));
2239+
jsonObject.put(key, ret.get(_key));
22382240
} else {
22392241
jsonObject.put(key, request.get(key));
22402242
}
@@ -2249,7 +2251,7 @@ private JSONObject batchVerify(RequestMethod method, String tag, int version, St
22492251
}
22502252
return jsonObject;
22512253
}
2252-
2254+
22532255
private void setRequestAttribute(String key, boolean isArray, String attrKey, @NotNull JSONObject request) {
22542256
Object attrVal = null;
22552257
if(isArray) {
@@ -2263,39 +2265,62 @@ private void setRequestAttribute(String key, boolean isArray, String attrKey, @N
22632265
request.getJSONObject(key).put(attrKey, attrVal);
22642266
}
22652267
}
2266-
/**
2267-
* { "xxx:aa":{ "@tag": "" }}
2268-
* 生成规则:
2269-
* 1、@tag存在,tag=@tag
2270-
* 2、@tag不存在
2271-
* 1)、存在别名
2272-
* key=对象: tag=key去除别名
2273-
* key=数组: tag=key去除别名 + []
2274-
* 2)、不存在别名
2275-
* tag=key
2276-
* tag=key + []
2277-
* @param request
2278-
* @param key
2279-
* @return
2280-
*/
2281-
private String buildTag(JSONObject request, String key) {
2282-
String _tag = null;
2283-
if (request.get(key) instanceof JSONObject && request.getJSONObject(key).getString("@tag") != null) {
2284-
_tag = request.getJSONObject(key).getString("@tag");
2285-
} else {
2286-
int keyIndex = key.indexOf(":");
2287-
if (keyIndex != -1) {
2288-
_tag = key.substring(0, keyIndex);
2289-
if (apijson.JSONObject.isTableArray(key)) {
2290-
_tag += apijson.JSONObject.KEY_ARRAY;
2268+
2269+
private String keyDelAliase(String key) {
2270+
int keyIndex = key.indexOf(":");
2271+
if (keyIndex != -1) {
2272+
String _key = key.substring(0, keyIndex);
2273+
if (apijson.JSONObject.isTableArray(key)) {
2274+
_key += apijson.JSONObject.KEY_ARRAY;
2275+
}
2276+
return _key;
2277+
}
2278+
return key;
2279+
}
2280+
2281+
private String buildTag(JSONObject request, String key, RequestMethod method, String tag) {
2282+
if (method == RequestMethod.CRUD) {
2283+
if (keyObjectAttributesMap.get(key) != null && keyObjectAttributesMap.get(key).get(JSONRequest.KEY_TAG) != null) {
2284+
if (request.get(key) instanceof JSONArray) {
2285+
return keyObjectAttributesMap.get(key).get(JSONRequest.KEY_TAG).toString();
2286+
} else {
2287+
tag = keyObjectAttributesMap.get(key).get(JSONRequest.KEY_TAG).toString();
22912288
}
22922289
} else {
2293-
// 不存在别名
2294-
_tag = key;
2290+
// key 作为默认的 tag
2291+
if (StringUtil.isEmpty(tag)) {
2292+
if (request.get(key) instanceof JSONArray) {
2293+
return keyDelAliase(key);
2294+
} else {
2295+
tag = key;
2296+
}
2297+
} else {
2298+
if (request.get(key) instanceof JSONArray) {
2299+
return tag;
2300+
}
2301+
}
2302+
}
2303+
} else {
2304+
if (StringUtil.isEmpty(tag, true)) {
2305+
throw new IllegalArgumentException("请在最外层传 tag !一般是 Table 名,例如 \"tag\": \"User\" ");
2306+
}
2307+
if (request.get(key) instanceof JSONArray) {
2308+
return tag;
22952309
}
22962310
}
2297-
return _tag;
2311+
2312+
// 通用判断
2313+
// 对象, 需处理别名
2314+
if (request.get(key) instanceof JSONObject && StringUtil.isNotEmpty(tag)) {
2315+
int keyIndex = tag.indexOf(":");
2316+
if (keyIndex != -1) {
2317+
return tag.substring(0, keyIndex);
2318+
}
2319+
return tag;
2320+
}
2321+
return tag;
22982322
}
2323+
22992324

23002325
protected JSONObject objectVerify(RequestMethod method, String tag, int version, String name, @NotNull JSONObject request, int maxUpdateCount, SQLCreator creator, JSONObject object) throws Exception {
23012326
// 获取指定的JSON结构 >>>>>>>>>>>>>>
@@ -2311,7 +2336,10 @@ protected JSONObject objectVerify(RequestMethod method, String tag, int version,
23112336
* @return
23122337
*/
23132338
public RequestMethod getRealMethod(RequestMethod method, String key, Object value) {
2314-
if(method == CRUD && key.startsWith("@") == false && (value instanceof JSONObject || value instanceof JSONArray)) {
2339+
if(method == CRUD && (value instanceof JSONObject || value instanceof JSONArray)) {
2340+
if (this.keyObjectAttributesMap.get(key) == null || this.keyObjectAttributesMap.get(key).get(apijson.JSONObject.KEY_METHOD) == null) {
2341+
return method;
2342+
}
23152343
return (RequestMethod)this.keyObjectAttributesMap.get(key).get(apijson.JSONObject.KEY_METHOD);
23162344
}
23172345
return method;

0 commit comments

Comments
 (0)