JSONDecoder convertFromSnakeCase keyDecodingStrategy documentation is incorrect

Originator:wm.hass
Number:rdar://FB7673625 Date Originated:April 23rd, 2020
Status:Open Resolved:
Product:Foundation documentation Product Version:
Classification: Reproducible:Yes
 
The Foundation’s type `JSONDecoder` has a property that allow defining a decoding strategy. The documentation description of what `.convertFromSnakeCase` does is incorrect. It says:

/// Converting from snake case to camel case:
/// 1. Capitalizes the word starting after each `_`
/// 2. Removes all `_`
/// 3. Preserves starting and ending `_` (as these are often used to indicate private variables or other metadata).

What is wrong is that it actually makes every character lowercased before performing the operations described in the documentations.

For example, when using convertFromSnakeCase decoding strategy, a JSON key named `some_name_ICreated` will be translated to `someNameIcreated` (according to the documentation it should be `someNameICreated`)


Please list the steps you took to reproduce the issue:
mport Foundation

let jsonData = "{\"some_name_ICreated\": \"hello\"}".data(using: .utf8)!
struct MyEntity: Codable {
    
    let someProp: String
    
    enum CodingKeys: String, CodingKey {
        case someProp = "someNameIcreated"
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        print(container.allKeys)
        someProp = try container.decode(String.self, forKey: .someProp)
    }
    
}

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
do {
    let decoded = try decoder.decode(MyEntity.self, from: jsonData)
    print("Did decode: \(decoded.someProp)")
} catch let e {
    print("Failed: \(e.localizedDescription)")
}

What did you expect to happen?
I think there are two ways of fixing it:
- Update the documentation to reflect what the code does,
OR
- Update the code to make what the documentation says it does

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!