Skip to content

feat: sanitize OpenAPI tag names for Commander.js - #28

Merged
lucianfialho merged 1 commit into
mainfrom
feat/sanitize-tag-names
May 26, 2026
Merged

lucianfialho merged 1 commit into
mainfrom
feat/sanitize-tag-names

Conversation

@lucianfialho

Copy link
Copy Markdown
Owner

Problem

Specs whose tags contain slashes, spaces, apostrophes or other special characters break command registration with errors like:

```
Error: cannot add command 'Actors/Actor' as already have command 'Actors/Actor'
```

Real-world examples: Apify ships tags like `Actors/Actor builds`, `Actors/Actor versions`, `Last Actor run's abort`, `Last Actor task run's default key-value store`.

Fix

  • New `cli/sanitize.ts` module with `sanitizeCommandName` (lowercase + collapse non-alphanumeric to hyphens) and `uniqueName` (numeric suffix for collisions)
  • Applied in both `commander-builder.ts` (used by templates) and `dynamic-commands.ts` (used by `--spec` direct flow)
  • Tests for `commander-builder` split into focused files to satisfy the 150-line gate

Breaking change

Command groups are now always lowercase. `tocli petstore Pets list` becomes `tocli petstore pets list`. Matches the existing `simplifyName` behavior for operations and standard CLI convention.

Suggested release: 0.8.0 (minor bump because of the breaking change).

Test plan

  • 123/123 tests pass (including the JSON parsing tests now in their own file)
  • Apify spec (39 tags, multiple hierarchical) loads end-to-end and runs `actors acts_get --dry-run` correctly

🤖 Generated with Claude Code

Tags with slashes, spaces, apostrophes or other special characters
(e.g. Apify's 'Actors/Actor builds', \"Last Actor run's abort\") broke
command registration because Commander.js interprets / as path
separator and rejects spaces/quotes in command names.

Now tag names are normalized via sanitizeCommandName() to
lowercase-hyphenated form, and collisions get numeric suffixes
(-2, -3, ...). Applied in both static and dynamic command builders.

Split commander-builder.test.ts into per-concern files to stay
within the 150-line gate.

BREAKING: command group names are now always lowercase. Previously
'Pets' worked as a literal command; now it's 'pets'. This matches
standard CLI convention and the existing simplifyName output.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@lucianfialho
lucianfialho merged commit ced5bbd into main May 26, 2026
1 check passed
@lucianfialho
lucianfialho deleted the feat/sanitize-tag-names branch May 26, 2026 01:54
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.

1 participant