Skip to content

Sample a repeated character class instead of printing a pattern placeholder - #64

Merged
glenn-jocher merged 2 commits into
mainfrom
pattern-examples
Nov 30, 2026
Merged

glenn-jocher merged 2 commits into
mainfrom
pattern-examples

Conversation

@glenn-jocher

@glenn-jocher glenn-jocher commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

stringExample prints <pattern value> when no candidate matches a string's pattern. The usual case is an ID or a hash: a pattern such as ^[0-9a-fA-F]{24}$ or ^[a-f\d]{32}$ that no word-like candidate can satisfy. Docs and request samples then show "<pattern value>", which is neither valid nor readable.

This adds one candidate for a pattern that is a single character class repeated ({n}, {n,m} or +): it cycles through the characters the class allows, lowercase and digits first.

Pattern Before After
^[0-9a-fA-F]{24}$ "<pattern value>" "0123456789abcdef01234567"
^[a-f\d]{32}$ "<pattern value>" "0123456789abcdef0123456789abcdef"
^[A-Z]{3}$ "<pattern value>" "ABC"
^[a-z0-9_-]+$, ^\d{4}-\d{2}$, plain strings unchanged unchanged

The class is applied with RegExp to each candidate character rather than parsed, so ranges, escapes such as \d, and negations behave as the pattern defines them. Like every other candidate, the result is kept only if the whole pattern and the length limits accept it. Patterns of any other shape keep their existing behavior.

Testing

  • bun test: 21 pass, including a new assertion for ^[0-9a-fA-F]{24}$.
  • The table above was produced by schemaExample on this branch.
  • bun run typecheck and bun run lint pass.

🛠️ PR Summary

Made with ❤️ by Ultralytics Actions

🌟 Summary

String example generation now creates a candidate for patterns consisting of one repeated character class, replacing an otherwise unusable placeholder when the candidate passes validation.

📊 Key Changes

  • Detects a single character class followed by {n}, {n,m}, or +.
  • Builds candidate characters by testing digits and lowercase letters first, then uppercase letters, against the class.
  • Repeats those characters to form a candidate, capped at 256 characters; existing pattern and length checks still determine whether it is accepted.
  • Adds a test asserting that ^[0-9a-fA-F]{24}$ produces 0123456789abcdef01234567.

🎯 Purpose & Impact

  • Generated examples for matching patterns such as hexadecimal IDs can now contain a value that fits the pattern instead of "<pattern value>"; patterns outside the supported shape retain their existing behavior.

@UltralyticsAssistant UltralyticsAssistant added bug Something isn't working enhancement New feature or request labels Nov 30, 2026

@UltralyticsAssistant UltralyticsAssistant left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 PR Review

Made with ❤️ by Ultralytics Actions

Reviewed string-example generation and its new test. The ID/hash case works, but punctuation-only repeated classes still produce the placeholder.

💬 Posted 1 inline comment
  • 📝 LOW lib/openapi.ts:961 For a valid repeated class that allows only punctuation, such as ^[._-]{3}$, both candidate alphabets are empty, so this falls through to <pattern value> instead of sampling the class. Include punctuation candidates after the current alphanumeric candidates.

Comment thread lib/openapi.ts Resolved
@glenn-jocher
glenn-jocher merged commit 71a34e3 into main Nov 30, 2026
7 checks passed
@glenn-jocher
glenn-jocher deleted the pattern-examples branch November 30, 2026 21:01
@UltralyticsAssistant

Copy link
Copy Markdown
Member

🎉 PR #64 was merged—thank you, @glenn-jocher! String example generation can now produce valid candidates for repeated character-class patterns, including hexadecimal IDs, instead of falling back to "<pattern value>".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants