Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic type check for Item value #4542

Merged
merged 42 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
d05a3ef
增加配置项类型判断,后端以及sql修改。
furaul Aug 31, 2022
2493e02
apollo配置项增加类型校验
Bobji Aug 31, 2022
eda2cbd
update CHANGES.md
Bobji Aug 31, 2022
ada1e94
Merge branch 'master' into master
furaul Aug 31, 2022
2fa847f
Merge remote-tracking branch 'origin/master'
Bobji Aug 31, 2022
c34ac0d
Update apollo-portal/src/main/resources/static/i18n/zh-CN.json
furaul Aug 31, 2022
50d24d8
add ddl sql to delta file
Bobji Sep 2, 2022
35731fd
Merge remote-tracking branch 'origin/master'
Bobji Sep 2, 2022
d185ab2
Merge branch 'master' into master
furaul Sep 2, 2022
07da8c8
add ddl sql to delta file ( in a new foler v200-v210)
Bobji Sep 4, 2022
8037240
Merge branch 'master' of https://github.com/furaul/apollo
Bobji Sep 4, 2022
5392e04
Merge branch 'master' into master
nobodyiam Sep 5, 2022
81ece95
Update scripts/sql/delta/v200-v210/apolloconfigdb-v200-v210.sql
furaul Sep 6, 2022
4c02770
Update scripts/sql/apolloconfigdb.sql
furaul Sep 6, 2022
5f9a0d6
Merge branch 'master' into master
furaul Sep 10, 2022
0a1f823
Merge branch 'master' into master
nobodyiam Sep 11, 2022
26270b9
[openapi] add item type
furaul Sep 12, 2022
6b983a0
Optimize the code after review.
Bobji Sep 12, 2022
3687796
[openapi] add type check.
furaul Sep 12, 2022
95a8bb4
Fix unit tests.
furaul Sep 12, 2022
013ad40
Merge branch 'master' of https://github.com/furaul/apollo
furaul Sep 12, 2022
8d3d9f1
Merge branch 'master' into master
furaul Sep 12, 2022
94b2a83
Merge branch 'master' into master
nobodyiam Sep 12, 2022
ebff5a3
Update apollo-portal/src/main/resources/static/scripts/directive/item…
furaul Sep 12, 2022
84915d4
Update apollo-portal/src/main/resources/static/scripts/directive/item…
furaul Sep 12, 2022
287c0a0
Update apollo-portal/src/main/resources/static/scripts/directive/item…
furaul Sep 12, 2022
bfbf475
Update apollo-portal/src/main/resources/static/scripts/directive/item…
furaul Sep 12, 2022
595c689
Update apollo-portal/src/main/resources/static/scripts/directive/item…
furaul Sep 12, 2022
6bb23c2
Fix adminservice unit tests.
furaul Sep 12, 2022
9d7fc02
Revert "Optimize the code after review."
furaul Sep 12, 2022
2e28067
Re optimize the code after review.
furaul Sep 12, 2022
d1353d6
1. use english comments.
furaul Sep 12, 2022
74fe467
move openapi check to service side.
furaul Sep 12, 2022
45d52fb
move openapi check to service side.
furaul Sep 12, 2022
8f83d27
move type check to com.ctrip.framework.apollo.biz.service.ItemService.
furaul Sep 12, 2022
42e4856
add type in gray and related namespace pages.
furaul Sep 12, 2022
e2a0928
correct comments and update com.ctrip.framework.apollo.biz.service.It…
furaul Sep 12, 2022
92c559a
Fix 'Type missing problem' while click 'Full Release' in Grayscale Ve…
furaul Sep 17, 2022
7f3967b
correct comments
furaul Sep 17, 2022
5ece455
Merge branch 'master' into master
nobodyiam Sep 18, 2022
7f774e4
Add unit tests for ItemService.
furaul Sep 18, 2022
e129c00
Merge branch 'master' of https://github.com/furaul/apollo
furaul Sep 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Apollo 2.1.0
* [fix the deleted at timestamp issue](https://github.com/apolloconfig/apollo/pull/4493)
* [add configuration processor for portal developers](https://github.com/apolloconfig/apollo/pull/4521)
* [Add a potential json value check feature](https://github.com/apolloconfig/apollo/pull/4519)
* [Add basic type check for Item value](https://github.com/apolloconfig/apollo/pull/4542)

------------------
All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/11?closed=1)
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ public ItemDTO update(@PathVariable("appId") String appId,

Item beforeUpdateItem = BeanUtils.transform(Item.class, managedEntity);

//protect. only value,comment,lastModifiedBy can be modified
//protect. only value,type,comment,lastModifiedBy can be modified
managedEntity.setType(entity.getType());
furaul marked this conversation as resolved.
Show resolved Hide resolved
managedEntity.setValue(entity.getValue());
managedEntity.setComment(entity.getComment());
managedEntity.setDataChangeLastModifiedBy(entity.getDataChangeLastModifiedBy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class Item extends BaseEntity {
@Column(name = "key", nullable = false)
private String key;

@Column(name = "type")
private int type;

@Column(name = "value")
@Lob
private String value;
Expand Down Expand Up @@ -88,8 +91,17 @@ public void setLineNum(Integer lineNum) {
this.lineNum = lineNum;
}

public int getType() {
return type;
}

public void setType(int type) {
this.type = type;
}

public String toString() {
return toStringHelper().add("namespaceId", namespaceId).add("key", key).add("value", value)
.add("lineNum", lineNum).add("comment", comment).toString();
return toStringHelper().add("namespaceId", namespaceId).add("key", key)
.add("type", type).add("value", value)
.add("lineNum", lineNum).add("comment", comment).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class ItemDTO extends BaseDTO{

private String key;

private int type;

private String value;

private String comment;
Expand Down Expand Up @@ -90,4 +92,12 @@ public void setLineNum(int lineNum) {
this.lineNum = lineNum;
}

public int getType() {
return type;
}

public void setType(int type) {
this.type = type;
}

}
21 changes: 18 additions & 3 deletions apollo-portal/src/main/resources/static/i18n/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,24 @@
"Component.ConfigItem.ModifyItem": "修改配置项",
"Component.ConfigItem.ItemKey": "Key",
"Component.ConfigItem.ItemValue": "Value",
"Component.ConfigItem.ItemValueTips": "注意: 隐藏字符(空格、换行符、制表符Tab容易导致配置出错,如果需要检测 Value 中隐藏字符,请点击",
"Component.ConfigItem.ItemValueShowDetection": "检测隐藏字符",
"Component.ConfigItem.ItemValueNotHiddenChars": "无隐藏字符",
"Component.ConfigItem.ItemValueTips": "注意: 特殊字符(空格、换行符、制表符Tab、中文逗号)容易导致配置出错,如果需要检测 Value 中特殊字符,请点击",
furaul marked this conversation as resolved.
Show resolved Hide resolved
"Component.ConfigItem.ItemValueShowDetection": "检测特殊字符",
"Component.ConfigItem.ItemValueNotHiddenChars": "无特殊字符",
"Component.ConfigItem.ItemJSONValueTips": "注意: 检测JSON字符串格式是否正确,请点击",
"Component.ConfigItem.ItemJSONValueDetection": "检测JSON格式",
"Component.ConfigItem.ValidItemJSONValue": "JSON格式正确",
"Component.ConfigItem.InvalidItemJSONValue": "JSON格式错误",
"Component.ConfigItem.ItemComment": "Comment",
"Component.ConfigItem.ChooseCluster": "选择集群",
"Component.ConfigItem.ItemTypeName": "类型",
"Component.ConfigItem.ItemTypeString": "String",
"Component.ConfigItem.ItemTypeNumber": "Number",
"Component.ConfigItem.ItemTypeBoolean": "Boolean",
"Component.ConfigItem.ItemTypeJson": "Json",
furaul marked this conversation as resolved.
Show resolved Hide resolved
"Component.ConfigItem.ItemNumberError": "非法 Number",
"Component.ConfigItem.ItemJsonError": "JSON 格式不正确",
"Component.ConfigItem.ItemTypeTrue": "true",
"Component.ConfigItem.ItemTypeFalse": "false",
"Component.MergePublish.Title": "全量发布",
"Component.MergePublish.Tips": "全量发布将会把灰度版本的配置合并到主分支,并发布。",
"Component.MergePublish.NextStep": "全量发布后,您希望",
Expand Down Expand Up @@ -124,6 +133,7 @@
"Component.Namespace.MasterBranch.Body.PublishState": "发布状态",
"Component.Namespace.MasterBranch.Body.ItemKey": "Key",
"Component.Namespace.MasterBranch.Body.ItemValue": "Value",
"Component.Namespace.MasterBranch.Body.ItemType": "类型",
furaul marked this conversation as resolved.
Show resolved Hide resolved
"Component.Namespace.MasterBranch.Body.ItemComment": "备注",
"Component.Namespace.MasterBranch.Body.ItemLastModify": "最后修改人",
"Component.Namespace.MasterBranch.Body.ItemLastModifyTime": "最后修改时间",
Expand All @@ -132,6 +142,10 @@
"Component.Namespace.MasterBranch.Body.ItemNoPublish": "未发布",
"Component.Namespace.MasterBranch.Body.ItemEffective": "已生效的配置",
"Component.Namespace.MasterBranch.Body.ItemPublished": "已发布",
"Component.Namespace.MasterBranch.Body.ItemTypeString": "String",
"Component.Namespace.MasterBranch.Body.ItemTypeNumber": "Number",
"Component.Namespace.MasterBranch.Body.ItemTypeBoolean": "Boolean",
"Component.Namespace.MasterBranch.Body.ItemTypeJson": "Json",
furaul marked this conversation as resolved.
Show resolved Hide resolved
"Component.Namespace.MasterBranch.Body.AddedItem": "新增的配置",
"Component.Namespace.MasterBranch.Body.ModifyItem": "修改此灰度配置",
"Component.Namespace.Branch.GrayScaleRule.NoPermissionTips": "您没有权限编辑灰度规则, 具有 Namespace 修改权或者发布权的人员才可以编辑灰度规则. 如需要编辑灰度规则,请找应用管理员申请权限.",
Expand Down Expand Up @@ -813,6 +827,7 @@
"ItemModal.Tabs": "制表符",
"ItemModal.NewLine": "换行符",
"ItemModal.Space": "空格",
"ItemModal.ChineseComma": "中文逗号",
"ApolloNsPanel.LoadingHistoryError": "加载修改历史记录出错",
"ApolloNsPanel.LoadingGrayscaleError": "加载修改历史记录出错",
"ApolloNsPanel.Deleted": "删除成功",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ function controller($rootScope, $scope, $translate, toastr, AppUtil, EventManage
}

$scope.item = _.clone(toEditItem);

$scope.item.type = String($scope.item.type || 0)
if (namespace.isBranch || namespace.isLinkedNamespace) {
var existedItem = false;
namespace.items.forEach(function (item) {
Expand Down Expand Up @@ -283,6 +283,9 @@ function controller($rootScope, $scope, $translate, toastr, AppUtil, EventManage
$scope.item = {
tableViewOperType: 'create'
};
$scope.item.type = '0';
$scope.showNumberError = false;
$scope.showJsonError = false;

$scope.toOperationNamespace = namespace;
AppUtil.showModal('#itemModal');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ function itemModalDirective($translate, toastr, $sce, AppUtil, EventManager, Con
scope.showHiddenChars = showHiddenChars;
scope.detectJSON = detectJSON;
scope.check = check;
scope.changeType = changeType;
scope.verifyLoseFocus = verifyLoseFocus;

$('#itemModal').on('show.bs.modal', function (e) {
scope.showHiddenCharsContext = false;
Expand All @@ -57,12 +59,49 @@ function itemModalDirective($translate, toastr, $sce, AppUtil, EventManager, Con

$("#valueEditor").textareafullscreen();

function verifyLoseFocus() {
furaul marked this conversation as resolved.
Show resolved Hide resolved
if (scope.item.type == '1') {
furaul marked this conversation as resolved.
Show resolved Hide resolved
//校验类型为Number的时候输入框的值是否满足
furaul marked this conversation as resolved.
Show resolved Hide resolved
let regNumber = /-[0-9]+(\\.[0-9]+)?|[0-9]+(\\.[0-9]+)?/
furaul marked this conversation as resolved.
Show resolved Hide resolved
if (regNumber.test(Number(scope.item.value)) == true) {
scope.showNumberError = false
} else {
scope.showNumberError = true
}
} else if (scope.item.type == '3') {
furaul marked this conversation as resolved.
Show resolved Hide resolved
checkJson(scope.item.value)
} else {
scope.showNumberError = false
scope.showJsonError = false
}
}
// 数字 null 字符串 true false 是合法json 这里暂时排除掉这些内容 为了规范格式
function checkJson(str) {
if (str.trim() == 'null') {
scope.showJsonError = true
} else {
try {
if (!(typeof JSON.parse(str) == 'object')) {
scope.showJsonError = true
} else {
scope.showJsonError = false
}
} catch (error) {
scope.showJsonError = true
}
}
}

function doItem() {

if (!scope.item.value) {
scope.item.value = "";
}

if (scope.item.type) {
scope.item.type = Number(scope.item.type)
}

if (scope.item.tableViewOperType == TABLE_VIEW_OPER_TYPE.CREATE) {

//check key unique
Expand Down Expand Up @@ -163,6 +202,24 @@ function itemModalDirective($translate, toastr, $sce, AppUtil, EventManager, Con
selectedClusters = data;
}

function changeType() {
scope.showNumberError = false
scope.showJsonError = false
if (scope.item.type == '2') {
scope.item.lastValue = scope.item.value
scope.item.value = 'false'
} else {
if (scope.item.lastType =='2') {
scope.item.value = scope.item.lastValue
} else {
// String Number Json互相切换不做更改
furaul marked this conversation as resolved.
Show resolved Hide resolved
}
}

scope.item.lastType = scope.item.type
verifyLoseFocus()
}

function check(){
var value = scope.item.value;
if (!value || value.length < 2) {
Expand Down Expand Up @@ -220,7 +277,7 @@ function itemModalDirective($translate, toastr, $sce, AppUtil, EventManager, Con
}

function isHiddenChar(c) {
return c == '\t' || c == '\n' || c == ' ';
return c == '\t' || c == '\n' || c == ' ' || c == ',';
}

function viewHiddenChar(c) {
Expand All @@ -231,6 +288,8 @@ function itemModalDirective($translate, toastr, $sce, AppUtil, EventManager, Con
return '<mark>#' + $translate.instant('ItemModal.NewLine') + '#</mark>';
} else if (c == ' ') {
return '<mark>#' + $translate.instant('ItemModal.Space') + '#</mark>';
} else if (c == ',') {
return '<mark>#' + $translate.instant('ItemModal.ChineseComma') + '#</mark>';
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,34 @@ <h4 class="modal-title">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">{{'Component.ConfigItem.ItemValue' | translate }}</label>
<label class="col-sm-2 control-label">
<apollorequiredfield></apollorequiredfield>
{{'Component.ConfigItem.ItemTypeName' | translate }}
</label>
<div class="col-sm-10" valdr-form-group>
<select class="form-control" name="type" ng-model="item.type" ng-change="changeType()">
<option value="0">{{'Component.ConfigItem.ItemTypeString' | translate }}</option>
<option value="1">{{'Component.ConfigItem.ItemTypeNumber' | translate }}</option>
<option value="2">{{'Component.ConfigItem.ItemTypeBoolean' | translate }}</option>
<option value="3">{{'Component.ConfigItem.ItemTypeJson' | translate }}</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">{{'Component.ConfigItem.ItemValue' | translate }}</label>
<div class="col-sm-10" valdr-form-group ng-show="item.type != '2'">
<textarea id="valueEditor" name="value" class="form-control" rows="6" tabindex="2"
ng-required="false" ng-model="item.value" ng-change="check()">
ng-required="false" ng-model="item.value" ng-change="check()" ng-blur="verifyLoseFocus()">
</textarea>
{{'Component.ConfigItem.ItemValueTips' | translate }} <a
ng-click="showHiddenChars()">{{'Component.ConfigItem.ItemValueShowDetection' | translate }}</a>
<br>
<div ng-show="showNumberError" ng-model="showNumberError" style="color:red">
{{'Component.ConfigItem.ItemNumberError' | translate }}
</div>
<div ng-show="showJsonError" ng-model="showJsonError" style="color:red">
{{'Component.ConfigItem.ItemJsonError' | translate }}
</div>
<div ng-show="showCheckJSONHint">
{{'Component.ConfigItem.ItemJSONValueTips' | translate }} <a
ng-click="detectJSON()">{{'Component.ConfigItem.ItemJSONValueDetection' | translate }}</a>
Expand All @@ -63,6 +83,12 @@ <h4 class="modal-title">
<div class="bg-info" ng-bind-html="jsonDetectResult"
ng-show="showJSONDetectContext"></div>
</div>
<div class="col-sm-10" valdr-form-group ng-show="item.type == '2'">
<select class="form-control" name="value" ng-model="item.value">
<option value="true">{{'Component.ConfigItem.ItemTypeTrue' | translate }}</option>
<option value="false">{{'Component.ConfigItem.ItemTypeFalse' | translate }}</option>
</select>
</div>


</div>
Expand Down Expand Up @@ -92,7 +118,7 @@ <h4 class="modal-title">
{{'Common.Cancel' | translate }}
</button>
<button type="submit" class="btn btn-primary"
ng-disabled="itemForm.$invalid || (item.addItemBtnDisabled && item.tableViewOperType == 'create')">
ng-disabled="itemForm.$invalid || (item.addItemBtnDisabled && item.tableViewOperType == 'create') || showNumberError || showJsonError ">
{{'Common.Submit' | translate }}
</button>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@
ng-click="col='item.key';desc=!desc;">
{{'Component.Namespace.Master.Items.Body.ItemKey' | translate }}&nbsp;
<span class="glyphicon glyphicon-sort"></span>

</th>
<th>
{{'Component.Namespace.MasterBranch.Body.ItemType' | translate }}
</th>
<th>
{{'Component.Namespace.Master.Items.Body.ItemValue' | translate }}
Expand Down Expand Up @@ -350,13 +352,23 @@
data-placement="bottom"
title="{{'Component.Namespace.Master.Items.Body.DeletedTips' | translate }}">{{'Component.Namespace.Master.Items.Body.Deleted' | translate }}</span>
</td>
<td width="30%" class="cursor-pointer"
<td width="7%" class="text-center">
<span ng-if="config.item.type == '0'"
>{{'Component.Namespace.MasterBranch.Body.ItemTypeString' | translate }}</span>
<span ng-if="config.item.type == '1'"
>{{'Component.Namespace.MasterBranch.Body.ItemTypeNumber' | translate }}</span>
<span ng-if="config.item.type == '2'"
>{{'Component.Namespace.MasterBranch.Body.ItemTypeBoolean' | translate }}</span>
<span ng-if="config.item.type == '3'"
>{{'Component.Namespace.MasterBranch.Body.ItemTypeJson' | translate }}</span>
</td>
<td width="25%" class="cursor-pointer"
title="{{'Component.Namespace.Master.Items.Body.ClickToSee' | translate }}"
ng-click="showText(config.item.value)">
<span ng-bind="config.item.value | limitTo: 250"></span>
<span ng-bind="config.item.value.length > 250 ? '...': ''"></span>
</td>
<td width="13%" title="{{config.item.comment}}">
<td width="11%" title="{{config.item.comment}}">
<span ng-bind="config.item.comment | limitTo: 250"></span>
<span ng-bind="config.item.comment.length > 250 ?'...' : ''"></span>
</td>
Expand Down
1 change: 1 addition & 0 deletions scripts/sql/apolloconfigdb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ CREATE TABLE `Item` (
`Id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增Id',
`NamespaceId` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '集群NamespaceId',
`Key` varchar(128) NOT NULL DEFAULT 'default' COMMENT '配置项Key',
`Type` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '配置项类型,0: String,1: Number,2: Boolean,3: Json',
furaul marked this conversation as resolved.
Show resolved Hide resolved
`Value` longtext NOT NULL COMMENT '配置项值',
`Comment` varchar(1024) DEFAULT '' COMMENT '注释',
`LineNum` int(10) unsigned DEFAULT '0' COMMENT '行号',
Expand Down
1 change: 1 addition & 0 deletions scripts/sql/delta/v190-v200/apolloconfigdb-v190-v200.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ ALTER TABLE `GrayReleaseRule`

ALTER TABLE `Item`
ADD COLUMN `DeletedAt` BIGINT(20) NOT NULL DEFAULT '0' COMMENT 'Delete timestamp based on milliseconds' AFTER `IsDeleted`,
ADD COLUMN `Type` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '配置项类型,0: String,1: Number,2: Boolean,3: Json' AFTER `Key`,
furaul marked this conversation as resolved.
Show resolved Hide resolved
ADD INDEX IX_key (`Key`);

ALTER TABLE `Namespace`
Expand Down