Skip to content

fix(api): return empty array instead of null for silence matchers - #5331

Merged
ultrotter merged 1 commit into
prometheus:mainfrom
siavashs:fix/apiv2/silence-null-matchers
Jun 29, 2026
Merged

ultrotter merged 1 commit into
prometheus:mainfrom
siavashs:fix/apiv2/silence-null-matchers

Conversation

@siavashs

@siavashs siavashs commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

GettableSilenceFromProto left the Matchers field as a nil slice when a silence had no matcher sets, only ever populating it via append. Go's encoding/json marshals a nil slice as null, so such silences were serialized as "matchers": null. The OpenAPI schema declares matchers as a required array, so an empty silence should serialize as [] instead.

Initialize Matchers to an empty slice so the field is always a valid JSON array, keeping the v2 API schema-compliant for matcher-less silences.

Related to #5326

image

Pull Request Checklist

Please check all the applicable boxes.

  • Please list all open issue(s) discussed with maintainers related to this change
    • Fixes #
  • Is this a new Receiver integration?
  • Is this a bugfix?
    • I have added tests that can reproduce the bug which pass with this bugfix applied
  • Is this a new feature?
    • I have added tests that test the new feature's functionality
  • Does this change affect performance?
    • I have provided benchmarks comparison that shows performance is improved or is not degraded
      • You can use benchstat to compare benchmarks
    • I have added new benchmarks if required or requested by maintainers
  • Is this a breaking change?
    • My changes do not break the existing cluster messages
    • My changes do not break the existing api
  • I have added/updated the required documentation
  • I have signed-off my commits
  • I will follow best practices for contributing to this project

Which user-facing changes does this PR introduce?

[BUGFIX] silence with no matchers should populate an empty array in API response

GettableSilenceFromProto left the Matchers field as a nil slice when a
silence had no matcher sets, only ever populating it via append. Go's
encoding/json marshals a nil slice as null, so such silences were
serialized as "matchers": null. The OpenAPI schema declares matchers as
a required array, so an empty silence should serialize as [] instead.

Initialize Matchers to an empty slice so the field is always a valid JSON
array, keeping the v2 API schema-compliant for matcher-less silences.

Related to prometheus#5326

Signed-off-by: Siavash Safi <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 29, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

In GettableSilenceFromProto, the Matchers field of the returned GettableSilence struct is now initialized to an empty open_api_models.Matchers{} instead of being left unset. All other logic remains unchanged.

Changes

Matchers Initialization

Layer / File(s) Summary
Initialize Matchers field on construction
api/v2/compat.go
Matchers is now set to open_api_models.Matchers{} in the GettableSilence struct literal, preventing a nil value when no matchers are populated.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

Possibly related PRs

  • prometheus/alertmanager#5330: Addresses the same legacy silence Matchers field initialization from matcher_sets in snapshot marshaling, directly complementing this change.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately describes the main API serialization fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
Description check ✅ Passed The PR description follows the template closely, explains the bugfix, links the related issue, and includes release notes.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 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 `@api/v2/compat.go`:
- Line 41: The matcher-less silence fix is incomplete because the OpenAPI model
contract still rejects empty matcher sets even though compat.go now serializes
them as an empty array. Update the validation logic in the silence model and
matcher model so the generated contract accepts matcher-less silences
consistently; specifically adjust the validation in Silence and Matchers to
allow len(MatcherSets) == 0 without failing schema checks, keeping the
serialization change in sync with the validator behavior.
🪄 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: Enterprise

Run ID: 470d0f7f-0e79-4948-ad19-3175884fd37c

📥 Commits

Reviewing files that changed from the base of the PR and between 058616d and 1fda5b5.

📒 Files selected for processing (1)
  • api/v2/compat.go

Comment thread api/v2/compat.go Resolved
@ultrotter
ultrotter merged commit 3aef64d into prometheus:main Jun 29, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants