Skip to content

fix(api): use external key format for customer routes - #3078

Merged
tothandras merged 1 commit into
mainfrom
feat/customer-key-fix
Jul 8, 2025
Merged

tothandras merged 1 commit into
mainfrom
feat/customer-key-fix

Conversation

@tothandras

@tothandras tothandras commented Jul 8, 2025 •

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Introduced a unified identifier type allowing both ULIDs and external keys for customer-related API endpoints.
    • Improved validation and documentation for path parameters accepting either a ULID or an external key across the API.
  • Refactor

    • Standardized and centralized the schema for identifiers, reducing duplication and enhancing clarity in API specifications and validation logic.

@tothandras tothandras added the release-note/bug-fix Release note: Bug Fixes label Jul 8, 2025
@coderabbitai

coderabbitai Bot commented Jul 8, 2025 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

A new type ULIDOrExternalKey is introduced across the API specification, type definitions, and validation schemas to standardize identifiers that can be either a ULID or an external key. This type replaces previous inline or less specific definitions for customer and related path parameters, updating OpenAPI, TypeScript, and Zod validation logic accordingly.

Changes

Files/Paths Change Summary
api/spec/src/types.tsp Added ULIDOrExternalKey union type (ULID or ExternalKey) with Go type annotation.
api/spec/src/customer.tsp Updated all customer-related method signatures to use ULIDOrExternalKey for customerIdOrKey.
api/openapi.yaml
api/openapi.cloud.yaml
Added ULIDOrExternalKey schema; replaced multiple inline string schemas with this reference for relevant path parameters.
api/client/javascript/src/client/schemas.ts Added ULIDOrExternalKey schema/type; updated customer-related API parameter types to use it.
api/client/javascript/src/zod/index.ts Updated Zod validation: split ULID and external key logic; increased external key max length to 256; adjusted constants and descriptions.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Biome (1.9.4)
api/client/javascript/src/zod/index.ts

