[swift5] fix: keep pattern regexes valid Swift string literals - #24865
Open
wiebren wants to merge 2 commits into
Open
[swift5] fix: keep pattern regexes valid Swift string literals#24865wiebren wants to merge 2 commits into
wiebren wants to merge 2 commits into
Conversation
addRegularExpressionDelimiter wraps a pattern in "/.../" and escapes every inner "/" as "\/", which is not a valid escape sequence in a Swift string literal, so any schema pattern containing "/" produced code that does not compile. The generated Validator hands rule.pattern straight to NSRegularExpression, which has no delimiter syntax, so the delimiters were never wanted in the first place: override toRegularExpression to escape the pattern for the string literal without adding delimiters, the same way AbstractKotlinCodegen does. Fixes OpenAPITools#15604 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CxDNCjqJycKTfzVWg2SeTJ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Any schema
patternthat contains a/currently generates swift5 code that does notcompile. Fixes #15604 (open since 6.3.0, still present on master).
Given:
master emits:
and
swiftcstops witherror: invalid escape sequence in literalon every\/—\/isnot a valid escape in a Swift string literal. The
plainSlashcase shows the backslash isnot coming from the spec (its pattern contains no backslash at all): it is introduced by the
generator.
The cause
DefaultCodegen.addRegularExpressionDelimiterwraps an undelimited pattern in/.../andescapes every inner slash while doing so:
That Perl-style delimiter escaping is fine for languages that embed the pattern in a
/.../regex literal, but swift5's
modelObject.mustacheembeds it in a double-quoted stringliteral, where
\/is a compile error.The delimiters themselves are also unwanted for this generator: the generated
Validation.swifthandsrule.patternstraight toNSRegularExpression(pattern:), whichhas no delimiter syntax — ICU treats the wrapping
/as literal characters to match.The fix
Swift5ClientCodegenoverridestoRegularExpressionto escape the pattern for the stringliteral without adding delimiters — the same override
AbstractKotlinCodegenalready usesfor the same reason (its patterns also go into a plain string literal):
After the fix the repro above generates
which
swiftc -parseaccepts, andNSRegularExpression("http(s)?://x")now actuallymatches
https://x(with the delimiters it could not, because of the leading literal/).A pattern that already carries delimiters in the spec — like the petstore's
/[a-z]/i— isleft exactly as it was, so no sample output changes:
./bin/generate-samples.sh ./bin/configs/swift5-*.yamland./bin/utils/export_docs_generators.shboth produce anempty diff. The only pattern in the swift5 samples is the already-delimited
/[a-z]/i.Tests
Swift5ClientCodegenTest#testToRegularExpressionRemainsValidInSwiftStringLiteralcovers apattern with bare slashes, one with JSON-style
\/escapes, one with regex backslashescapes, and one that already carries delimiters. It fails on master (master returns
"/http(s)?:\/\/x/"for the first case) and passes with the fix; the full swift5 testclasses (40 tests) pass.
The swift6 generator inherits the same defect from the same
DefaultCodegendefault and theidentical override would fix it there too — happy to add it here or as a follow-up PR,
whichever you prefer.
PR checklist
./bin/generate-samples.sh ./bin/configs/swift5-*.yamland./bin/utils/export_docs_generators.sh— bothproduced no diff, see above).
Generated with Claude Code
Summary by cubic
Fixes swift5 codegen so schema patterns containing
/generate Swift that compiles (issue #15604) and reachNSRegularExpressionwithout unwanted delimiters.Bug Fixes
toRegularExpressionnow escapes the pattern for the Swift string literal without adding/.../delimiters, matchingAbstractKotlinCodegen.swiftcrejected the\/escapes andNSRegularExpressionmatched the literal delimiters, sohttp(s)?://xnever matchedhttps://x.Written for commit 4a44ee0. Summary will update on new commits.