[go] Escape control characters in pattern validate tags

Merged
MO
1mo ago
+76-0

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.