@@ -410,11 +410,12 @@ public JSONObject parseResponse(JSONObject request){
410410requestObject = request ;
411411try {
412412setVersion (requestObject .getIntValue (JSONRequest .KEY_VERSION ));
413+ requestObject .remove (JSONRequest .KEY_VERSION );
414+
413415if (getMethod () != RequestMethod .CRUD ){
414416setTag (requestObject .getString (JSONRequest .KEY_TAG ));
415417requestObject .remove (JSONRequest .KEY_TAG );
416418 }
417- requestObject .remove (JSONRequest .KEY_VERSION );
418419 } catch (Exception e ){
419420return extendErrorResult (requestObject , e , requestMethod , getRequestURL (), isRoot );
420421 }
@@ -2089,7 +2090,7 @@ protected JSONObject getRequestStructure(RequestMethod method, String tag, int v
20892090 }
20902091
20912092protected 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 );
20932094List <String > removeTmpKeys = new ArrayList <>(); // 请求json里面的临时变量,不需要带入后面的业务中,比如 @post、@get等
20942095
20952096Set <String > reqSet = request == null ? null : request .keySet ();
@@ -2098,49 +2099,82 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
20982099 }
20992100
21002101for (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 );
21042105 }
21052106
2106- // @post、@get等RequestMethod
2107+ // @post、@get 等 RequestMethod
21072108try {
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 ){
21092111// 如果不匹配,异常不处理即可
2110- RequestMethod _method = RequestMethod .valueOf (key .substring (1 ).toUpperCase ());
21112112removeTmpKeys .add (key );
21122113
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 ();
21152136
2116- for (String objKey : set ){
2137+ for (Entry <String , Object > objEntry : set ){
2138+ String objKey = objEntry == null ? null : objEntry .getKey ();
21172139if (objKey == null ){
21182140continue ;
21192141 }
21202142
21212143Map <String , Object > objAttrMap = new HashMap <>();
2122- objAttrMap .put (apijson .JSONObject .KEY_METHOD , _method );
2144+ objAttrMap .put (apijson .JSONObject .KEY_METHOD , keyMethod );
21232145keyObjectAttributesMap .put (objKey , objAttrMap );
2124- JSONObject objAttrJson = obj .getJSONObject (objKey );
2125- Set <Entry <String , Object >> objSet = objAttrJson == null ? new HashSet <>() : objAttrJson .entrySet ();
21262146
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 );
21312152 }
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 ();
21322159
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+ }
21442178 }
21452179 }
21462180 }
@@ -2189,15 +2223,17 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
21892223 }
21902224
21912225if (key .startsWith ("@" ) || key .endsWith ("@" )){
2192- jsonObject .put (key , obj );
2226+ correctRequest .put (key , obj );
21932227continue ;
21942228 }
21952229
21962230if (obj instanceof JSONObject || obj instanceof JSONArray ){
2197- RequestMethod _method = null ;
2231+ RequestMethod _method ;
21982232if (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 );
22012237if (combine != null && RequestMethod .isPublicMethod (_method ) == false ){
22022238throw new IllegalArgumentException (key + ":{} 里的 @combine:value 不合法!开放请求 GET、HEAD 才允许传 @combine:value !" );
22032239 }
@@ -2207,22 +2243,14 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
22072243if (attrMap == null ){
22082244if (method == RequestMethod .CRUD ){
22092245_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 );
22172249 } else {
22182250_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 );
22262254 }
22272255 } else {
22282256_method = (RequestMethod ) attrMap .get (apijson .JSONObject .KEY_METHOD );
@@ -2236,42 +2264,42 @@ protected JSONObject batchVerify(RequestMethod method, String tag, int version,
22362264
22372265// get请求不校验
22382266if (RequestMethod .isPublicMethod (_method )){
2239- jsonObject .put (key , obj );
2267+ correctRequest .put (key , obj );
22402268continue ;
22412269 }
22422270
2243- if (tag != null && !tag .contains (":" )){
2271+ if (tag != null && !tag .contains (":" )){
22442272JSONObject object = getRequestStructure (_method , tag , version );
22452273JSONObject ret = objectVerify (_method , tag , version , name , request , maxUpdateCount , creator , object );
2246- jsonObject .putAll (ret );
2274+ correctRequest .putAll (ret );
22472275break ;
22482276 }
22492277
22502278String _tag = buildTag (request , key , method , tag );
22512279JSONObject object = getRequestStructure (_method , _tag , version );
2252- if (method == RequestMethod .CRUD && StringUtil .isEmpty (tag , true )){
2280+ if (method == RequestMethod .CRUD && StringUtil .isEmpty (tag , true )){
22532281JSONObject requestItem = new JSONObject ();
22542282requestItem .put (key , obj );
22552283JSONObject ret = objectVerify (_method , _tag , version , name , requestItem , maxUpdateCount , creator , object );
2256- jsonObject .put (key , ret .get (key ));
2284+ correctRequest .put (key , ret .get (key ));
22572285 } else {
22582286return objectVerify (_method , _tag , version , name , request , maxUpdateCount , creator , object );
22592287 }
22602288 } else {
2261- jsonObject .put (key , obj );
2289+ correctRequest .put (key , obj );
22622290 }
22632291 } catch (Exception e ){
22642292e .printStackTrace ();
22652293throw new Exception (e );
22662294 }
22672295 }
22682296
2269- // 这里是requestObject ref request 的引用, 删除不需要的临时变量
2297+ // 这里是 requestObject ref request 的引用, 删除不需要的临时变量
22702298for (String removeKey : removeTmpKeys ){
22712299request .remove (removeKey );
22722300 }
22732301
2274- return jsonObject ;
2302+ return correctRequest ;
22752303 }
22762304
22772305public 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
22842312return defaultEnum ;
22852313 }
22862314 }
2287-
2315+
22882316protected void setRequestAttribute (String key , boolean isArray , String attrKey , @ NotNull JSONObject request ){
22892317Map <String , Object > attrMap = keyObjectAttributesMap .get (isArray ? key + apijson .JSONObject .KEY_ARRAY : key );
22902318Object attrVal = attrMap == null ? null : attrMap .get (attrKey );
@@ -2308,7 +2336,7 @@ protected String buildTag(JSONObject request, String key, RequestMethod method,
23082336 }
23092337return tag ;
23102338 }
2311-
2339+
23122340
23132341protected JSONObject objectVerify (RequestMethod method , String tag , int version , String name , @ NotNull JSONObject request
23142342 , int maxUpdateCount , SQLCreator creator , JSONObject object ) throws Exception {
@@ -2317,7 +2345,7 @@ protected JSONObject objectVerify(RequestMethod method, String tag, int version,
23172345// JSONObject clone 浅拷贝没用,Structure.parse 会导致 structure 里面被清空,第二次从缓存里取到的就是{}
23182346return getVerifier ().verifyRequest (method , name , target , request , maxUpdateCount , getGlobalDatabase (), getGlobalSchema (), creator );
23192347 }
2320-
2348+
23212349/***
23222350 * 兼容url crud, 获取真实method
23232351 * @param method = crud
0 commit comments