The --json option is unstable/experimental and its output might change between patches/minor releases.
{"summary":{"changed":0,"unchanged":1,"matches":0,"duration":{"secs":6,"nanos":367023857},"errors":475,"warnings":0,"skipped":0,"suggestedFixesSkipped":0,"diagnosticsNotPrinted":0},"diagnostics":[{"category":"lint/complexity/useRegexLiterals","severity":"error","description":"Use a regular expression literal instead of the RegExp constructor.","message":[{"elements":[],"content":"Use a regular expression literal instead of the "},{"elements":["Emphasis"],"content":"RegExp"},{"elements":[],"content":" constructor."}],"advices":{"advices":[{"log":["info",[{"elements":[],"content":"Regular expression literals avoid some escaping required in a string literal, and are easier to analyze statically."}]]},{"log":["info",[{"elements":[],"content":"Safe fix: Use a "},{"elements":["Emphasis"],"content":"literal notation"},{"elements":[],"content":" instead."}]]},{"diff":{"dictionary":"/* eslint-

... [truncated 99999042 characters] ...

\n 'Numeric represents an arbitrary precision number.'\n )\n .describe(\n 'The quantity of the usage discount.\n\nMust be positive.'\n ),\n })\n Biome encountered an unexpected error

This is a bug in Biome, not an error in your code, and we would appreciate it if you could report it to https://github.com/biomejs/biome/issues/ along with the following information to help us fixing the issue:

Source Location: crates/biome_console/src/lib.rs:149:14
Thread Name: main
Message: called Result::unwrap() on an Err value: Os { code: 32, kind: BrokenPipe, message: "Broken pipe" }

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@tothandras
tothandras force-pushed the feat/customer-key-fix branch from ba38cfd to 59a979b Compare July 8, 2025 10:55
@tothandras
tothandras marked this pull request as ready for review July 8, 2025 10:55
@tothandras
tothandras requested a review from a team as a code owner July 8, 2025 10:55

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

♻️ Duplicate comments (6)
api/openapi.yaml (6)

3109-3113: Same remark as above – reuse of the central schema is correct.


3182-3186: Same remark as above – reuse of the central schema is correct.


3318-3324: Same remark as above – reuse of the central schema is correct.


3388-3392: Same remark as above – reuse of the central schema is correct.


3466-3472: Same remark as above – reuse of the central schema is correct.


3625-3631: Same remark as above – reuse of the central schema is correct.

🧹 Nitpick comments (3)
api/openapi.yaml (1)

22488-22499: Tighten external-key specification & add example

The union schema definition works, but the “external key” branch is very permissive and lacks an example.
Consider adding a simple pattern (alphanumerics + _ . -) so accidental whitespace or path-breaking characters are caught early.

         - type: string
+          pattern: '^[A-Za-z0-9._-]{1,256}$'
           minLength: 1
           maxLength: 256
           description: ExternalKey is a looser version of key.
+          example: merchant_123ABC

This leaves plenty of freedom while still preventing obvious mistakes.

api/openapi.cloud.yaml (1)

21562-21573: Add format & examples to ULIDOrExternalKey for better tooling support

Nice union schema. Two small, non-blocking tweaks could improve generated SDKs and docs:

  1. Declare a format for the ULID branch (format: ulid); many generators surface it for validation.
  2. Provide an example for the external-key branch (example: ext-customer-42) so docs don’t only show the ULID variant.
  3. Optional: give each branch a title to make swagger-ui labels clearer.
       - type: string
         pattern: ^[0-7][0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{25}$
+        format: ulid
+        title: ULID
         description: ULID (Universally Unique Lexicographically Sortable Identifier).
         example: 01G65Z755AFWAKHE12NY0CQ9FH
       - type: string
         minLength: 1
         maxLength: 256
+        title: ExternalKey
         description: ExternalKey is a looser version of key.
+        example: ext-customer-42

These tweaks are optional but help downstream consumers.

api/client/javascript/src/zod/index.ts (1)

6770-6788: Refactor to eliminate code duplication across validation schemas.

The validation logic is correct and follows the proper pattern for ULIDOrExternalKey union types. However, the same regex pattern and max length constants are duplicated across all customer operations.

Consider creating a reusable helper function or shared constants:

+// Shared constants for customer ID validation
+export const CUSTOMER_ID_ULID_REGEX = new RegExp(
+  '^[0-7][0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{25}$'
+)
+export const CUSTOMER_EXTERNAL_KEY_MAX_LENGTH = 256
+
+// Helper function for customer ID validation
+export const createCustomerIdValidation = () => 
+  zod.coerce
+    .string()
+    .regex(CUSTOMER_ID_ULID_REGEX)
+    .describe('ULID (Universally Unique Lexicographically Sortable Identifier).')
+    .or(
+      zod.coerce
+        .string()
+        .min(1)
+        .max(CUSTOMER_EXTERNAL_KEY_MAX_LENGTH)
+        .describe('ExternalKey is a looser version of key.')
+    )

// Then use in each operation:
-export const getCustomerPathCustomerIdOrKeyRegExpOne = new RegExp(
-  '^[0-7][0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{25}$'
-)
-export const getCustomerPathCustomerIdOrKeyMaxTwo = 256
-
 export const getCustomerParams = zod.object({
-  customerIdOrKey: zod.coerce
-    .string()
-    .regex(getCustomerPathCustomerIdOrKeyRegExpOne)
-    .describe(
-      'ULID (Universally Unique Lexicographically Sortable Identifier).'
-    )
-    .or(
-      zod.coerce
-        .string()
-        .min(1)
-        .max(getCustomerPathCustomerIdOrKeyMaxTwo)
-        .describe('ExternalKey is a looser version of key.')
-    ),
+  customerIdOrKey: createCustomerIdValidation(),
 })

Also applies to: 6808-6826, 6944-6962, 6987-7005, 7034-7052, 7389-7411, 7450-7468

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8ae5364 and 59a979b.

📒 Files selected for processing (6)
  • api/client/javascript/src/client/schemas.ts (9 hunks)
  • api/client/javascript/src/zod/index.ts (8 hunks)
  • api/openapi.cloud.yaml (8 hunks)
  • api/openapi.yaml (8 hunks)
  • api/spec/src/customer.tsp (7 hunks)
  • api/spec/src/types.tsp (1 hunks)
🧰 Additional context used
🧠 Learnings (7)
📓 Common learnings
Learnt from: chrisgacsal
PR: openmeterio/openmeter#2522
File: api/spec/src/productcatalog/routes.tsp:163-314
Timestamp: 2025-03-25T09:13:03.300Z
Learning: ULIDOrKey is defined in api/spec/src/types.tsp as a scalar type with a pattern that validates against both ULID and Key formats, not as a type alias like PlanIdOrKey.
api/spec/src/types.tsp (1)
Learnt from: chrisgacsal
PR: openmeterio/openmeter#2522
File: api/spec/src/productcatalog/routes.tsp:163-314
Timestamp: 2025-03-25T09:13:03.300Z
Learning: ULIDOrKey is defined in api/spec/src/types.tsp as a scalar type with a pattern that validates against both ULID and Key formats, not as a type alias like PlanIdOrKey.
api/openapi.yaml (1)

undefined

<retrieved_learning>
Learnt from: chrisgacsal
PR: #2522
File: api/spec/src/productcatalog/routes.tsp:163-314
Timestamp: 2025-03-25T09:13:03.300Z
Learning: ULIDOrKey is defined in api/spec/src/types.tsp as a scalar type with a pattern that validates against both ULID and Key formats, not as a type alias like PlanIdOrKey.
</retrieved_learning>

api/openapi.cloud.yaml (1)

undefined

<retrieved_learning>
Learnt from: chrisgacsal
PR: #2522
File: api/spec/src/productcatalog/routes.tsp:163-314
Timestamp: 2025-03-25T09:13:03.300Z
Learning: ULIDOrKey is defined in api/spec/src/types.tsp as a scalar type with a pattern that validates against both ULID and Key formats, not as a type alias like PlanIdOrKey.
</retrieved_learning>

api/client/javascript/src/client/schemas.ts (1)

undefined

<retrieved_learning>
Learnt from: chrisgacsal
PR: #2522
File: api/spec/src/productcatalog/routes.tsp:163-314
Timestamp: 2025-03-25T09:13:03.300Z
Learning: ULIDOrKey is defined in api/spec/src/types.tsp as a scalar type with a pattern that validates against both ULID and Key formats, not as a type alias like PlanIdOrKey.
</retrieved_learning>

api/client/javascript/src/zod/index.ts (1)

undefined

<retrieved_learning>
Learnt from: chrisgacsal
PR: #2522
File: api/spec/src/productcatalog/routes.tsp:163-314
Timestamp: 2025-03-25T09:13:03.300Z
Learning: ULIDOrKey is defined in api/spec/src/types.tsp as a scalar type with a pattern that validates against both ULID and Key formats, not as a type alias like PlanIdOrKey.
</retrieved_learning>

api/spec/src/customer.tsp (1)
Learnt from: chrisgacsal
PR: openmeterio/openmeter#2522
File: api/spec/src/productcatalog/routes.tsp:163-314
Timestamp: 2025-03-25T09:13:03.300Z
Learning: ULIDOrKey is defined in api/spec/src/types.tsp as a scalar type with a pattern that validates against both ULID and Key formats, not as a type alias like PlanIdOrKey.
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: Migration Checks
  • GitHub Check: Lint
  • GitHub Check: E2E
  • GitHub Check: CI
  • GitHub Check: Developer environment
  • GitHub Check: Quickstart
  • GitHub Check: Commit hooks
  • GitHub Check: Build
  • GitHub Check: Test
  • GitHub Check: Analyze (go)
🔇 Additional comments (8)
api/spec/src/types.tsp (1)

42-51: LGTM! Well-structured union type for flexible customer identification.

The new ULIDOrExternalKey union type provides a cleaner and more flexible approach compared to the existing ULIDOrKey scalar type. This design allows for:

  • Type-safe distinction between ULID and ExternalKey
  • More permissive external key validation (256 chars vs 64 chars for Key)
  • Better code generation with explicit union handling

The TypeSpec syntax, annotations, and documentation are all properly implemented.

api/openapi.yaml (1)

3041-3046: Consistent identifier type 👍

Switching the customerId path parameter to the new $ref: '#/components/schemas/ULIDOrExternalKey' nicely centralises validation logic and removes duplicated regexes.
Just make sure any remaining ULIDOrKey / inline-regex usages were swept as well; otherwise generators may emit multiple, incompatible types.

api/client/javascript/src/client/schemas.ts (4)

9840-9841: LGTM: Well-documented schema definition

The new ULIDOrExternalKey schema type is properly defined with clear documentation explaining its purpose as a union type for identifiers.


10872-10872: LGTM: Consistent type export naming

The type export follows TypeScript conventions with UlidOrExternalKey (camelCase) for the exported type while maintaining consistency with the schema definition.


14895-14895: LGTM: Consistent application across customer operations

All customer-related API operations (getCustomer, updateCustomer, deleteCustomer, listCustomerAppData, upsertCustomerAppData, deleteCustomerAppData, getCustomerAccess, listCustomerSubscriptions) have been consistently updated to use the new ULIDOrExternalKey schema type for the customerIdOrKey path parameter.

Also applies to: 14989-14989, 15087-15087, 15284-15284, 15378-15378, 15476-15476, 15675-15675


9840-9841: Ignore Zod validation alignment suggestion

The JavaScript client doesn’t include any Zod‐based validators—schemas.ts only defines TypeScript types, and all OpenAPI YAML schemas for ULIDOrExternalKey have been updated correctly. You can safely disregard the recommendation to align a non‐existent Zod layer here.

Likely an incorrect or invalid review comment.

api/client/javascript/src/zod/index.ts (2)

6770-6771: ULID regex pattern is correct.

The regex ^[0-7][0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{25}$ correctly validates ULIDs:

  • 26 total characters (1 + 25)
  • Uses Crockford's Base32 alphabet
  • First character restricted to 0-7 for timestamp encoding

Also applies to: 6808-6809, 6944-6945, 6987-6988, 7034-7035, 7389-7390, 7450-7451


6784-6788: External key validation is appropriate.

The validation allowing 1-256 characters for external keys provides good flexibility while preventing empty strings and overly long identifiers.

Also applies to: 6822-6826, 6958-6962, 7001-7005, 7048-7052, 7407-7411, 7464-7468

Comment thread api/openapi.cloud.yaml Resolved
Comment thread api/spec/src/customer.tsp Resolved
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