fix(schema): preserve YAML formatting when forking a schema#1130
fix(schema): preserve YAML formatting when forking a schema#1130linjinze999 wants to merge 1 commit into
Conversation
Use yaml's Document API (parseDocument + doc.set) instead of round -tripping through parseSchema/stringifyYaml so block scalars, comments and key order in the source schema.yaml survive the fork
📝 WalkthroughWalkthroughThe PR refactors how the ChangesYAML Document-Level Editing
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/commands/schema.ts (1)
629-631: ⚡ Quick winDon’t worry about silent corruption:
toString()already fails on YAML parse errors
yaml(eemeli/yaml)Document#toString()throws whendoc.errors.length > 0, sofs.writeFileSync(...)won’t run and the existing try/catch will prevent overwriting an invalidschema.yaml.- An explicit
doc.errorscheck is still a useful optional improvement for clearer error messaging and to avoid mutating viadoc.set.Proposed fix
const doc = parseDocument(schemaContent); +if (doc.errors.length > 0) { + throw new Error( + `Failed to parse '${destSchemaPath}': ${doc.errors.map((e) => e.message).join('; ')}` + ); +} doc.set('name', destinationName); fs.writeFileSync(destSchemaPath, doc.toString());🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/commands/schema.ts` around lines 629 - 631, The code currently calls parseDocument(schemaContent) then mutates the Document with doc.set('name', destinationName) before writing, which can silently mutate an invalid Document; update the flow in the block that uses parseDocument, Document#toString, and fs.writeFileSync so you first check doc.errors (after parseDocument) and if any errors exist throw or surface a clear error message, and only then call doc.set('name', destinationName) and fs.writeFileSync(destSchemaPath, doc.toString()); this ensures you don’t mutate an invalid Document and that toString() won’t be relied on to detect parse problems.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/commands/schema.ts`:
- Around line 629-631: The code currently calls parseDocument(schemaContent)
then mutates the Document with doc.set('name', destinationName) before writing,
which can silently mutate an invalid Document; update the flow in the block that
uses parseDocument, Document#toString, and fs.writeFileSync so you first check
doc.errors (after parseDocument) and if any errors exist throw or surface a
clear error message, and only then call doc.set('name', destinationName) and
fs.writeFileSync(destSchemaPath, doc.toString()); this ensures you don’t mutate
an invalid Document and that toString() won’t be relied on to detect parse
problems.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: e76ea6c6-73d8-48bc-bf75-41ddf0f74f7e
📒 Files selected for processing (1)
src/commands/schema.ts
Use yaml's Document API (parseDocument + doc.set) instead of round -tripping through parseSchema/stringifyYaml so block scalars, comments and key order in the source schema.yaml survive the fork
Fix yaml:
xxx
yyy
will be:
Summary by CodeRabbit