Fix JSONEncoder
key conversion for CodingKeyRepresentable
dictionary keys
#1526
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes an issue where
JSONEncoder
incorrectly applied key encoding strategies (like.convertToSnakeCase
) to dictionary keys that implementCodingKeyRepresentable
, when these keys should preserve their semantic meaning and remain unchanged.Problem
When encoding dictionaries with
CodingKeyRepresentable
keys (e.g., custom enum or struct keys),JSONEncoder
was treating them as regular coding key values and applying key conversion strategies. This caused keys like "leaveMeAlone
" to be transformed to "leave_me_alone
" even when the key itself represents a semantic identifier that should remain unchanged.Solution
_JSONStringDictionaryEncodableMarker
protocol to cover anyCodingKeyRepresentable
-keyed dictionaries, not justString
-keyed ones._JSONCodingKeyRepresentableDictionaryEncodableMarker
to reflect its broader scope.CodingKeyRepresentable
keys directly to their string representation without applying encoding transformations.Test Plan
Added test case
encodingDictionaryCodingKeyRepresentableKeyConversionUntouched()
that verifies:CodingKeyRepresentable
keys preserves original key names..convertToSnakeCase
are not applied to the dictionary keys.CodingKeyRepresentable
.Checklist
This change ensures that dictionaries with semantic keys maintain their intended structure while still allowing key
encoding strategies to work correctly for object properties during regular encoding.