Skip to content

fix(casing): stop transforming an index after its part is removed - #1113

Open
youdie006 wants to merge 1 commit into
danielgtaylor:mainfrom
youdie006:casing-join-remove-index
Open

fix(casing): stop transforming an index after its part is removed#1113
youdie006 wants to merge 1 commit into
danielgtaylor:mainfrom
youdie006:casing-join-remove-index

Conversation

@youdie006

Copy link
Copy Markdown

casing.Camel panics when a transform removes the first part.

drop := func(p string) string { if p == "and" { return "" }; return p }
casing.Camel("and-one-two", drop)
panic: runtime error: index out of range [-1]
github.com/danielgtaylor/huma/v2/casing.Join(...) casing/casing.go:214
github.com/danielgtaylor/huma/v2/casing.Camel(...) casing/casing.go:296

Same root cause, silently wrong value when the removed part is not first:

suffix := func(s string) string { return s + "x" }
casing.Join([]string{"one", "and", "two"}, "_", drop, suffix)
// "onexx_twox"   want "onex_twox"  -  "one" gets suffixed twice

Cause

casing/casing.go:212:

for i := 0; i < len(parts); i++ {
    for _, t := range transform {
        parts[i] = t(parts[i])

        if parts[i] == "" {
            // Transformer completely removed this part.
            parts = append(parts[:i], parts[i+1:]...)
            i--
        }
    }
}

i-- is the outer loop's correction, but it is applied while the inner transform loop is
still running. The next transform in the list then reads parts[i] at the decremented index — the
element before the one that was removed, which has already been transformed. With the removed part
first, i becomes -1.

Camel (casing/casing.go:295) always does append(transform, strings.Title), so every
Camel/LowerCamel call has at least two transforms and a single removing transform is enough to
reach it.

The change

break out of the inner loop after removing the part, so the outer i-- and i++ land on the
element that shifted into this position and it starts its transform chain from the beginning.

Why the tests missed it

TestRemovePart (casing/casing_test.go:82) exercises removal with exactly one transform, via
Snake. It still gives "one_two", unchanged by this PR. The next step — a second transform, or
Camel, which supplies one implicitly — is where it breaks.

Tests

TestRemovePartWithMultipleTransforms, covering both the panic and the double-transform value.
Mutation-checked in both directions:

  • Revert the fix (drop break): panic: runtime error: index out of range [-1]
  • Over-correct (keep break, drop i--): expected: "OneTwo" actual: "oneTwo" and
    expected: "onex_twox" — so the test pins the boundary from both sides rather than just one.

go test -race ./... passes across all 21 packages. gofmt -l casing/ empty, go vet ./casing/
clean.

Observable change: only on inputs that currently panic or double-transform. No existing test
changes result.

One more, not bundled

LowerCamel("") panics too, at casing/casing.go:300, which indexes runes[0] without a length
check — also reachable from any all-punctuation input, since Split("_") returns an empty slice. A
separate root cause, so I left it out to keep this PR to one; happy to fold it in or send it after.


Disclosure: found and prepared with AI assistance (Claude). Every figure above is from a run on this
branch, and the root cause is one I traced and verified by hand.

Join decrements the outer index when a transform empties a part, but the
inner transform loop kept running and then indexed the shifted slice one
place too low. With a leading part removed this indexes -1 and panics;
elsewhere it applies a later transform twice.
Copilot AI lite review requested due to automatic review settings September 4, 2026 03:22
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 4, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-04T03:25:00.799857Z 65c31a6 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The fix is minimal, directly addresses the documented root cause, and is backed by a focused regression test covering both the panic and incorrect-output scenarios.

Pull request overview

This PR fixes a panic and incorrect output in casing.Join/casing.Camel when a transform removes a part while multiple transforms are applied, by preventing subsequent transforms from running on a shifted index.

Changes:

  • Stop applying remaining transforms to an index after a part is removed (break out of the inner transform loop).
  • Add a regression test covering both the panic case (removed first part) and the “double-transform” incorrect output case (removed middle part).
File summaries
File Description
casing/casing.go Breaks out of the inner transform loop after removing a part to avoid transforming the wrong shifted element (and negative index).
casing/casing_test.go Adds a test that exercises part removal with multiple transforms to prevent panic and double-application regressions.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Sep 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.20%. Comparing base (ad801e0) to head (65c31a6).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1113   +/-   ##
=======================================
  Coverage   93.20%   93.20%           
=======================================
  Files          23       23           
  Lines        4988     4989    +1     
=======================================
+ Hits         4649     4650    +1     
  Misses        272      272           
  Partials       67       67           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants