48
48
*/
49
49
public abstract class AbstractParser <T extends Object > implements Parser <T >, ParserCreator <T >, VerifierCreator <T >, SQLCreator {
50
50
protected static final String TAG = "AbstractParser" ;
51
- protected Map <Object , RequestMethod > keyMethodMap = new HashMap <>();
51
+
52
+ /**
53
+ * json对象、数组对应的数据源、版本、角色、method等
54
+ */
55
+ protected Map <Object , Map <String , Object >> keyObjectAttributesMap = new HashMap <>();
52
56
/**
53
57
* 可以通过切换该变量来控制是否打印关键的接口请求内容。保守起见,该值默认为false。
54
58
* 与 {@link Log#DEBUG} 任何一个为 true 都会打印关键的接口请求内容。
@@ -1158,7 +1162,8 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
1158
1162
}
1159
1163
1160
1164
//不能允许GETS,否则会被通过"[]":{"@role":"ADMIN"},"Table":{},"tag":"Table"绕过权限并能批量查询
1161
- if (isSubquery == false && RequestMethod .isGetMethod (requestMethod , true ) == false ) {
1165
+ RequestMethod _method = request .get (apijson .JSONObject .KEY_METHOD ) == null ? requestMethod : RequestMethod .valueOf (request .getString (apijson .JSONObject .KEY_METHOD ));
1166
+ if (isSubquery == false && RequestMethod .isGetMethod (_method , true ) == false ) {
1162
1167
throw new UnsupportedOperationException ("key[]:{} 只支持 GET, GETS 方法!其它方法不允许传 " + name + ":{} 等这种 key[]:{} 格式!" );
1163
1168
}
1164
1169
if (request == null || request .isEmpty ()) { // jsonKey-jsonValue 条件
@@ -1913,7 +1918,7 @@ public JSONObject executeSQL(SQLConfig config, boolean isSubquery) throws Except
1913
1918
JSONObject res = getSQLExecutor ().execute (config , false );
1914
1919
1915
1920
//如果是查询方法,才能执行explain
1916
- if (RequestMethod .isQueryMethod (config .getMethod ())){
1921
+ if (RequestMethod .isQueryMethod (config .getMethod ()) && config . isElasticsearch () == false ){
1917
1922
config .setExplain (explain );
1918
1923
JSONObject explainResult = config .isMain () && config .getPosition () != 0 ? null : getSQLExecutor ().execute (config , false );
1919
1924
@@ -2083,6 +2088,7 @@ protected JSONObject getRequestStructure(RequestMethod method, String tag, int v
2083
2088
2084
2089
private JSONObject batchVerify (RequestMethod method , String tag , int version , String name , @ NotNull JSONObject request , int maxUpdateCount , SQLCreator creator ) throws Exception {
2085
2090
JSONObject jsonObject = new JSONObject (true );
2091
+ List <String > removeTmpKeys = new ArrayList <>(); // 请求json里面的临时变量,不需要带入后面的业务中,比如 @post、@get等
2086
2092
if (request .keySet () == null || request .keySet ().size () == 0 ) {
2087
2093
throw new IllegalArgumentException ("json对象格式不正确 !,例如 \" User\" : {}" );
2088
2094
}
@@ -2098,59 +2104,117 @@ private JSONObject batchVerify(RequestMethod method, String tag, int version, St
2098
2104
if (key .startsWith ("@" )) {
2099
2105
try {
2100
2106
// 如果不匹配,异常不处理即可
2101
- RequestMethod l_method = RequestMethod .valueOf (key .substring (1 ).toUpperCase ());
2102
- for (String objKey : StringUtil .split (request .getString (key ))) {
2103
- keyMethodMap .put (objKey , l_method );
2107
+ RequestMethod _method = RequestMethod .valueOf (key .substring (1 ).toUpperCase ());
2108
+ removeTmpKeys .add (key );
2109
+ for (String objKey : request .getJSONObject (key ).keySet ()) {
2110
+ Map <String , Object > object_attributes_map = new HashMap <>();
2111
+ object_attributes_map .put (apijson .JSONObject .KEY_METHOD , _method );
2112
+ keyObjectAttributesMap .put (objKey , object_attributes_map );
2113
+ JSONObject objAttrJson = request .getJSONObject (key ).getJSONObject (objKey );
2114
+ for (String objAttr : objAttrJson .keySet ()) {
2115
+ switch (objAttr ) {
2116
+ case apijson .JSONObject .KEY_DATASOURCE :
2117
+ object_attributes_map .put (apijson .JSONObject .KEY_DATASOURCE , objAttrJson .getString (objAttr ));
2118
+ break ;
2119
+ case apijson .JSONObject .KEY_SCHEMA :
2120
+ object_attributes_map .put (apijson .JSONObject .KEY_SCHEMA , objAttrJson .getString (objAttr ));
2121
+ break ;
2122
+ case apijson .JSONObject .KEY_DATABASE :
2123
+ object_attributes_map .put (apijson .JSONObject .KEY_DATABASE , objAttrJson .getString (objAttr ));
2124
+ break ;
2125
+ case apijson .JSONObject .VERSION :
2126
+ object_attributes_map .put (apijson .JSONObject .VERSION , objAttrJson .getString (objAttr ));
2127
+ break ;
2128
+ case apijson .JSONObject .KEY_ROLE :
2129
+ object_attributes_map .put (apijson .JSONObject .KEY_ROLE , objAttrJson .getString (objAttr ));
2130
+ break ;
2131
+ default :
2132
+ break ;
2133
+ }
2134
+ }
2104
2135
}
2136
+ continue ;
2105
2137
} catch (Exception e ) {
2106
2138
}
2107
2139
}
2108
-
2109
- //
2140
+
2110
2141
// 1、非crud,对于没有显式声明操作方法的,直接用 URL(/get, /post 等) 对应的默认操作方法
2111
2142
// 2、crud, 没有声明就用 GET
2112
2143
// 3、兼容 sql@ JSONObject,设置 GET方法
2113
2144
// 将method 设置到每个object, op执行会解析
2114
2145
if (request .get (key ) instanceof JSONObject ) {
2115
- if (keyMethodMap .get (key ) == null ) {
2146
+ if (keyObjectAttributesMap .get (key ) == null ) {
2116
2147
// 数组会解析为对象进行校验,做一下兼容
2117
- if (keyMethodMap .get (key + apijson .JSONObject .KEY_ARRAY ) == null ) {
2118
- if (method == RequestMethod .CRUD || ( key .endsWith ("@" ) && request . get ( key ) instanceof JSONObject )) {
2148
+ if (keyObjectAttributesMap .get (key + apijson .JSONObject .KEY_ARRAY ) == null ) {
2149
+ if (method == RequestMethod .CRUD || key .endsWith ("@" )) {
2119
2150
request .getJSONObject (key ).put (apijson .JSONObject .KEY_METHOD , GET );
2120
- keyMethodMap .put (key , GET );
2151
+ if (keyObjectAttributesMap .get (key ) == null ) {
2152
+ Map <String , Object > object_attributes_map = new HashMap <>();
2153
+ object_attributes_map .put (apijson .JSONObject .KEY_METHOD , GET );
2154
+ keyObjectAttributesMap .put (key , object_attributes_map );
2155
+ }else {
2156
+ keyObjectAttributesMap .get (key ).put (apijson .JSONObject .KEY_METHOD , GET );
2157
+ }
2121
2158
} else {
2122
2159
request .getJSONObject (key ).put (apijson .JSONObject .KEY_METHOD , method );
2123
- keyMethodMap .put (key , method );
2160
+ if (keyObjectAttributesMap .get (key ) == null ) {
2161
+ Map <String , Object > object_attributes_map = new HashMap <>();
2162
+ object_attributes_map .put (apijson .JSONObject .KEY_METHOD , method );
2163
+ keyObjectAttributesMap .put (key , object_attributes_map );
2164
+ }else {
2165
+ keyObjectAttributesMap .get (key ).put (apijson .JSONObject .KEY_METHOD , method );
2166
+ }
2124
2167
}
2125
2168
} else {
2126
- request .getJSONObject (key ).put (apijson .JSONObject .KEY_METHOD , keyMethodMap .get (key + apijson .JSONObject .KEY_ARRAY ));
2169
+ setRequestAttribute (key , true , apijson .JSONObject .KEY_METHOD , request );
2170
+ setRequestAttribute (key , true , apijson .JSONObject .KEY_DATASOURCE , request );
2171
+ setRequestAttribute (key , true , apijson .JSONObject .KEY_SCHEMA , request );
2172
+ setRequestAttribute (key , true , apijson .JSONObject .KEY_DATABASE , request );
2173
+ setRequestAttribute (key , true , apijson .JSONObject .VERSION , request );
2174
+ setRequestAttribute (key , true , apijson .JSONObject .KEY_ROLE , request );
2127
2175
}
2128
2176
} else {
2129
- request .getJSONObject (key ).put (apijson .JSONObject .KEY_METHOD , keyMethodMap .get (key ));
2177
+ setRequestAttribute (key , false , apijson .JSONObject .KEY_METHOD , request );
2178
+ setRequestAttribute (key , false , apijson .JSONObject .KEY_DATASOURCE , request );
2179
+ setRequestAttribute (key , false , apijson .JSONObject .KEY_SCHEMA , request );
2180
+ setRequestAttribute (key , false , apijson .JSONObject .KEY_DATABASE , request );
2181
+ setRequestAttribute (key , false , apijson .JSONObject .VERSION , request );
2182
+ setRequestAttribute (key , false , apijson .JSONObject .KEY_ROLE , request );
2130
2183
}
2131
2184
}
2132
-
2185
+
2133
2186
if (key .startsWith ("@" ) || key .endsWith ("@" )) {
2134
2187
jsonObject .put (key , request .get (key ));
2135
2188
continue ;
2136
2189
}
2137
2190
2138
-
2139
2191
if (request .get (key ) instanceof JSONObject || request .get (key ) instanceof JSONArray ) {
2140
2192
RequestMethod _method = null ;
2141
2193
if (request .get (key ) instanceof JSONObject ) {
2142
2194
_method = RequestMethod .valueOf (request .getJSONObject (key ).getString (apijson .JSONObject .KEY_METHOD ).toUpperCase ());
2143
2195
} else {
2144
- if (keyMethodMap .get (key ) == null ) {
2196
+ if (keyObjectAttributesMap .get (key ) == null ) {
2145
2197
if (method == RequestMethod .CRUD ) {
2146
2198
_method = GET ;
2147
- keyMethodMap .put (key , GET );
2199
+ if (keyObjectAttributesMap .get (key ) == null ) {
2200
+ Map <String , Object > object_attributes_map = new HashMap <>();
2201
+ object_attributes_map .put (apijson .JSONObject .KEY_METHOD , GET );
2202
+ keyObjectAttributesMap .put (key , object_attributes_map );
2203
+ }else {
2204
+ keyObjectAttributesMap .get (key ).put (apijson .JSONObject .KEY_METHOD , GET );
2205
+ }
2148
2206
} else {
2149
2207
_method = method ;
2150
- keyMethodMap .put (key , method );
2208
+ if (keyObjectAttributesMap .get (key ) == null ) {
2209
+ Map <String , Object > object_attributes_map = new HashMap <>();
2210
+ object_attributes_map .put (apijson .JSONObject .KEY_METHOD , method );
2211
+ keyObjectAttributesMap .put (key , object_attributes_map );
2212
+ }else {
2213
+ keyObjectAttributesMap .get (key ).put (apijson .JSONObject .KEY_METHOD , method );
2214
+ }
2151
2215
}
2152
2216
} else {
2153
- _method = keyMethodMap .get (key );
2217
+ _method = ( RequestMethod ) keyObjectAttributesMap .get (key ). get ( apijson . JSONObject . KEY_METHOD );
2154
2218
}
2155
2219
}
2156
2220
@@ -2179,10 +2243,26 @@ private JSONObject batchVerify(RequestMethod method, String tag, int version, St
2179
2243
throw new Exception (e );
2180
2244
}
2181
2245
}
2182
-
2246
+ // 这里是requestObject ref request 的引用, 删除不需要的临时变量
2247
+ for (String removeKey : removeTmpKeys ) {
2248
+ request .remove (removeKey );
2249
+ }
2183
2250
return jsonObject ;
2184
2251
}
2185
2252
2253
+ private void setRequestAttribute (String key , boolean isArray , String attrKey , @ NotNull JSONObject request ) {
2254
+ Object attrVal = null ;
2255
+ if (isArray ) {
2256
+ attrVal = keyObjectAttributesMap .get (key + apijson .JSONObject .KEY_ARRAY ).get (attrKey );
2257
+ }else {
2258
+ attrVal = keyObjectAttributesMap .get (key ).get (attrKey );
2259
+ }
2260
+
2261
+ if (attrVal != null && request .getJSONObject (key ).get (attrKey ) == null ) {
2262
+ // 如果对象内部已经包含该属性,不覆盖
2263
+ request .getJSONObject (key ).put (attrKey , attrVal );
2264
+ }
2265
+ }
2186
2266
/**
2187
2267
* { "xxx:aa":{ "@tag": "" }}
2188
2268
* 生成规则:
@@ -2231,8 +2311,8 @@ protected JSONObject objectVerify(RequestMethod method, String tag, int version,
2231
2311
* @return
2232
2312
*/
2233
2313
public RequestMethod getRealMethod (RequestMethod method , String key , Object value ) {
2234
- if (method == CRUD && (value instanceof JSONObject || value instanceof JSONArray )) {
2235
- return this .keyMethodMap .get (key );
2314
+ if (method == CRUD && key . startsWith ( "@" ) == false && (value instanceof JSONObject || value instanceof JSONArray )) {
2315
+ return ( RequestMethod ) this .keyObjectAttributesMap .get (key ). get ( apijson . JSONObject . KEY_METHOD );
2236
2316
}
2237
2317
return method ;
2238
2318
}
0 commit comments