Correct escaping of forward slash in pattern regex, deprioritizing escaping of unnecessarily escaped forward slash in pattern regex #288
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.
Fixing the test definition
First I fix the issue with the test I initially added here.
The core issue was not acknowledging that the test strings are written in javascript which has its own escaping rules, so the string that was being provided and tested for was wrong. I'll put details in-line in the PR
Fixing the implementation
For the case that my PR broke (
/
needs to end up as/\//
), it's easy to understand what to doI add back the
.replace(/\//g, "\\/")
to transform/
to\/
(to prepare the pattern for being defined in code with a regex string (/<regex in here>/
). That's easy to understand when you look at the test case description I put in the PR.What was happening in my initial bug report?
In this bug report I indicated that the pattern
.+\/.+
produced/.+\\/.+/
output code, which is invalid because the/
in the middle of the regex is not escaped.To simplify further the pattern
\/
yields the code/\\//
.What does
\/
in an initial pattern mean? It means the character/
, written with an unnecessary escape character.Should openapi-zod-client support transforming regexes that have unnecessary escape characters?
OpenAPI Spec points to JSON Schema spec, which points to ECMA-262, which is a little too dense for me to quickly get to a definitive answer.
In Javascript, it's totally fine to have an unnecessary escape character when defining regular expressions:
So I'm inclined to think that openapi-zod-client should support this.
how to support it?
To address the case where the user gives a pattern with an unnecessary escape character like
/\
, what is the right output code?probably
/\//
However that means the existing replace
.replace(/\//g, "\\/");
(/
->\/
) would get in the way of this solution.Another option could be to replace
\/
with/
first. That way\/
->/
->\/
in the final output. That works, but would break the situation where the user specifies an escaped backslash followed by an unescaped forward slash like\\/
. That would go\\/
->\/
->\\/
when we'd want to end up with\\\/
.I'm not sure a better way to address this situation. I'm publishing this PR as is because I think it's more important to address the case where
\
is specified without an unnecessary escape character.