Conversation
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
|
You would only escape So if your Regexp was written like => I am a bit confused how you are hitting this in the first place, and I am not 100% convinced that this is always correct. It seems "harmless enough", but that shouldn't be the basis of our decision here tbh. Can you show us any official documents that show escaped backslashes in OpenAPI definitions? This seems like a quirk of whatever you generate your spec with here tbh. |
|
Thanks for taking a look, that's a fair question. To be upfront, I found this while reading the regex constants code rather than from a production spec, and then checked what common tools emit. Specs generated from Zod schemas are one real source: On the spec side, OpenAPI 3.0 says Today that input makes generation fail (the |
Root cause: with
outputRegexConstants: true,generateRegexConstantsForTypeescapes the schemapatternby replacing every/with\/before wrapping it in a regex literal. That also hits slashes that are already escaped. A pattern like^https?:\/\/[^\s]+$(valid in JSON Schema, and what you get from any JSRegExp#source, sincesourcealways escapes/) becomes/^https?:\\/\\/[^\s]+$/. The\\is read as an escaped backslash, so the next/closes the literal early, and prettier then throwsSyntaxError: Invalid characterwhile formatting the output, so the whole generation fails.Fix: only escape bare slashes. The replace now matches escape pairs (a backslash plus the next character) and bare
/together, and leaves the pairs untouched. Patterns without escaped slashes produce the same output as before.Test: the
websitepattern intest/fixtures/petstore.yamlnow uses the escaped form (petstore.jsonkeeps the unescaped one, which the existing test still covers), and a new test ingenerateEndpoints.test.tsexpectsexport const userWebsitePattern = /^https?:\/\/[^\s]+$/. Before the fix it fails (and so does the existing YAML regex test) with the SyntaxError above; after the fix all regex constants tests pass.