alibaba / alibaba/HandyJSON

包含didSet/willSet属性同时指定映射,无法获取正确值。

Open
#364 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
4.3k
Forks
677
PR merge metrics
No merged PRs in 30d

Description

Swift 5.1
Xcode 11.3
HandyJSON: 5.0.1

我的使用场景是序列化时可以不触发didSet/willSet方法。手动设置该属性时需要触发didSet/willSet方法。

```
class Test: HandyJSON{
required init() { }
var xyzList = [String](){
didSet{
//do some thing
}
}
func mapping(mapper: HelpingMapper) {
do{
let pointer = withUnsafePointer(to: &self.xyzList, { return $0 })
let key = Int(bitPattern: pointer)
print("xyzList key: \(key)")
}
mapper <<< self.xyzList <-- "yyyList"
}
}
```

原因是:xyzList放入mapper.mappingHandlers的key 和 getRawValueFrom方法中 let address = Int(bitPattern: property.address) 的地址不一致。 经验证mapper.mappingHandlers中的key 是错误的。

导致 下面方法中if方法体不能被执行,此时从dict使用key位xyzList,而不是 yyyList,导致取不到。

```
fileprivate func getRawValueFrom(dict: [String: Any], property: PropertyInfo, mapper: HelpingMapper) -> Any? {
let address = Int(bitPattern: property.address)
if let mappingHandler = mapper.getMappingHandler(key: address) {
if let mappingPaths = mappingHandler.mappingPaths, mappingPaths.count > 0 {
for mappingPath in mappingPaths {
if let _value = dict.findValueBy(path: mappingPath) {
return _value
}
}
return nil
}
}
if HandyJSONConfiguration.deserializeOptions.contains(.caseInsensitive) {
return dict[property.key.lowercased()]
}
return dict[property.key]
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the provided Test case with Swift 5.1 and HandyJSON 5.0.1, then inspect getRawValueFrom and the mapper's mappingHandlers keys, comparing the property address with the registered mapping key. Done means a mapped xyzList property resolves the yyyList input during deserialization while manual assignments still trigger didSet/willSet.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.