Skip to content

[go] Escape control characters in pattern validate tags - #25269

Merged
wing328 merged 3 commits into
OpenAPITools:masterfrom
mohitduhan19:fix/go-pattern-control-characters
Oct 19, 2026
Merged

wing328 merged 3 commits into
OpenAPITools:masterfrom
mohitduhan19:fix/go-pattern-control-characters

Conversation

@mohitduhan19

@mohitduhan19 mohitduhan19 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #25263

A pattern with a control character (for example one that forbids U+0000 to U+001F in a text field, a common validation) was copied raw into the Go validate:"regexp=..." struct tag. Go source can't hold a NUL or a byte order mark, so the generated model failed to compile with illegal character NUL.

AbstractGoCodegen now writes each control character (Character.isISOControl, so U+0000 to U+001F and U+007F to U+009F) as a regexp escape like \\x00, and a byte order mark as \\x{feff}, in the same place where backticks and commas are already escaped (#19358, #24349).

The backslash is doubled on purpose. Go unquotes the struct tag value before validator.v2 compiles the pattern, so \\x00 reaches the regexp as \x00. A single backslash (as suggested in the issue) works up to U+007F, but for U+0080 to U+009F Go's unquote turns \x9f into a raw byte that isn't valid UTF-8, and the regexp fails with "bad parameter".

Generated tag for the issue's spec:

Key *string `json:"key,omitempty" validate:"regexp=^[^\\x00-\\x1f]+$"`

Testing

  • New GoClientCodegenTest.testControlCharactersInPatternAreEscaped with a spec covering C0, DEL, C1 and a byte order mark. It checks the escaped tags and that the generated model contains no control characters (other than newlines and tabs) or byte order mark.
  • Generated a Go client from that spec and ran gofmt and go vet on it, and a Go test calling validator.Validate (gopkg.in/validator.v2): plain text passes, and strings containing NUL, U+001F, DEL, U+0085 or a byte order mark are rejected.
  • All Go generator tests pass (Go*Test, 71 tests), except testStringNotEnumValidation, which needs network access to download Go modules and fails the same way without this change in my environment.
  • ./bin/generate-samples.sh bin/configs/go-*.yaml: no sample changes, since no sample spec has a control character in a pattern.

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    Built the generator and CLI modules and regenerated the Go samples (no changes). No generator options changed, so the docs are unaffected.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

cc @antihax @grokify @kemokemo @jirikuncar @ph4r5h4d @lwj5 (Go technical committee)


Summary by cubic

Fixes generated Go models failing to compile when a schema pattern contains a control character or a byte order mark, previously emitted raw into the validate:"regexp=..." struct tag and rejected with illegal character NUL.

Bug Fixes

  • Control characters (U+0000–U+001F, U+007F–U+009F) and the byte order mark are now written as \\x00 / \\x{feff} escapes; the backslash is doubled because Go unquotes the struct tag value before the validator compiles the pattern.
  • Adds a test and a test spec covering C0, DEL, C1 controls, and a byte order mark.

Written for commit 97f608c. Summary will update on new commits.

View guided diff

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 3 files

View guided diff | Re-trigger cubic

@wing328

wing328 commented Oct 19, 2026

Copy link
Copy Markdown
Member

tested locally and no longer able to repeat the issue with this fix

@wing328
wing328 merged commit 27c3373 into OpenAPITools:master Oct 19, 2026
15 checks passed
@wing328 wing328 added this to the 7.27.0 milestone Oct 19, 2026
mohitduhan19 added a commit to mohitduhan19/mohitduhan19 that referenced this pull request Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [Go] pattern values containing control characters are copied raw into generated struct tags, producing invalid Go source (illegal character NUL)

2 participants