Fix SnakeCase conversion for properties with consecutive uppercase letters #839
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.
Problem
The
MemberNameStrategies.SnakeCasestrategy had a bug when converting property names containing consecutive uppercase letters (acronyms). This caused templates to fail to render values for such properties.For example, consider this code:
The issue affected any property name with consecutive uppercase letters followed by lowercase letters:
OpenAIModelwas incorrectly converted toopen_aimodelinstead ofopen_ai_modelOEMVendorwas incorrectly converted tooemvendorinstead ofoem_vendorIDSecuritywas incorrectly converted toidsecurityinstead ofid_securityIDproduced an invalid string with a null characterRoot Cause
The algorithm didn't handle the case where consecutive uppercase letters are followed by a lowercase letter. When this pattern occurs (e.g., "AI" in "OpenAIModel"), the last uppercase letter ("I") actually starts a new word ("Model") and should have an underscore before it.
Additionally, the NET6+ implementation had a buffer size calculation bug where it pre-allocated space based on the total number of uppercase letters, but the actual logic didn't always need that many underscores, leading to uninitialized characters in the output.
Solution
Updated both the NET6+ and pre-NET6 implementations of
RenameSnakeCaseto:Correctly detect word boundaries: Insert an underscore before an uppercase letter when:
Fix buffer sizing: Calculate the exact number of underscores needed upfront to ensure the output string is properly sized
Changes
MemberNameStrategies.RenameSnakeCasefor NET6+ (.NET 6.0 and later)MemberNameStrategies.RenameSnakeCasefor pre-NET6 frameworksVerified Conversions
All conversions now work correctly:
UserName→user_nameOpenAIModel→open_ai_modelOEMVendor→oem_vendorIDSecurity→id_securityID→idXMLParser→xml_parserHTMLElement→html_elementIOError→io_errorJSONData→json_dataThe fix works correctly in both modern mode (using System.Text.Json naming policies) and legacy mode (using custom implementation).
Fixes #1234
Original prompt
Fixes #616
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.