-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correct escaping of forward slash in pattern regex, deprioritizing escaping of unnecessarily escaped forward slash in pattern regex #288
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2f34f63
correct test expectation to look for a backslash
mjperrone 54e4ea5
correct text expectation for validation.test.ts
mjperrone 18f1509
add back replacing / with \/
mjperrone 7757031
add test showing unnecessarily escaped forward slash rendering to a r…
mjperrone 6fd06aa
add changeset
mjperrone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"openapi-zod-client": patch | ||
--- | ||
|
||
Fix escaping of forward slash `/` in pattern regex so the output code will be `/\//`. This change also breaks escaping of unnecessarily escaped forward slash `\/` in pattern regex, such that the output code will be `/\\//`. |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,11 @@ test("regex-with-escapes", () => { | |
properties: { | ||
str: { | ||
type: "string", | ||
pattern: "^\/$" | ||
pattern: "^/$" | ||
}, | ||
} | ||
}}) | ||
).toMatchInlineSnapshot( | ||
'"z.object({ str: z.string().regex(/^\/$/) }).partial().passthrough()"' | ||
'"z.object({ str: z.string().regex(/^\\/$/) }).partial().passthrough()"' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The point of this test is that we want the resulting code to include the escape character Since the
|
||
); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { getZodSchema } from "../src/openApiToZod"; | ||
import { test, expect } from "vitest"; | ||
|
||
test("regex-with-unnecessary-escape fails", () => { | ||
expect( | ||
getZodSchema({schema: { | ||
type: "object", | ||
properties: { | ||
str: { | ||
type: "string", | ||
pattern: "^\\/$" | ||
}, | ||
} | ||
}}) | ||
).toMatchInlineSnapshot( | ||
// This is what it should produce, but to prioritize escaping forward slashes without an unnecessary escape, | ||
// we leave this is failing for now. | ||
// '"z.object({ str: z.string().regex(/^\\/$/) }).partial().passthrough()"' | ||
'"z.object({ str: z.string().regex(/^\\\\/$/) }).partial().passthrough()"' | ||
); | ||
}); |
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When this string is loaded into memory it will be:
^/$
.The previous
\
was unnecessary, an escape character for a character that doesn't need escaping, resulting in the character anyways: