Skip to content

fix(case-style): handle empty words from leading or repeated separators - #359

Open
KuljeetSinghXDA wants to merge 1 commit into
schultek:mainfrom
KuljeetSinghXDA:fix/case-style-empty-words
Open

KuljeetSinghXDA wants to merge 1 commit into
schultek:mainfrom
KuljeetSinghXDA:fix/case-style-empty-words

Conversation

@KuljeetSinghXDA

@KuljeetSinghXDA KuljeetSinghXDA commented Nov 16, 2026 •

Copy link
Copy Markdown

Problem

A private field combined with a case style breaks code generation.

@MappableClass(caseStyle: CaseStyle.pascalCase)
class Model with ModelMappable {
  @MappableField()
  final String _name = 'name';

  Model();
}

The builder fails with RangeError (index): Invalid value: Valid value range is empty: 0 from TextTransform.capitalCase. The name _name is split into ['', 'name'] because the leading separator produces an empty word, and capitalCase('') reads text[0].

The same empty words leak into generated keys:

  • CaseStyle.camelCase.transform('_field') returns Field instead of field
  • CaseStyle.paramCase.transform('_field') returns -field
  • CaseStyle.pascalCase.transform('_') throws

Fix

  • Ignore empty words when a name is split by the case-style splitter.
  • Return an empty string when no words remain.
  • Guard TextTransform.capitalCase against empty input.

After the fix, _name maps to name for camelCase, snakeCase, paramCase and lowerCase, and to Name for pascalCase. Names that contain no separators are unchanged.

Tests

  • packages/dart_mappable/test/core/case_style_test.dart: leading/trailing/repeated separators and capitalCase('').
  • packages/dart_mappable/test/serialization/private_field_test.dart: a private field with camelCase and pascalCase encodes with the expected key.
  • packages/dart_mappable_builder/test/case_style_test.dart: builds a model with a private field and asserts the generated key: r'Name' / key: r'name'. This test fails with the previous RangeError.

Verification

  • dart test in packages/dart_mappable: 159 tests passed.
  • dart test in packages/dart_mappable_builder: 10 tests passed.
  • dart analyze in both packages: no issues.
  • dart format --output=none --set-exit-if-changed on all touched files: clean.

Leading, trailing and repeated separators split a name into empty words.
Those empty words leaked into the generated key, and capitalCase even
threw a RangeError on the empty string.

This made the builder crash for private fields with a case style, for
example:

    @MappableClass(caseStyle: CaseStyle.pascalCase)
    class Model { final String _name; }

It also produced keys like '-name' for paramCase.

Ignore empty words and guard TextTransform.capitalCase against empty
input, so '_name' maps to 'Name' / 'name' / 'name' / 'name' instead of
crashing or emitting stray separators.
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