Skip to content

Commit 38567f2

Browse files
author
George
committed
[stdlib] Improve error messages when snake case conversion is used.
`convertFromSnakeCase` and `convertToSnakeCase` are not inverses of each other for keys like "pictureURL" (which roundtrips to "pictureUrl"). This does not fix the underlying issue, but improves the error message if this situation is detected.
1 parent 22294f2 commit 38567f2

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Darwin/Foundation-swiftoverlay/JSONEncoder.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,10 +1376,13 @@ fileprivate struct _JSONKeyedDecodingContainer<K : CodingKey> : KeyedDecodingCon
13761376
// In this case we can attempt to recover the original value by reversing the transform
13771377
let original = key.stringValue
13781378
let converted = JSONEncoder.KeyEncodingStrategy._convertToSnakeCase(original)
1379+
let roundtrip = JSONDecoder.KeyDecodingStrategy._convertFromSnakeCase(converted)
13791380
if converted == original {
13801381
return "\(key) (\"\(original)\")"
1381-
} else {
1382+
} else if roundtrip == original {
13821383
return "\(key) (\"\(original)\"), converted to \(converted)"
1384+
} else {
1385+
return "\(key) (\"\(original)\"), with divergent representation \(roundtrip), converted to \(converted)"
13831386
}
13841387
default:
13851388
// Otherwise, just report the converted string

0 commit comments

Comments
 (0)