Skip to content

feat: unit config authoring on v3 api - OM-396 - #4604

Merged
rolosp merged 2 commits into
mainfrom
feat/om-396-writable-unit-config-on-v3
Jun 30, 2026
Merged

rolosp merged 2 commits into
mainfrom
feat/om-396-writable-unit-config-on-v3

Conversation

@rolosp

@rolosp rolosp commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Wires unit_config through the v3 plan create/update path, all gated behind the dark unitConfig.enabled flag, plus a fix for the OM-395 adapter mappers that dropped unit_config on expanded plan/add-on relations.

Note to reviewers

  • the v1 read-side coexistence gap (a v3-authored unit_config silently dropped when listed on v1) is a known and is deferred to a later ticket

Summary by CodeRabbit

  • New Features
    • Added explicit unit configuration support for v3 rate-card conversions, including new bidirectional unit-config conversion helpers.
  • Bug Fixes
    • Preserved stored unit configuration when converting and mapping plan/addon rate-cards into product-catalog models.
    • Enforced that unit configuration is only valid with usage-based or tiered price types; invalid requests now return a dedicated “unit config requires usage-based price” validation error.
    • Improved conversion-factor handling by surfacing errors for non-numeric values and aligning precision/rounding behavior.
  • Tests
    • Added/extended unit-config mapping, round-trip, and validation coverage across v3 conversions and adapter mappings.

@rolosp
rolosp requested a review from a team as a code owner June 29, 2026 16:06
@coderabbitai

coderabbitai Bot commented Jun 29, 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

Run ID: b2be27a2-15b8-4dbf-8695-d15b20fa39f8

📥 Commits

Reviewing files that changed from the base of the PR and between a7698d8 and 2614d7e.

📒 Files selected for processing (9)
  • api/v3/handlers/plans/convert.go
  • api/v3/handlers/plans/convert_test.go
  • openmeter/productcatalog/addon/adapter/adapter_test.go
  • openmeter/productcatalog/addon/adapter/mapping.go
  • openmeter/productcatalog/errors.go
  • openmeter/productcatalog/plan/adapter/adapter_test.go
  • openmeter/productcatalog/plan/adapter/mapping.go
  • openmeter/productcatalog/ratecard.go
  • openmeter/productcatalog/ratecard_test.go
🚧 Files skipped from review as they are similar to previous changes (9)
  • openmeter/productcatalog/addon/adapter/mapping.go
  • openmeter/productcatalog/plan/adapter/mapping.go
  • openmeter/productcatalog/plan/adapter/adapter_test.go
  • openmeter/productcatalog/errors.go
  • openmeter/productcatalog/ratecard.go
  • openmeter/productcatalog/addon/adapter/adapter_test.go
  • api/v3/handlers/plans/convert.go
  • api/v3/handlers/plans/convert_test.go
  • openmeter/productcatalog/ratecard_test.go

📝 Walkthrough

Walkthrough

Adds bidirectional UnitConfig conversion for billing rate cards, preserves stored UnitConfig during API conversion, propagates it through plan/addon adapter mappings, and adds validation that rejects UnitConfig on non-usage-based prices.

Changes

UnitConfig propagation and validation

Layer / File(s) Summary
Validation error and price-type check
openmeter/productcatalog/errors.go, openmeter/productcatalog/ratecard.go, openmeter/productcatalog/ratecard_test.go
Adds a new validation issue for unit_config and enforces that it only passes validation with unit-based or tiered prices, with tests covering accepted and rejected cases.
API and domain UnitConfig conversion
api/v3/handlers/plans/convert.go, api/v3/handlers/plans/convert_test.go
Adds ToAPIBillingUnitConfig and FromAPIBillingUnitConfig, changes rate-card conversion to preserve stored UnitConfig, and adds tests for mapping, defaults, errors, and round-trips.
Plan and addon adapter UnitConfig propagation
openmeter/productcatalog/plan/adapter/mapping.go, openmeter/productcatalog/plan/adapter/adapter_test.go, openmeter/productcatalog/addon/adapter/mapping.go, openmeter/productcatalog/addon/adapter/adapter_test.go
Copies UnitConfig from Ent rate-card rows into RateCardMeta for both addon and plan mappings, with tests confirming the value is retained.

Sequence Diagram(s)

sequenceDiagram
  participant Handler
  participant FromAPIBillingRateCard
  participant FromAPIBillingUnitConfig
  participant ToAPIBillingRateCard
  participant ToAPIBillingUnitConfig
  Handler->>FromAPIBillingRateCard: rc with UnitConfig
  FromAPIBillingRateCard->>FromAPIBillingUnitConfig: api.BillingUnitConfig
  FromAPIBillingUnitConfig-->>FromAPIBillingRateCard: *productcatalog.UnitConfig
  FromAPIBillingRateCard-->>Handler: meta.UnitConfig set
  Handler->>ToAPIBillingRateCard: domain RateCard
  ToAPIBillingRateCard->>ToAPIBillingUnitConfig: productcatalog.UnitConfig (if stored)
  ToAPIBillingUnitConfig-->>ToAPIBillingRateCard: api.BillingUnitConfig
  ToAPIBillingRateCard-->>Handler: API rate card with UnitConfig
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • openmeterio/openmeter#4309: Modifies the same api/v3/handlers/plans/convert.go path around v3 unit_config synthesis and conversion behavior.
  • openmeterio/openmeter#4331: Touches RateCardMeta.Validate() around usage-based rate-card validation, which is the same validation surface this PR extends.
  • openmeterio/openmeter#4560: Changes the same v3 plan conversion flow for BillingRateCard.UnitConfig, making it directly adjacent to this PR’s mapping updates.

Suggested labels

area/billing

Suggested reviewers

  • tothandras
  • chrisgacsal
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding unit config authoring support in the v3 API.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/om-396-writable-unit-config-on-v3

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.

@greptile-apps

greptile-apps Bot commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds unit config support to v3 plan authoring and related product catalog mappings. The main changes are:

  • v3 rate-card conversion now reads and writes stored unit_config.
  • API and domain unit config helpers were added.
  • Plan and add-on adapter mappers now preserve unit config on expanded relations.
  • Product catalog validation now rejects incompatible unit config price shapes at publish validation.
  • Unit tests cover conversion, validation, and mapper preservation.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

Filename Overview
api/v3/handlers/plans/convert.go Adds stored unit config handling and API/domain conversion for v3 rate cards.
openmeter/productcatalog/ratecard.go Adds unit config price-shape validation for rate-card metadata.
openmeter/productcatalog/plan/adapter/mapping.go Preserves unit config when mapping expanded add-on rate cards.
openmeter/productcatalog/addon/adapter/mapping.go Preserves unit config when mapping expanded plan rate cards.

Reviews (3): Last reviewed commit: "feat: unit config authoring on v3" | Re-trigger Greptile

@rolosp

rolosp commented Jun 29, 2026

Copy link
Copy Markdown
Contributor Author

As the feature is still work in progress and behind a config flag, for now I relied mostly on unit test coverage but for sanity check I created an e2e test with hurl and tested that the v3 unit config authoring works e2e.

@rolosp rolosp added the release-note/feature Release note: Exciting New Features label Jun 29, 2026

@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.

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/v3/handlers/plans/convert_test.go`:
- Around line 729-836: Add a write-path test for FromAPIBillingRateCard in
TestUnitConfigMapping so the v3 authoring flow is covered, not just the helper
round-trips and ToAPIBillingRateCard. Create a subtest that builds an
api.BillingRateCard with unit_config, calls FromAPIBillingRateCard, and asserts
the resulting meta via AsMeta() still contains the same UnitConfig fields. This
directly verifies the copy behavior in FromAPIBillingRateCard and will catch
regressions if rc.UnitConfig is no longer propagated into meta.UnitConfig.
🪄 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: f2738161-fd96-43fb-aadb-2861be9b01da

📥 Commits

Reviewing files that changed from the base of the PR and between eaeb4d5 and 33872c6.

📒 Files selected for processing (9)
  • api/v3/handlers/plans/convert.go
  • api/v3/handlers/plans/convert_test.go
  • openmeter/productcatalog/addon/adapter/adapter_test.go
  • openmeter/productcatalog/addon/adapter/mapping.go
  • openmeter/productcatalog/errors.go
  • openmeter/productcatalog/plan/adapter/adapter_test.go
  • openmeter/productcatalog/plan/adapter/mapping.go
  • openmeter/productcatalog/ratecard.go
  • openmeter/productcatalog/ratecard_test.go

Comment thread api/v3/handlers/plans/convert_test.go Resolved
@rolosp
rolosp force-pushed the feat/om-396-writable-unit-config-on-v3 branch from 33872c6 to a7698d8 Compare June 29, 2026 17:19
Comment on lines +304 to +308
priceAllowsUnitConfig := r.Price != nil &&
(r.Price.Type() == UnitPriceType || r.Price.Type() == TieredPriceType)
if !priceAllowsUnitConfig {
errs = append(errs, ErrRateCardUnitConfigRequiresUsageBasedPrice)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I can imagine that this might be used at other places, so it might worth just adding a SupportsUnitConfig() (bool) to the Price interface, that delegates it to the downstream pricers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good idea, i'll add it to the next pr

@rolosp
rolosp force-pushed the feat/om-396-writable-unit-config-on-v3 branch from a7698d8 to 2614d7e Compare June 30, 2026 08:10
@rolosp
rolosp merged commit 9c90d9b into main Jun 30, 2026
26 checks passed
@rolosp
rolosp deleted the feat/om-396-writable-unit-config-on-v3 branch June 30, 2026 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/feature Release note: Exciting New Features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants