CodeGen: value-list values are not escaped in generated TypeScript (union/z.literal) or the EntityFieldValue INSERT
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- postgresql, sql, typescript
Research direction
Read the value-list interpolation points in packages/CodeGenLib/src/Misc/entity_subclasses_codegen.ts at lines 529 and 1650, then inspect EscapeSingleQuotes() nearby. Also read the INSERT in packages/CodeGenLib/src/Database/manage-metadata.ts at line 6595. Reproduce with values containing a backslash and an apostrophe; done means generated TypeScript builds and both values are stored correctly.
Written by the indexing model from the issue text.
Description
Summary
CodeGen interpolates value-list values (EntityFieldValue.Value) into generated code and SQL without escaping them. A value containing a backslash or a single quote produces invalid TypeScript, which breaks the GeneratedEntities build. In the SQL path, such a value is silently dropped from the value list.
Seen on @memberjunction/codegen-lib 6.1.6 (PostgreSQL). The same code is unchanged on next (f135c8cf).
Where
All three places wrap the raw value in quotes:
File (next) |
Line | Code |
|---|---|---|
packages/CodeGenLib/src/Misc/entity_subclasses_codegen.ts |
529 | sortedValues.map((v) => `${quotes}${v.Value}${quotes}`).join(' | ') (TS union type for the getter/setter) |
packages/CodeGenLib/src/Misc/entity_subclasses_codegen.ts |
1650 | sortedValues.map((v) => `z.literal(${quotes}${v.Value}${quotes})`) (Zod schema) |
packages/CodeGenLib/src/Database/manage-metadata.ts |
6595 | ... '${v}', '${v}', ... in the syncEntityFieldValues INSERT (Value and Code) |
EntitySubClassGeneratorBase.EscapeSingleQuotes() already exists in the same file (escapes \ and ') and is used for relationship names. It is not applied to value-list values.
Reproduce
- Add a soft value list through
additionalSchemaInfo(Fields[].PossibleValues) on a string column, with values containing a backslash and an apostrophe, for example["PASS", "FAIL", "\\"]and["General", "Women's Network"]. A CHECK-constraint-derived list would hit the same code. - Run
mj codegen.
What happens
- Backslash. The Zod schema gets
z.literal('\'). The backslash escapes the closing quote, sotscreports thousands ofTS1005/TS1434errors from that line to the end of the file. The AFTER commandnpm run buildinGeneratedEntitiesfails. - Single quote. The INSERT becomes
VALUES (..., 'Women's Network', 'Women's Network', ...), a SQL syntax error. That value never reachesEntityFieldValue; the other values of the list are stored, so the list is silently incomplete. The same interpolation also lets a value inject SQL. - Hard to recover from. After the first run,
applyValueListForFieldskips any field that already hasEntityFieldValuerows. Removing the bad value fromadditionalSchemaInfotherefore does not remove it from the database. The row has to be deleted by hand before the TypeScript can be regenerated cleanly.
Expected
Values are emitted as valid string literals whatever characters they contain. Either:
- run them through
EscapeSingleQuotes, or - use
JSON.stringify(v.Value)for the TS/Zod output, and a parameterized query (or proper SQL quoting) for the INSERT.
Numeric value lists (NeedsQuotes === false) are unaffected.
Context
This came from loading data-derived value lists for a large connector schema (about 2,200 fields). Real operational data contains stray characters, such as a lone \ entered as a grade, and labels with apostrophes. Any data-driven value list will eventually hit this.
🤖 Generated with Claude Code
- Dominant language
- TSQL
- Stars
- 32
- Forks
- 7
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 714
Getting set up
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from MemberJunction/MJ
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
MemberJunction/MJ#6024 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
MemberJunction/MJ#6018 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
MemberJunction/MJ#6012 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
MemberJunction/MJ#5930 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
MemberJunction/MJ#5864 ·
Maintainers usually reply within 1 day
All issues in MemberJunction/MJ
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
AstrBotDevs/AstrBot#10720 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
Maintainers usually reply within 1 day
-
bug: author create/update with an unknown avatarId returns 404 instead of 400 UNKNOWN_REFERENCEPossibly taken @Vinu2111 claimed this today. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
atomityhq/blog-cms#59 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
area: core
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day