Skip to content

fix(common/load/streamToUnstructured): skip empty documents - #241

Merged
haarchri merged 2 commits into
crossplane:mainfrom
nkzk:fix-loader-skip-empty-documents
Aug 3, 2026
Merged

fix(common/load/streamToUnstructured): skip empty documents#241
haarchri merged 2 commits into
crossplane:mainfrom
nkzk:fix-loader-skip-empty-documents

Conversation

@nkzk

@nkzk nkzk commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Description of your changes

Reimplementing crossplane/crossplane#7089.

The streamToUnstructured() function loads a stream of yaml into unstructured objects, and is used by different Loaders during beta validate.
If a yaml document in the stream only contains a comment, or the file is empty, this function will currently not fail, but add an empty object to the unstructured array, resulting in this warning during validation in resource validate: [!] could not find CRD/XRD for: /, Kind=

A simple example of where this might be an issue:

We generate the rendered resources that should be validated with a tool that wraps around crossplane render functionality.
Since we want the user to know that these files were generated, we add the following comment at the top:

# Generated by: x
# Do not edit this file manually
---
apiVersion: example.io/v1alpha1
kind: SomeResource
metadata:
  name: example
spec: {}
---
other resources...

But these comment results in the empty document issue when passing this file to the validate command.

Since this is valid yaml i believe the best thing is to fix what i believe is a logical issue in this function, instead of changing the generated file to accommodate.

I added a test and fix that ensures streamToUnstructured() skips empty objects like this.

I have:

Need help with this checklist? See the cheat sheet.

Signed-off-by: Nikita Z <nkzk95@gmail.com>
@nkzk
nkzk marked this pull request as ready for review August 3, 2026 11:28
@nkzk
nkzk requested review from a team, jcogilvie and tampakrap as code owners August 3, 2026 11:28
@nkzk
nkzk requested review from negz and removed request for a team August 3, 2026 11:28
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Changes

Empty document handling

Layer / File(s) Summary
Skip empty documents
cmd/crossplane/common/load/loader.go, cmd/crossplane/common/load/loader_test.go
The loader ignores empty or comment-only YAML documents. The test verifies an empty non-nil resource slice without an error.

Estimated code review effort: 1 (Trivial) | ~3 minutes

Suggested reviewers: adamwg


Important

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

❌ Failed checks (1 error)

Check name Status Explanation Resolution
Breaking Changes ❌ Error The cmd diff removes prior behavior by skipping empty YAML objects; the supplied PR metadata contains no 'breaking-change' label. Add the 'breaking-change' label, or revise the change so empty documents retain the previous behavior.
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Feature Gate Requirement ✅ Passed The PR changes only loader code and its unit test. It adds no apis/** changes or feature-gate implementation, and it fixes empty-document handling rather than adding an experimental feature.
Title check ✅ Passed The title is descriptive, directly matches the change, and is 59 characters, below the 72-character limit.
Description check ✅ Passed The description clearly explains the empty-document issue, the fix, its impact, and the added test.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
cmd/crossplane/common/load/loader_test.go (1)

489-497: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add a regression case for comment-only documents.

Thanks for covering empty byte documents. The PR objective also includes YAML documents that contain only comments. Add a table entry such as []byte("# comment only\n") and expect an empty non-nil resources slice with no error.

🤖 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/common/load/loader_test.go` around lines 489 - 497, Extend the
table-driven cases in the loader test around “SkipEmptyDocuments” with a
comment-only YAML stream such as []byte("# comment only\n"). Assert no error and
an empty, non-nil resources slice, matching the existing empty-byte document
expectations.
🤖 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 `@cmd/crossplane/common/load/loader_test.go`:
- Around line 489-497: Extend the table-driven cases in the loader test around
“SkipEmptyDocuments” with a comment-only YAML stream such as []byte("# comment
only\n"). Assert no error and an empty, non-nil resources slice, matching the
existing empty-byte document expectations.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: b8dc6e85-c7d7-46a1-9216-8dd2d4163d63

📥 Commits

Reviewing files that changed from the base of the PR and between 7185390 and 596f22b.

📒 Files selected for processing (2)
  • cmd/crossplane/common/load/loader.go
  • cmd/crossplane/common/load/loader_test.go

… comment

ref. coderabbit suggestion

Signed-off-by: Nikita Z <nkzk95@gmail.com>
@nkzk nkzk changed the title fix(load/streamToUnstructured): skip empty documents fix(common/load/streamToUnstructured): skip empty documents Aug 3, 2026
@haarchri
haarchri merged commit 3c0858c into crossplane:main Aug 3, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants