Skip to content

fix: handle unknown metric types in API responses#40

Merged
lorenjphillips merged 2 commits into
mainfrom
fix/unknown-metric-type
Mar 12, 2026
Merged

fix: handle unknown metric types in API responses#40
lorenjphillips merged 2 commits into
mainfrom
fix/unknown-metric-type

Conversation

@lorenjphillips

@lorenjphillips lorenjphillips commented Mar 11, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Adds a #[serde(other)] catch-all Unknown variant to the MetricType enum
  • Prevents deserialization crashes when the backend returns metric types the CLI doesn't yet recognize (e.g. METRIC_TRACE_AUDIO_UPLOAD_STT_WER)
  • Hidden from --type CLI arg via #[value(skip)] so users can't create metrics with unknown type

Test plan

  • cargo test — 17 passed
  • cargo clippy -- -D warnings — clean
  • coval metrics list --include-builtin — returns results instead of crashing

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced system resilience to gracefully handle unknown values instead of failing on unexpected inputs.

Add serde(other) catch-all variant to MetricType enum so the CLI
gracefully handles new metric types added to the backend API without
crashing on deserialization.
@coderabbitai

coderabbitai Bot commented Mar 11, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

A new Unknown variant is added to the MetricType enum to handle deserialization of unrecognized values instead of failing. The variant includes #[serde(other)] and #[value(skip)] attributes, with the Display implementation mapping it to "BUILT_IN".

Changes

Cohort / File(s) Summary
Unknown MetricType Variant
src/client/models/metric.rs
Added Unknown enum variant to MetricType with #[serde(other)] and #[value(skip)] attributes. Updated Display implementation to render Unknown as "BUILT_IN".

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A mystery wrapped in serde and clap,
When unknown metrics come, we've got the map,
No failures now, just graceful decline,
As Unknown whispers "BUILT_IN" so fine! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding handling for unknown metric types in API responses to prevent deserialization failures.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/unknown-metric-type

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

@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: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/client/models/metric.rs`:
- Line 101: The Display implementation for the metric enum currently maps
Self::Unknown to "BUILT_IN", which mislabels any future/unrecognized server
metric types; update the mapping in the Display (the branch matching
Self::Unknown) to return a neutral label such as "UNKNOWN" (or similar) instead
of "BUILT_IN" so unknown variants are represented correctly and are easier to
spot.
- Around line 82-84: The MetricType enum's Unknown variant can be serialized
back into outbound requests (used in CreateMetricRequest and
UpdateMetricRequest) which yields an invalid literal; fix by preventing Unknown
from being written: either split into separate request/response enums so
requests never include Unknown, or implement a custom Serialize for MetricType
that returns an error (or Serde::serialize_str with an Err) when encountering
the Unknown variant; update references to MetricType in CreateMetricRequest and
UpdateMetricRequest to use the new request-only enum or ensure serialization
rejects Unknown so invalid "Unknown" strings are never sent.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3287ffa9-85ff-4b9b-bddd-ec480b2bcf95

📥 Commits

Reviewing files that changed from the base of the PR and between b97d894 and 9cdeaa5.

📒 Files selected for processing (1)
  • src/client/models/metric.rs

Comment on lines +82 to +84
#[serde(other)]
#[value(skip)]
Unknown,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🌐 Web query:

In serde 1.x, when an enum derives Serialize/Deserialize and one unit variant is annotated with #[serde(other)], how is that catch-all variant serialized if it has no #[serde(rename = "...")]?

💡 Result:

#[serde(other)] only affects deserialization (it’s the fallback when the input tag doesn’t match any other variant). It does not change how the variant is serialized. So if you serialize that unit variant and it has no #[serde(rename = "...")], Serde will serialize it like any normal unit variant, using its (possibly rename_all-transformed) variant name as the tag value (e.g., Unknown"Unknown" unless renamed by rules). [1]

Source:
[1] (serde.rs)

Citations:


Block Unknown from outbound request serialization.

The Unknown variant will be serialized as "Unknown" by the derived Serialize impl, which is not a valid backend enum literal. Since MetricType is used in CreateMetricRequest and UpdateMetricRequest, a deserialized unknown value can leak into outbound requests as invalid data. Either split request and response enums or add a manual Serialize impl that rejects Unknown on writes.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/client/models/metric.rs` around lines 82 - 84, The MetricType enum's
Unknown variant can be serialized back into outbound requests (used in
CreateMetricRequest and UpdateMetricRequest) which yields an invalid literal;
fix by preventing Unknown from being written: either split into separate
request/response enums so requests never include Unknown, or implement a custom
Serialize for MetricType that returns an error (or Serde::serialize_str with an
Err) when encountering the Unknown variant; update references to MetricType in
CreateMetricRequest and UpdateMetricRequest to use the new request-only enum or
ensure serialization rejects Unknown so invalid "Unknown" strings are never
sent.

Self::Regex => write!(f, "REGEX"),
Self::Pause => write!(f, "PAUSE"),
Self::CompositeEvaluation => write!(f, "COMPOSITE"),
Self::Unknown => write!(f, "BUILT_IN"),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Don't label every unknown server value as BUILT_IN.

Unknown now covers any future metric type, not just builtin ones. Rendering it as BUILT_IN turns the fallback into incorrect user-facing data and makes new backend types harder to spot. A neutral label like UNKNOWN would be safer here.

Suggested change
-            Self::Unknown => write!(f, "BUILT_IN"),
+            Self::Unknown => write!(f, "UNKNOWN"),
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Self::Unknown => write!(f, "BUILT_IN"),
Self::Unknown => write!(f, "UNKNOWN"),
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/client/models/metric.rs` at line 101, The Display implementation for the
metric enum currently maps Self::Unknown to "BUILT_IN", which mislabels any
future/unrecognized server metric types; update the mapping in the Display (the
branch matching Self::Unknown) to return a neutral label such as "UNKNOWN" (or
similar) instead of "BUILT_IN" so unknown variants are represented correctly and
are easier to spot.

@lorenjphillips lorenjphillips merged commit e5f01a1 into main Mar 12, 2026
7 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