Skip to content

Fix TextGenerator to handle small StringLength attributes without exceptions#30

Draft
Copilot wants to merge 3 commits intomasterfrom
copilot/fix-11
Draft

Fix TextGenerator to handle small StringLength attributes without exceptions#30
Copilot wants to merge 3 commits intomasterfrom
copilot/fix-11

Conversation

Copy link

Copilot AI commented Jul 15, 2025

The TextGenerator.Generate() method was throwing ArgumentOutOfRangeException when used with small StringLength attribute values (1 or 2 characters). This was causing random failures when generating objects with properties decorated with [StringLength(1)] or [StringLength(2)].

Root Cause

The issue was in the random number generation logic:

return sampleText.Substring(0, random.Next(1, Length - 1)).Trim();

When Length was small, this created invalid ranges:

  • Length = 1: random.Next(1, 0)minValue > maxValue → Exception
  • Length = 2: random.Next(1, 1)minValue == maxValue → Exception

Solution

Replaced the problematic logic with proper bounds checking and special handling for edge cases:

public string Generate()
{
    // Ensure we have a valid length to work with
    int maxLength = Math.Min(Length, sampleText.Length);
    
    if (maxLength <= 0)
        return string.Empty;
    
    if (maxLength == 1)
        return sampleText.Substring(0, 1).Trim();
    
    // For lengths 2 and above, generate a random length between 1 and maxLength (inclusive)
    int randomLength = random.Next(1, maxLength + 1);
    return sampleText.Substring(0, randomLength).Trim();
}

Testing

  • All existing 111 tests continue to pass
  • Added 4 new comprehensive tests covering edge cases:
    • [StringLength(4000)] - large values
    • [StringLength(1)] - minimum edge case
    • [StringLength(2)] - second edge case
    • Multiple iterations to test randomness
  • Manual testing confirms proper behavior across all scenarios

The fix is minimal and surgical, preserving all existing functionality while safely handling the edge cases that were causing exceptions.

Fixes #11.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits July 15, 2025 15:29
Co-authored-by: PrintsCharming <85384+PrintsCharming@users.noreply.github.com>
Co-authored-by: PrintsCharming <85384+PrintsCharming@users.noreply.github.com>
Copilot AI changed the title [WIP] Applying a large string length attribute like [StringLength(4000)] randomly exceptions Fix TextGenerator to handle small StringLength attributes without exceptions Jul 15, 2025
Copilot AI requested a review from PrintsCharming July 15, 2025 15:35
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.

Applying a large string length attribute like [StringLength(4000)] randomly exceptions

2 participants