Skip to content

Commit

Permalink
Merge pull request #103 from IMBlues/development
Browse files Browse the repository at this point in the history
fix: skip when certain key missing in extras of legacy data
  • Loading branch information
IMBlues authored Oct 14, 2021
2 parents be1de4e + b1ca09d commit 114fd07
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/api/bkuser_core/profiles/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def validate_extras_value_unique(value: dict, category_id: int, profile_id: int
for s in queryset.only("pk", "extras").extra(
where=["JSON_SEARCH(extras, 'one', %s) is not null"], params=[target_value]
):
# 防御: 可能存在部分旧数据并未添加所有 extra key
if f.name not in s.extras:
continue

if s.extras[f.name] == target_value:
raise ValidationError(
_("自定义字段 {} 需要保证唯一,而目录<id:{}>中已经存在值为 {} 的记录").format(f.display_name, category_id, target_value)
Expand Down
19 changes: 19 additions & 0 deletions src/api/bkuser_core/tests/profiles/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,22 @@ def test_black_extra_name(self, black_names, target):
order=100,
type=DynamicFieldTypeEnum.STRING.value,
)

@pytest.mark.parametrize(
"field_names,force_extras",
[
(["xxxx", "yyyy"], [{"yyyy": "abcd"}, {"xxxx": "abcd"}]),
],
)
def test_duplicate_other_missing_key(self, field_names, force_extras):
for field_name in field_names:
DynamicFieldInfo.objects.create(
name=field_name,
display_name=f"Dis_{field_name}",
unique=True,
order=100,
type=DynamicFieldTypeEnum.STRING.value,
)

for i, e in enumerate(force_extras):
make_simple_profile(username=f"fake{i}", force_create_params={"extras": e})

0 comments on commit 114fd07

Please # to comment.