@@ -410,11 +410,12 @@ public JSONObject parseResponse(JSONObject request) {
410
410
requestObject = request ;
411
411
try {
412
412
setVersion (requestObject .getIntValue (JSONRequest .KEY_VERSION ));
413
+ requestObject .remove (JSONRequest .KEY_VERSION );
414
+
413
415
if (getMethod () != RequestMethod .CRUD ) {
414
416
setTag (requestObject .getString (JSONRequest .KEY_TAG ));
415
417
requestObject .remove (JSONRequest .KEY_TAG );
416
418
}
417
- requestObject .remove (JSONRequest .KEY_VERSION );
418
419
} catch (Exception e ) {
419
420
return extendErrorResult (requestObject , e , requestMethod , getRequestURL (), isRoot );
420
421
}
@@ -2089,7 +2090,7 @@ protected JSONObject getRequestStructure(RequestMethod method, String tag, int v
2089
2090
}
2090
2091
2091
2092
protected JSONObject batchVerify (RequestMethod method , String tag , int version , String name , @ NotNull JSONObject request , int maxUpdateCount , SQLCreator creator ) throws Exception {
2092
- JSONObject jsonObject = new JSONObject (true );
2093
+ JSONObject correctRequest = new JSONObject (true );
2093
2094
List <String > removeTmpKeys = new ArrayList <>(); // 请求json里面的临时变量,不需要带入后面的业务中,比如 @post、@get等
2094
2095
2095
2096
Set <String > reqSet = request == null ? null : request .keySet ();
@@ -2098,49 +2099,82 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
2098
2099
}
2099
2100
2100
2101
for (String key : reqSet ) {
2101
- // key重复直接抛错 (xxx:alias, xxx:alias[])
2102
- if (jsonObject .containsKey (key ) || jsonObject .containsKey (key + apijson .JSONObject .KEY_ARRAY )) {
2103
- throw new IllegalArgumentException ("对象名重复,请添加别名区分 ! , 重复对象名为: " + key );
2102
+ // key 重复直接抛错 (xxx:alias, xxx:alias[])
2103
+ if (correctRequest .containsKey (key ) || correctRequest .containsKey (key + apijson .JSONObject .KEY_ARRAY )) {
2104
+ throw new IllegalArgumentException ("对象名重复,请添加别名区分 ! 重复对象名为: " + key );
2104
2105
}
2105
2106
2106
- // @post、@get等RequestMethod
2107
+ // @post、@get 等 RequestMethod
2107
2108
try {
2108
- if (key .startsWith ("@" ) && getEnum (RequestMethod .class , key .substring (1 ).toUpperCase (), null ) != null ) {
2109
+ RequestMethod keyMethod = apijson .orm .JSONRequest .KEY_METHOD_ENUM_MAP .get (key );
2110
+ if (keyMethod != null ) {
2109
2111
// 如果不匹配,异常不处理即可
2110
- RequestMethod _method = RequestMethod .valueOf (key .substring (1 ).toUpperCase ());
2111
2112
removeTmpKeys .add (key );
2112
2113
2113
- JSONObject obj = request .getJSONObject (key );
2114
- Set <String > set = obj == null ? new HashSet <>() : obj .keySet ();
2114
+ Object val = request .get (key );
2115
+ JSONObject obj = val instanceof JSONObject ? request .getJSONObject (key ) : null ;
2116
+ if (obj == null ) {
2117
+ if (val instanceof String ) {
2118
+ String [] tbls = StringUtil .split ((String ) val );
2119
+ if (tbls != null && tbls .length > 0 ) {
2120
+ obj = new JSONObject (true );
2121
+ for (int i = 0 ; i < tbls .length ; i ++) {
2122
+ String tbl = tbls [i ];
2123
+ if (obj .containsKey (tbl )) {
2124
+ throw new ConflictException (key + ": value 中 " + tbl + " 已经存在,不能重复!" );
2125
+ }
2126
+ obj .put (tbl , new JSONObject (true ));
2127
+ }
2128
+ }
2129
+ }
2130
+ else {
2131
+ throw new IllegalArgumentException (key + ": value 中 value 类型错误,只能是 String 或 JSONObject {} !" );
2132
+ }
2133
+ }
2134
+
2135
+ Set <Entry <String , Object >> set = obj == null ? new HashSet <>() : obj .entrySet ();
2115
2136
2116
- for (String objKey : set ) {
2137
+ for (Entry <String , Object > objEntry : set ) {
2138
+ String objKey = objEntry == null ? null : objEntry .getKey ();
2117
2139
if (objKey == null ) {
2118
2140
continue ;
2119
2141
}
2120
2142
2121
2143
Map <String , Object > objAttrMap = new HashMap <>();
2122
- objAttrMap .put (apijson .JSONObject .KEY_METHOD , _method );
2144
+ objAttrMap .put (apijson .JSONObject .KEY_METHOD , keyMethod );
2123
2145
keyObjectAttributesMap .put (objKey , objAttrMap );
2124
- JSONObject objAttrJson = obj .getJSONObject (objKey );
2125
- Set <Entry <String , Object >> objSet = objAttrJson == null ? new HashSet <>() : objAttrJson .entrySet ();
2126
2146
2127
- for (Entry <String , Object > entry : objSet ) {
2128
- String objAttrKey = entry == null ? null : entry .getKey ();
2129
- if (objAttrKey == null ) {
2130
- continue ;
2147
+ Object objVal = objEntry .getValue ();
2148
+ JSONObject objAttrJson = objVal instanceof JSONObject ? obj .getJSONObject (objKey ) : null ;
2149
+ if (objAttrJson == null ) {
2150
+ if (objVal instanceof String ) {
2151
+ objAttrMap .put (JSONRequest .KEY_TAG , objVal );
2131
2152
}
2153
+ else {
2154
+ throw new IllegalArgumentException (key + ": { " + objKey + ": value 中 value 类型错误,只能是 String 或 JSONObject {} !" );
2155
+ }
2156
+ }
2157
+ else {
2158
+ Set <Entry <String , Object >> objSet = objAttrJson == null ? new HashSet <>() : objAttrJson .entrySet ();
2132
2159
2133
- switch (objAttrKey ) {
2134
- case apijson .JSONObject .KEY_DATASOURCE :
2135
- case apijson .JSONObject .KEY_SCHEMA :
2136
- case apijson .JSONObject .KEY_DATABASE :
2137
- case JSONRequest .KEY_VERSION :
2138
- case apijson .JSONObject .KEY_ROLE :
2139
- case JSONRequest .KEY_TAG :
2140
- objAttrMap .put (objAttrKey , entry .getValue ());
2141
- break ;
2142
- default :
2143
- break ;
2160
+ for (Entry <String , Object > entry : objSet ) {
2161
+ String objAttrKey = entry == null ? null : entry .getKey ();
2162
+ if (objAttrKey == null ) {
2163
+ continue ;
2164
+ }
2165
+
2166
+ switch (objAttrKey ) {
2167
+ case apijson .JSONObject .KEY_DATASOURCE :
2168
+ case apijson .JSONObject .KEY_SCHEMA :
2169
+ case apijson .JSONObject .KEY_DATABASE :
2170
+ case JSONRequest .KEY_VERSION :
2171
+ case apijson .JSONObject .KEY_ROLE :
2172
+ case JSONRequest .KEY_TAG :
2173
+ objAttrMap .put (objAttrKey , entry .getValue ());
2174
+ break ;
2175
+ default :
2176
+ break ;
2177
+ }
2144
2178
}
2145
2179
}
2146
2180
}
@@ -2189,15 +2223,17 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
2189
2223
}
2190
2224
2191
2225
if (key .startsWith ("@" ) || key .endsWith ("@" )) {
2192
- jsonObject .put (key , obj );
2226
+ correctRequest .put (key , obj );
2193
2227
continue ;
2194
2228
}
2195
2229
2196
2230
if (obj instanceof JSONObject || obj instanceof JSONArray ) {
2197
- RequestMethod _method = null ;
2231
+ RequestMethod _method ;
2198
2232
if (obj instanceof JSONObject ) {
2199
- _method = RequestMethod .valueOf (request .getJSONObject (key ).getString (apijson .JSONObject .KEY_METHOD ).toUpperCase ());
2200
- String combine = request .getJSONObject (key ).getString (KEY_COMBINE );
2233
+ JSONObject tblObj = request .getJSONObject (key );
2234
+ String mn = tblObj == null ? null : tblObj .getString (apijson .JSONObject .KEY_METHOD );
2235
+ _method = mn == null ? null : RequestMethod .valueOf (mn );
2236
+ String combine = _method == null ? null : tblObj .getString (KEY_COMBINE );
2201
2237
if (combine != null && RequestMethod .isPublicMethod (_method ) == false ) {
2202
2238
throw new IllegalArgumentException (key + ":{} 里的 @combine:value 不合法!开放请求 GET、HEAD 才允许传 @combine:value !" );
2203
2239
}
@@ -2207,22 +2243,14 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
2207
2243
if (attrMap == null ) {
2208
2244
if (method == RequestMethod .CRUD ) {
2209
2245
_method = GET ;
2210
- if (attrMap == null ) {
2211
- Map <String , Object > objAttrMap = new HashMap <>();
2212
- objAttrMap .put (apijson .JSONObject .KEY_METHOD , GET );
2213
- keyObjectAttributesMap .put (key , objAttrMap );
2214
- } else {
2215
- attrMap .put (apijson .JSONObject .KEY_METHOD , GET );
2216
- }
2246
+ Map <String , Object > objAttrMap = new HashMap <>();
2247
+ objAttrMap .put (apijson .JSONObject .KEY_METHOD , GET );
2248
+ keyObjectAttributesMap .put (key , objAttrMap );
2217
2249
} else {
2218
2250
_method = method ;
2219
- if (attrMap == null ) {
2220
- Map <String , Object > objAttrMap = new HashMap <>();
2221
- objAttrMap .put (apijson .JSONObject .KEY_METHOD , method );
2222
- keyObjectAttributesMap .put (key , objAttrMap );
2223
- } else {
2224
- attrMap .put (apijson .JSONObject .KEY_METHOD , method );
2225
- }
2251
+ Map <String , Object > objAttrMap = new HashMap <>();
2252
+ objAttrMap .put (apijson .JSONObject .KEY_METHOD , method );
2253
+ keyObjectAttributesMap .put (key , objAttrMap );
2226
2254
}
2227
2255
} else {
2228
2256
_method = (RequestMethod ) attrMap .get (apijson .JSONObject .KEY_METHOD );
@@ -2236,42 +2264,42 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
2236
2264
2237
2265
// get请求不校验
2238
2266
if (RequestMethod .isPublicMethod (_method )) {
2239
- jsonObject .put (key , obj );
2267
+ correctRequest .put (key , obj );
2240
2268
continue ;
2241
2269
}
2242
2270
2243
- if (tag != null && !tag .contains (":" )) {
2271
+ if (tag != null && ! tag .contains (":" )) {
2244
2272
JSONObject object = getRequestStructure (_method , tag , version );
2245
2273
JSONObject ret = objectVerify (_method , tag , version , name , request , maxUpdateCount , creator , object );
2246
- jsonObject .putAll (ret );
2274
+ correctRequest .putAll (ret );
2247
2275
break ;
2248
2276
}
2249
2277
2250
2278
String _tag = buildTag (request , key , method , tag );
2251
2279
JSONObject object = getRequestStructure (_method , _tag , version );
2252
- if (method == RequestMethod .CRUD && StringUtil .isEmpty (tag , true )) {
2280
+ if (method == RequestMethod .CRUD && StringUtil .isEmpty (tag , true )) {
2253
2281
JSONObject requestItem = new JSONObject ();
2254
2282
requestItem .put (key , obj );
2255
2283
JSONObject ret = objectVerify (_method , _tag , version , name , requestItem , maxUpdateCount , creator , object );
2256
- jsonObject .put (key , ret .get (key ));
2284
+ correctRequest .put (key , ret .get (key ));
2257
2285
} else {
2258
2286
return objectVerify (_method , _tag , version , name , request , maxUpdateCount , creator , object );
2259
2287
}
2260
2288
} else {
2261
- jsonObject .put (key , obj );
2289
+ correctRequest .put (key , obj );
2262
2290
}
2263
2291
} catch (Exception e ) {
2264
2292
e .printStackTrace ();
2265
2293
throw new Exception (e );
2266
2294
}
2267
2295
}
2268
2296
2269
- // 这里是requestObject ref request 的引用, 删除不需要的临时变量
2297
+ // 这里是 requestObject ref request 的引用, 删除不需要的临时变量
2270
2298
for (String removeKey : removeTmpKeys ) {
2271
2299
request .remove (removeKey );
2272
2300
}
2273
2301
2274
- return jsonObject ;
2302
+ return correctRequest ;
2275
2303
}
2276
2304
2277
2305
public static <E extends Enum <E >> E getEnum (final Class <E > enumClass , final String enumName , final E defaultEnum ) {
@@ -2284,7 +2312,7 @@ public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final Stri
2284
2312
return defaultEnum ;
2285
2313
}
2286
2314
}
2287
-
2315
+
2288
2316
protected void setRequestAttribute (String key , boolean isArray , String attrKey , @ NotNull JSONObject request ) {
2289
2317
Map <String , Object > attrMap = keyObjectAttributesMap .get (isArray ? key + apijson .JSONObject .KEY_ARRAY : key );
2290
2318
Object attrVal = attrMap == null ? null : attrMap .get (attrKey );
@@ -2308,7 +2336,7 @@ protected String buildTag(JSONObject request, String key, RequestMethod method,
2308
2336
}
2309
2337
return tag ;
2310
2338
}
2311
-
2339
+
2312
2340
2313
2341
protected JSONObject objectVerify (RequestMethod method , String tag , int version , String name , @ NotNull JSONObject request
2314
2342
, int maxUpdateCount , SQLCreator creator , JSONObject object ) throws Exception {
@@ -2317,7 +2345,7 @@ protected JSONObject objectVerify(RequestMethod method, String tag, int version,
2317
2345
// JSONObject clone 浅拷贝没用,Structure.parse 会导致 structure 里面被清空,第二次从缓存里取到的就是 {}
2318
2346
return getVerifier ().verifyRequest (method , name , target , request , maxUpdateCount , getGlobalDatabase (), getGlobalSchema (), creator );
2319
2347
}
2320
-
2348
+
2321
2349
/***
2322
2350
* 兼容url crud, 获取真实method
2323
2351
* @param method = crud
0 commit comments