Repository navigation
fix(jsonschema): emit unicode-safe patterns from concerto regexes - #301
rishabhknowss wants to merge 2 commits into
Conversation
Concerto regexes have no `u` flag, so their source may contain identity escapes such as `\<` that are invalid in unicode mode, which JSON Schema validators such as ajv use to compile `pattern`. Drop those redundant escapes when the source is not valid in unicode mode. Also compile every definition in the JSON Schema verification test, since ajv only compiles definitions that are referenced. Closes accordproject#300 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: rishabhknowss <rishabhrai1515@gmail.com>
|
Welcome, and thanks for your first PR to concerto-codegen, and for the clear write-up in #300. A maintainer will review it, and their feedback will appear here. CI has been approved to run for this PR. Drafted by an Accord Project agent, reviewed by a maintainer. |
There was a problem hiding this comment.
Thank you for this fix, and welcome to the project. Rewriting identity escapes such as \< so the emitted pattern compiles in unicode mode is the right fix, and the logic reads correctly. Falling back to the original source when the rewrite is still invalid is a safe choice. Strengthening the verification test to compile every definition through a $ref is a good change, because it checks definitions that were never compiled before. I found nothing blocking. One small suggestion on test coverage is below. Once that is considered and a maintainer has approved, this looks ready to merge.
Nothing blocking; some suggestions below. Findings: 1 nit.
Automated review by Mira, an Accord Project agent, at ae74e3c. Findings are labelled blocking, should-fix or nit; a maintainer makes the final call.
| expect(schema.properties.myString.pattern).equal('abc.*'); | ||
| }); | ||
|
|
||
| it('should generate unicode-safe patterns from regexes with identity escapes', () => { |
There was a problem hiding this comment.
nit · tests
The new test doesn't reach two branches of toUnicodeSafePattern. The alreadySafe regex is valid in unicode mode, so it returns early and the in-class \- handling (inClass && next === '-') never runs. The fallback that returns the original source when the rewritten regex is still invalid has no test either. Consider adding a case that needs rewriting and also has an escaped dash inside a class (for example /^[\w\-<\>]+$/), and a case that stays invalid after rewriting (for example a pattern containing \a), asserting that the source is returned unchanged. That would protect the edge handling against future changes.
There was a problem hiding this comment.
added both in a978654: ^[\w\-<\>]+$ now checks the escaped dash survives the rewrite, and ^\a<\>$ checks the fallback returns the source unchanged. confirmed each one fails if the matching branch is removed. npm test passes (692).
…back Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: rishabhknowss <rishabhrai1515@gmail.com>
There was a problem hiding this comment.
Thank you for this fix, and for adding the two extra tests so quickly. I read toUnicodeSafePattern closely: it returns already-valid sources unchanged, and otherwise drops the backslash only from identity escapes of non-alphanumeric characters that are not regex syntax. Those escapes match the same literal character with or without the backslash, so the meaning of the pattern is preserved. It keeps \- inside character classes, where unicode mode requires it, and falls back to the original source if the rewritten pattern is still invalid. The new tests cover each of those branches, and the verification test now compiling every definition through a $ref is a good way to stop this regression coming back. I have no changes to request. The test and verification workflows have not run on this PR yet, so please look out for them once a maintainer approves the runs, and check they pass, since the verification change widens what ajv compiles. After that, the PR needs a maintainer's approval before it can merge.
No problems found.
Automated review by Mira, an Accord Project agent, at a978654. Findings are labelled blocking, should-fix or nit; a maintainer makes the final call.
Closes #300
JSONSchemaVisitorcopied a concerto regex'ssourcestraight intopattern. concerto regexes have nouflag, but ajv compilespatternin unicode mode, so identity escapes like\<(used inagreement.cto) made the generated schema fail withInvalid escape.Changes
toUnicodeSafePattern(): if the source isn't valid with theuflag, drop backslashes before characters that don't need escaping (they match the same literal either way).\-is kept inside character classes. valid sources are returned unchanged.patternoutput.$ref, since ajv skips definitions nothing references (this is whyagreementpassed before). it fails on main withInvalid regular expression: /^[^\<\>]*$/u: Invalid escapeand passes with this fix.\<\>,\-outside a class, and an already-safe pattern.Flags
String(regex)invisitMapDeclaration) are left alone since fix(jsonschema): emit map property schemas directly and apply their key and value types #293 is already reworking them. happy to apply the same helper there once that lands.w3.org/2001/XMLSchema.xsdcan't be fetched; unrelated to this change.Related Issues
patternfrom concerto regex fails in ajv (unicode mode) #300Author Checklist
--signoffoption of git commit.mainfromfork:branchname🤖 Generated with Claude Code