Skip to content

[patch] Stop [PrefixAndSuffix] letting the prefix and suffix overlap, and reject empty strings - #408

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/338-prefix-suffix-overlap
Oct 9, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/338-prefix-suffix-overlap

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #338

What was wrong

PrefixAndSuffixAttribute had two problems:

  • Overlap. It checked StartsWith(prefix) and EndsWith(suffix) independently, so the same characters could count as both. A lone " passed [PrefixAndSuffix("\"", "\"")], and "aba" passed [PrefixAndSuffix("ab", "ba")].
  • Empty strings. It returned success for an empty string, unlike [StartsWith] and [EndsWith], which reject one.

Change (Semantics.Strings/Validation/Attributes/Text/PrefixAndSuffixAttribute.cs)

  • An empty string now fails, matching the sibling attributes.
  • A value shorter than prefix.Length + suffix.Length fails before the prefix and suffix checks, so each delimiter needs its own characters.
  • The type's summary now states both rules.

Release note: this tightens validation. A type that relied on "" or an overlapping value being accepted will now get an ArgumentException from Create.

Tests (TextValidationAttributesTests)

  • PrefixAndSuffixAttribute_LoneDelimiterServingAsBoth_ShouldThrow: " with [PrefixAndSuffix("\"", "\"")]
  • PrefixAndSuffixAttribute_OverlappingPrefixAndSuffix_ShouldThrow: aba with [PrefixAndSuffix("ab", "ba")]
  • PrefixAndSuffixAttribute_EmptyString_ShouldThrow
  • PrefixAndSuffixAttribute_DelimitersEachPresent_ShouldPass: "" (two quotes) and "quoted" are still accepted
  • PrefixAndSuffixAttribute_AdjacentPrefixAndSuffix_ShouldPass: abba is accepted, since the prefix and suffix touch but don't overlap

These are the three inputs from the issue's acceptance criteria. The existing PrefixAndSuffix_* tests, including "PrefixTestSuffix", still pass.

Verification

  • With the attribute change reverted, the three rejection tests fail. With it, all 15 PrefixAndSuffix tests pass.
  • The full Semantics.Test suite passes on net10.0: 1486 passed, 0 failed, 8 skipped (the same 8 are skipped on main).
  • Semantics.Strings builds with the local Sonar analyzers (.sonarlint/sonar-local.props) with 0 warnings.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LoEzLXruuTHCcJnXtVa49Z


Generated by Claude Code

… and reject empty strings

PrefixAndSuffixAttribute checked StartsWith and EndsWith independently, so
one '"' satisfied [PrefixAndSuffix("\"", "\"")], and it returned success for
an empty string. A value now needs at least prefix.Length + suffix.Length
characters, and an empty string fails as it does for [StartsWith]/[EndsWith].

Fixes #338

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LoEzLXruuTHCcJnXtVa49Z
@sonarqubecloud

sonarqubecloud Bot commented Oct 8, 2026

Copy link
Copy Markdown

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.

[PrefixAndSuffix] lets prefix and suffix overlap: [PrefixAndSuffix("\"", "\"")] accepts a lone ", and empty strings always pass

2 participants