feat: add --json-schema flag to xrd convert#142
Conversation
Signed-off-by: Jesús Fernández <7312236+fernandezcuesta@users.noreply.github.com>
9e0abf8 to
a92acf9
Compare
Signed-off-by: Jesús Fernández <7312236+fernandezcuesta@users.noreply.github.com>
📝 WalkthroughWalkthroughAdds shared CRD-to-JSON-Schema generation, refactors ChangesJSON Schema generation for xrd convert and xpkg get-crds
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 6✅ Passed checks (6 passed)
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.
Actionable comments posted: 2
🧹 Nitpick comments (1)
cmd/crossplane/xrd/convert_test.go (1)
109-189: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winSolid coverage of the three output modes — thank you for adding this!
One coverage suggestion that ties into the
writeOutputsconcern: every case here usesminimalXRD(...Namespaced, nil), which yields a single schema, so theOutputFile/Stdoutconcatenation path never sees more than one output. Adding a legacy-XRD-with-Claim case (whichTestToCRDsalready shows produces two CRDs) would exercise the multi-schema behavior and lock in whatever decision we land on for that path. Would you be open to adding it?🤖 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 `@cmd/crossplane/xrd/convert_test.go` around lines 109 - 189, TestConvertJSONSchema only covers a single-schema case, so writeOutputs is not validated when multiple JSON Schemas are produced. Update TestConvertJSONSchema to add a legacy XRD with Claim-enabled case, reusing the existing toCRDs and toJSONSchemaOutputs flow, so the OutputFile and Stdout paths are exercised with more than one schema. Keep the existing minimalXRD coverage, but add a case that mirrors the multi-CRD behavior already demonstrated in TestToCRDs and assert the expected multi-output behavior through convertCmd.writeOutputs.
🤖 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.
Inline comments:
In `@cmd/crossplane/xrd/convert.go`:
- Around line 116-146: `toJSONSchemaOutputs` can return multiple `convertOutput`
entries for a single XRD, but the concatenating branch in `writeOutputs` appears
to assume only one JSON schema output and may merge or overwrite them
incorrectly. Update `writeOutputs` to iterate over all outputs returned by
`toJSONSchemaOutputs` (and keep the `toCRDOutputs` behavior consistent) so every
schema emitted by `CRDsToJSONSchemas` is preserved, using the existing
`toJSONSchemaOutputs` and `writeOutputs` flow as the main places to adjust.
- Around line 163-179: The JSON schema output path in convert.go currently
concatenates multiple schemas into one invalid JSON document when c.OutputFile
or stdout is used. Update the conversion flow around the outputs buffer/write
logic to either reject multi-schema output unless c.OutputDir is set, or
otherwise enforce a single-schema constraint in --json-schema mode. Use the
existing Convert/c.OutputFile/stdout handling to add the guard before writing,
and make sure the multi-schema case is covered explicitly rather than silently
emitting invalid JSON.
---
Nitpick comments:
In `@cmd/crossplane/xrd/convert_test.go`:
- Around line 109-189: TestConvertJSONSchema only covers a single-schema case,
so writeOutputs is not validated when multiple JSON Schemas are produced. Update
TestConvertJSONSchema to add a legacy XRD with Claim-enabled case, reusing the
existing toCRDs and toJSONSchemaOutputs flow, so the OutputFile and Stdout paths
are exercised with more than one schema. Keep the existing minimalXRD coverage,
but add a case that mirrors the multi-CRD behavior already demonstrated in
TestToCRDs and assert the expected multi-output behavior through
convertCmd.writeOutputs.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 9de61f10-3569-431a-8696-519bc03b87fd
📒 Files selected for processing (5)
cmd/crossplane/xpkg/get_crds.gocmd/crossplane/xrd/convert.gocmd/crossplane/xrd/convert_test.gocmd/crossplane/xrd/help/convert.mdinternal/schemas/generator/json.go
Signed-off-by: Jesús Fernández <7312236+fernandezcuesta@users.noreply.github.com>
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
cmd/crossplane/xrd/convert_test.go (1)
129-132: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winPlease create the parent directory for the
-ocase.Thanks for covering the single-file path. One catch:
writeOutputs()does notMkdirAll()forOutputFile, so/out/schema.jsonwill fail onafero.NewMemMapFs()before the JSON assertions run. Could we either pre-create/outin the test or write to a top-level file instead?Suggested tweak
- cmd: convertCmd{JSONSchema: true, OutputFile: "/out/schema.json"}, - wantFile: "/out/schema.json", + cmd: convertCmd{JSONSchema: true, OutputFile: "/schema.json"}, + wantFile: "/schema.json",🤖 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 `@cmd/crossplane/xrd/convert_test.go` around lines 129 - 132, The OutputFile test case in convert_test.go is using a nested path without creating its parent directory, so the `writeOutputs()` path for `convertCmd` will fail before the JSON checks run. Update the `OutputFile` scenario to either pre-create the parent directory for `/out/schema.json` in the test setup or switch `wantFile` to a top-level file path, keeping the coverage focused on `convertCmd.writeOutputs()` behavior.
🧹 Nitpick comments (1)
cmd/crossplane/xrd/convert_test.go (1)
216-226: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winCould we make the rejection checks table-driven and assert the actual error?
Thanks for adding coverage for the multi-schema guardrails. Right now
err == nilonly proves a failure happened; it will not catch regressions in the actionableuse --output-dirguidance. Folding these intoargs/wantErrcases and comparing withcmp.Diff(..., cmpopts.EquateErrors())would match the repo’s test convention and lock down the CLI UX. As per path instructions,**/*_test.go: “Enforce table-driven test structure: PascalCase test names (no underscores), args/want pattern, use cmp.Diff with cmpopts.EquateErrors() for error testing.”🤖 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 `@cmd/crossplane/xrd/convert_test.go` around lines 216 - 226, The multi-schema rejection assertions in convertCmd.writeOutputs tests are still ad hoc and only check for nil errors; convert these cases in convert_test.go to the repo’s table-driven args/wantErr style, using cmp.Diff with cmpopts.EquateErrors() to assert the exact error message and preserve the actionable “use --output-dir” guidance. Keep the coverage around writeOutputs and the JSONSchema / OutputFile scenarios, but fold them into a single PascalCase test with structured cases instead of separate err == nil checks.Source: Path instructions
🤖 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.
Outside diff comments:
In `@cmd/crossplane/xrd/convert_test.go`:
- Around line 129-132: The OutputFile test case in convert_test.go is using a
nested path without creating its parent directory, so the `writeOutputs()` path
for `convertCmd` will fail before the JSON checks run. Update the `OutputFile`
scenario to either pre-create the parent directory for `/out/schema.json` in the
test setup or switch `wantFile` to a top-level file path, keeping the coverage
focused on `convertCmd.writeOutputs()` behavior.
---
Nitpick comments:
In `@cmd/crossplane/xrd/convert_test.go`:
- Around line 216-226: The multi-schema rejection assertions in
convertCmd.writeOutputs tests are still ad hoc and only check for nil errors;
convert these cases in convert_test.go to the repo’s table-driven args/wantErr
style, using cmp.Diff with cmpopts.EquateErrors() to assert the exact error
message and preserve the actionable “use --output-dir” guidance. Keep the
coverage around writeOutputs and the JSONSchema / OutputFile scenarios, but fold
them into a single PascalCase test with structured cases instead of separate err
== nil checks.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 387c002c-1e2f-4226-a32f-e7b669528125
📒 Files selected for processing (2)
cmd/crossplane/xrd/convert.gocmd/crossplane/xrd/convert_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
- cmd/crossplane/xrd/convert.go
Description of your changes
Add
--json-schemaflag toxrd convertsubcommand, similarly as whatxpkg get-crdssubcommand already provides.Refactor the common logic to avoid duplication.
Fixes #141
I have:
./nix.sh flake checkto ensure this PR is ready for review.backport release-x.ylabels to auto-backport this PR.Need help with this checklist? See the cheat sheet.