Skip to content

fix(api): let domain validation own the metered entitlement usage period - #4981

Merged
tothandras merged 2 commits into
mainfrom
fix/metered-entitlement-usage-period-validation
Aug 24, 2026
Merged

tothandras merged 2 commits into
mainfrom
fix/metered-entitlement-usage-period-validation

Conversation

@tothandras

@tothandras tothandras commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The v3 plan and addon rate card converters hard-rejected (400 at create) a metered entitlement that has no usage_period and no billing_cadence to default it from. That duplicates the domain layer: MeteredEntitlementTemplate.Validate already rejects a non-positive usage period (entitlement_template_negative_usage_period).

The converters now keep the defaulting (usage_period ← rate card billing cadence) but map a zero usage period through, so the domain validation reports it.

Companion one-liner: entitlement_template_negative_usage_period was missing the HTTP 400 status attribute its sibling ..._less_then_an_hour issue carries — added so its 400 mapping is deliberate rather than dependent on the sibling firing alongside.

Converters should translate bodies to service types faithfully and leave validation to the owning layer (same principle as #4979, sibling of #4980).

Behavior change

Create with a metered entitlement lacking both usage_period and billing_cadence still returns 400 at create — but now with the structured domain code entitlement_template_negative_usage_period (field $.entitlementTemplate.usagePeriod) instead of the converter's free-text message.

Writing the e2e tests surfaced why it stays a create-time 400 rather than the draft-with-issues flow used by other rate card warnings: ent auto-runs Validate() on the entitlement_template field's Go type at insert (ent/db/addonratecard_create.go generated check), severity-blind. Entitlement warnings therefore block persistence while rate-card-level warnings (e.g. billing_cadence_invalid_value) are stored on the draft. That layering inconsistency predates this PR and is left as a follow-up.

Test plan

  • New e2e tests (TestV3PlanMeteredEntitlementWithoutUsagePeriod, TestV3AddonMeteredEntitlementWithoutUsagePeriod) assert both endpoints reject with 400 + entitlement_template_negative_usage_period; run green against a local stack built from this branch.
  • go test ./api/v3/handlers/addons/... ./api/v3/handlers/plans/... ./openmeter/productcatalog/... green.

🤖 Generated with Claude Code

https://claude.ai/code/session_01S93A9RigLLfBnXoauNNt7w

Summary by CodeRabbit

  • Bug Fixes
    • Improved validation for metered entitlements with missing usage periods during plan and addon creation.
    • Usage periods continue to be inferred from billing cadence when available.
    • Missing usage periods now receive consistent domain validation instead of failing during conversion.
    • Invalid or missing usage periods return an HTTP 400 response with a clear validation error.

Greptile Summary

The PR moves missing metered-entitlement usage-period handling from the v3 plan and add-on converters into product-catalog domain validation.

  • Preserves billing-cadence defaulting while allowing an unresolved zero period to reach domain validation.
  • Explicitly maps the negative-usage-period validation issue to HTTP 400.
  • Adds end-to-end coverage confirming plan and add-on creation rejects the invalid entitlement with the domain validation code.

Confidence Score: 5/5

The PR appears safe to merge because no blocking failure remains within the eligible follow-up-review scope.

No blocking failure remains.

Important Files Changed

Filename Overview
api/v3/handlers/addons/convert.go Defers unresolved metered-entitlement usage periods to product-catalog validation while retaining cadence defaulting.
api/v3/handlers/plans/convert.go Applies the same domain-validation ownership change to plan rate-card conversion.
openmeter/productcatalog/errors.go Adds an explicit HTTP 400 attribute to the negative usage-period validation issue.
e2e/entitlement_usage_period_v3_test.go Covers the plan and add-on create paths and verifies the domain validation code returned for an absent usage period.

Sequence Diagram

sequenceDiagram
  participant Client
  participant API as V3 Plan/Add-on API
  participant Converter
  participant Domain as Product Catalog Validation
  Client->>API: Create with metered entitlement
  API->>Converter: Convert request
  alt Billing cadence is available
    Converter->>Converter: Default usage period from cadence
  else No usage period or cadence
    Converter->>Domain: Pass zero usage period through
    Domain-->>API: negative_usage_period validation issue
    API-->>Client: HTTP 400
  end
Loading

Reviews (2): Last reviewed commit: "test(api): cover missing entitlement usa..." | Re-trigger Greptile

The v3 plan and addon converters hard-rejected a metered entitlement
with no usage_period and no billing_cadence to default from, duplicating
MeteredEntitlementTemplate.Validate. Map the zero usage period through
so the issue surfaces on the draft and blocks publish.

Add the missing HTTP 400 attribute to
entitlement_template_negative_usage_period so its publish-time mapping
no longer depends on the sibling less-than-an-hour issue firing
alongside it.

Claude-Session: https://claude.ai/code/session_01S93A9RigLLfBnXoauNNt7w
@tothandras
tothandras requested a review from a team as a code owner August 23, 2026 13:49
@coderabbitai

coderabbitai Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: c505ca92-67ce-477b-9d0a-44ddfcd08fab

📥 Commits

Reviewing files that changed from the base of the PR and between 323722a and 3f6b14a.

📒 Files selected for processing (3)
  • api/v3/handlers/addons/convert_test.go
  • api/v3/handlers/plans/convert_test.go
  • e2e/entitlement_usage_period_v3_test.go
💤 Files with no reviewable changes (2)
  • api/v3/handlers/addons/convert_test.go
  • api/v3/handlers/plans/convert_test.go

Included review availability: Your plan provides up to 8 included reviews per hour; 6 remain after this review.


📝 Walkthrough

Walkthrough

Metered entitlement conversion now defers missing usage period validation to the domain when billing cadence is unavailable. End-to-end tests verify HTTP 400 responses for plans and addons. The validation issue now includes HTTP status code 400.

Changes

Metered entitlement validation

Layer / File(s) Summary
Conversion and domain validation
api/v3/handlers/addons/convert.go, api/v3/handlers/plans/convert.go, api/v3/handlers/plans/convert_test.go, openmeter/productcatalog/errors.go
Plan and addon conversion infer usage_period from billing_cadence when available. Otherwise, conversion preserves a zero value for domain validation. The validation issue specifies HTTP 400.
End-to-end validation coverage
e2e/entitlement_usage_period_v3_test.go
End-to-end tests create plans and addons with metered entitlements that omit usage_period. The tests verify HTTP 400 responses and the entitlement_template_negative_usage_period validation code.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to 3f6b1

The change preserves the existing create-time rejection while returning the structured domain validation error, with focused converter, error-mapping, and end-to-end test updates. No actionable merge-blocking risk remains beyond normal checks and review.

Suggested reviewers: borbelyr-kong

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 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.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: deferring metered entitlement usage period handling to domain validation.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/metered-entitlement-usage-period-validation

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.

Replaces the converter unit subtests. The e2e run surfaced the real
behavior: the entitlement_template ent field validator runs
EntitlementTemplate.Validate severity-blind at insert, so the zero usage
period rejects create with a 400 carrying
entitlement_template_negative_usage_period rather than being stored as a
draft validation issue.

Claude-Session: https://claude.ai/code/session_01S93A9RigLLfBnXoauNNt7w
@tothandras tothandras added the release-note/bug-fix Release note: Bug Fixes label Aug 23, 2026
@tothandras
tothandras merged commit 9d9b529 into main Aug 24, 2026
28 of 29 checks passed
@tothandras
tothandras deleted the fix/metered-entitlement-usage-period-validation branch August 24, 2026 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants