Skip to content

Conversation

harvinderRathore
Copy link

Problem

#[serde(rename_all = "camelCase")] could panic when a field or
variant name begins with a non-ASCII character. The cause was byte
slicing ([..1]) on a UTF-8 string before lowercasing the first
character. Examples include: İstanbul (U+0130) and other multi-byte
scalars.

Fix

Replace byte-slicing with Unicode-aware lowering of the first Unicode
scalar
using char::to_lowercase(), then append the remainder. This
handles multi-scalar mappings (e.g., İ -> i + U+0307) and avoids
UTF-8 boundary panics. Updated both CamelCase arms in
apply_to_variant and apply_to_field.

Compatibility

ASCII behavior is unchanged. Only the first scalar is lowercased (same
camelCase strategy as before). The change is confined to derive-time
rename logic.

Tests

  • ASCII regressions for fields/variants remain unchanged.
  • Greek Σ -> σ.
  • Expanding lowercase for İstanbul -> i\u{307}stanbul.
  • Non-cased scripts (Chinese/Han and Hindi/Devanagari) remain stable.
  • Mixed Devanagari + ASCII snake_case -> परियोजनाName.

All crate and workspace tests pass locally.

Closes: #2953

Problem
`#[serde(rename_all = "camelCase")]` could panic when a field or
variant name begins with a non-ASCII character due to byte slicing
(`[..1]`) on UTF-8. Examples: `İstanbul` (U+0130).

Fix
Use Unicode-aware `char::to_lowercase()` on the first scalar, then
append the remainder. Updates both CamelCase arms.

Compatibility
ASCII behavior unchanged; only first scalar is lowercased.

Tests
ASCII regression; Σ->σ; `İstanbul` expansion; Chinese/Hindi stable;
mixed Devanagari+ASCII snake_case -> `परियोजनाName`.

Closes: serde-rs#2953

Signed-off-by: Harvinder Singh Rathor <harvinderrathor9@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Bug: #[serde(rename_all = "camelCase")] panics on struct fields with non-ASCII (e.g., CJK) identifiers

1 participant