Skip to content

Commit b890396

Browse files
committed
Java: MultiDataSource 中零代码断言更好地兼容各种 status 和 code 情况,兼容 type = null/'undefined' 等
1 parent 3854e77 commit b890396

3 files changed

Lines changed: 44 additions & 20 deletions

File tree

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/api/apijson/JSONResponse.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -530,18 +530,27 @@ var JSONResponse = {
530530
*/
531531
compareResponse: function(res, target, real, folder, isMachineLearning, codeName, exceptKeys, ignoreTrend, noBizCode) {
532532
target = target || {}
533-
var tStatus = target.status || 200;
534-
var rStatus = (res || {}).status;
535-
if (rStatus != null && rStatus != tStatus) {
533+
codeName = StringUtil.isEmpty(codeName, true) ? JSONResponse.KEY_CODE : codeName;
534+
535+
var tStatus = target.status; // || 200;
536+
if (tStatus == null && StringUtil.isNotEmpty(target)) { // target[codeName] != null) {
537+
tStatus = 200;
538+
}
539+
var rStatus = (res || {}).status || 200;
540+
// if (rStatus == null && real[codeName] != null) {
541+
// rStatus = 200;
542+
// }
543+
544+
if (tStatus != null && rStatus != tStatus) {
536545
return {
537546
code: JSONResponse.COMPARE_CODE_CHANGE,
538547
msg: 'HTTP Status Code 改变!' + tStatus + ' -> ' + rStatus,
539548
path: ''
540549
}
541550
}
542-
codeName = StringUtil.isEmpty(codeName, true) ? JSONResponse.KEY_CODE : codeName;
551+
543552
var tCode = (isMachineLearning != true && noBizCode) ? JSONResponse.CODE_SUCCESS : (target || {})[codeName];
544-
var rCode = noBizCode ? tCode : (real || {})[codeName] || (real == null || real == {} ? null : JSONResponse.CODE_SUCCESS);
553+
var rCode = noBizCode ? tCode : (real || {})[codeName]; // || (real == null || real == {} ? null : JSONResponse.CODE_SUCCESS);
545554

546555
//解决了弹窗提示机器学习更新标准异常,但导致所有项测试结果都变成状态码 code 改变
547556
// if (real == null) {
@@ -620,9 +629,9 @@ var JSONResponse = {
620629

621630
if (noBizCode != true) {
622631
delete target[codeName];
623-
delete real[codeName];
632+
real[codeName] = typeof rCode == 'undefined' ? undefined : null; // delete real[codeName];
624633
delete target.throw;
625-
delete real.throw;
634+
real.throw = typeof rCode == 'undefined' ? rThrw : null; // delete real.throw;
626635
}
627636

628637
//可能提示语变化,也要提示
@@ -873,6 +882,9 @@ var JSONResponse = {
873882

874883
var type = target.type;
875884
log('compareWithStandard type = target.type = ' + type + ' >>');
885+
if (StringUtil.isEmpty(type) || ['null', 'undefined'].indexOf(type) >= 0) {
886+
type = null;
887+
}
876888

877889
var valueLevel = target.valueLevel;
878890
log('compareWithStandard valueLevel = target.valueLevel = ' + valueLevel + ' >>');
@@ -923,7 +935,11 @@ var JSONResponse = {
923935
};
924936

925937
var realType = JSONResponse.getType(real);
926-
if (type != realType && type != 'undefined' && (type != 'number' || realType != 'integer')) { //类型改变
938+
if (StringUtil.isEmpty(realType) || ['null', 'undefined'].indexOf(realType) >= 0) {
939+
realType = null;
940+
}
941+
942+
if (type != realType && type != null && (type != 'number' || realType != 'integer')) { //类型改变
927943
log('compareWithStandard type != realType && type != undefined && (type != number || realType != integer) >> return COMPARE_TYPE_CHANGE');
928944

929945
max = {
@@ -2065,7 +2081,7 @@ var JSONResponse = {
20652081

20662082
//String 类型在 长度超过一定值 或 不是 常量名 时,改成 无限模型
20672083
//不用 type 判断类型,这样可以保证 lengthType 不会自动升级
2068-
if (isLength != true && typeof real == 'string' && (real.length > 20 || StringUtil.isConstName(real) != true)) {
2084+
if (isLength != true && typeof real == 'string' && (real.length > StringUtil.MAX_NAME_LENGTH || StringUtil.isConstName(real) != true)) {
20692085
if (level != 2) { //自定义模型不受影响
20702086
target[levelName] = 3;
20712087
}

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/api/apijson/StringUtil.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
var StringUtil = {
2020
TAG: 'StringUtil',
21+
MAX_NAME_LENGTH: 30,
22+
MAX_NICK_NAME_LENGTH: 20,
23+
MAX_CONST_NAME_LENGTH: 30,
2124

2225
/**获取string,为null则返回''
2326
* @param s
@@ -80,27 +83,31 @@ var StringUtil = {
8083
* @return
8184
*/
8285
isName: function(s) {
83-
return s != null && s.length > 0 && /[a-zA-Z_]/.test(s.substring(0, 1)) && /^[0-9a-zA-Z_]+$/.test(s);
86+
var l = StringUtil.length(s);
87+
return l >= 2 && l <= StringUtil.MAX_NAME_LENGTH && /[a-zA-Z_]/.test(s.substring(0, 1)) && /^[0-9a-zA-Z_]+$/.test(s);
8488
},
8589

8690
/**判断是否为代码名称,只能包含字母,数字或下划线
8791
* @param s
8892
* @return
8993
*/
9094
isBigName: function(s) {
91-
return s != null && s.length > 0 && /[A-Z]/.test(s.substring(0, 1)) && /^[0-9a-zA-Z_]+$/.test(s);
95+
var l = StringUtil.length(s);
96+
return l >= 2 && l <= StringUtil.MAX_NAME_LENGTH && /[A-Z]/.test(s.substring(0, 1)) && /^[0-9a-zA-Z_]+$/.test(s);
9297
},
9398

9499
/**判断是否为代码名称,只能包含字母,数字或下划线
95100
* @param s
96101
* @return
97102
*/
98103
isSmallName: function(s) {
99-
return s != null && s.length > 0 && /[a-z]/.test(s.substring(0, 1)) && /^[0-9a-zA-Z_]+$/.test(s);
104+
var l = StringUtil.length(s);
105+
return l >= 1 && l <= StringUtil.MAX_NAME_LENGTH && /[a-z]/.test(s.substring(0, 1)) && /^[0-9a-zA-Z_]+$/.test(s);
100106
},
101107

102108
isConstName: function(s) {
103-
return s != null && s.length > 0 && /[A-Z_]/.test(s.substring(0, 1)) && /^[0-9A-Z_]+$/.test(s);
109+
var l = StringUtil.length(s);
110+
return l >= 2 && l <= StringUtil.MAX_CONST_NAME_LENGTH && /[A-Z_]/.test(s.substring(0, 1)) && /^[0-9A-Z_]+$/.test(s);
104111
},
105112

106113
SYNTAX_NAMES: ['bool', 'boolean', 'int', 'integer', 'num', 'number', 'str', 'string', 'arr', 'array', 'obj', 'object'
@@ -728,7 +735,8 @@ var StringUtil = {
728735
return StringUtil.isKeyOfCategory(key, 'Latitude');
729736
},
730737
isNameKey: function (key) {
731-
return StringUtil.isKeyOfCategory(key, 'Name');
738+
var l = StringUtil.length(key);
739+
return l >= 2 && l <= 30 && StringUtil.isKeyOfCategory(key, 'Name');
732740
},
733741
isPathKey: function (key) {
734742
return StringUtil.isKeyOfCategory(key, 'Path');

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/resources/static/api/js/main.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3200,31 +3200,31 @@ https://github.com/Tencent/APIJSON/issues
32003200
log(e)
32013201
}
32023202

3203-
var code_ = inputObj.code
3203+
var code_ = inputObj[JSONResponse.KEY_CODE]
32043204
if (isEditResponse) {
3205-
inputObj.code = null // delete inputObj.code
3205+
inputObj[JSONResponse.KEY_CODE] = typeof code_ == 'undefined' ? undefined : null // delete inputObj.code
32063206
}
32073207

32083208
commentObj = JSONResponse.updateStandard(commentStddObj, inputObj);
32093209
CodeUtil.parseComment(after, docObj == null ? null : docObj['[]'], path, this.schema, this.database, this.language, isEditResponse != true, commentObj, true);
32103210

32113211
if (isEditResponse) {
3212-
inputObj.code = code_
3212+
inputObj[JSONResponse.KEY_CODE] = code_
32133213
}
32143214
}
32153215

32163216
var rawRspStr = JSON.stringify(currentResponse || {})
3217-
const code = currentResponse.code;
3217+
const code = currentResponse[JSONResponse.KEY_CODE];
32183218
const thrw = currentResponse.throw;
3219-
delete currentResponse.code; // currentResponse.code = null; //code必须一致
3219+
currentResponse[JSONResponse.KEY_CODE] = typeof code == 'undefined' ? undefined : null; // delete currentResponse.code; // currentResponse.code = null; //code必须一致
32203220
delete currentResponse.throw; // currentResponse.throw = null; // throw必须一致
32213221

32223222
const isML = this.isMLEnabled;
32233223
const stddObj = isML ? JSONResponse.updateStandard({}, currentResponse) : {};
32243224
stddObj.status = (this.currentHttpResponse || {}).status || 200;
32253225
stddObj.code = code || 0;
32263226
stddObj.throw = thrw;
3227-
currentResponse.code = code;
3227+
currentResponse[JSONResponse.KEY_CODE] = code;
32283228
currentResponse.throw = thrw;
32293229

32303230
var config = vRandom.value;

0 commit comments

Comments
 (0)