Skip to content

fix(schema): add team/owner rate-limit fields to apikey JSON schema - #269

Merged
nic-6443 merged 2 commits into
mainfrom
fix/apikey-schema-missing-fields
May 13, 2026
Merged

fix(schema): add team/owner rate-limit fields to apikey JSON schema#269
nic-6443 merged 2 commits into
mainfrom
fix/apikey-schema-missing-fields

Conversation

@jarvis9443

@jarvis9443 jarvis9443 commented May 13, 2026

Copy link
Copy Markdown
Contributor

PR #267 added team_id, team_rate_limit, owner_id, and owner_rate_limit to the ApiKey Rust struct but missed updating the JSON schema validator in schema.rs. Since the apikey schema uses additionalProperties: false, any payload with these fields gets rejected at validation time before serde deserialization.

This adds the four fields to the apikey JSON schema and updates the unknown-field test to use a truly unknown field name (bogus_field) instead of max_budget_usd (dropped in #259).

Blocks api7/AISIX-Cloud#271 (CP e2e fails because the dev image rejects owner_id).

Summary by CodeRabbit

  • New Features

    • API key schema now accepts optional team and owner identifiers plus optional team and owner rate limit settings.
  • Tests

    • Validation tests updated to cover API keys with the new team/owner fields and to ensure unknown fields are rejected.

Review Change Stack

PR #267 added team_id, team_rate_limit, owner_id, and owner_rate_limit
to the ApiKey Rust struct but did not update the JSON schema validator.
Since the schema uses additionalProperties: false, any payload containing
these fields is rejected at schema validation time before serde even sees it.

Also updates the unknown-field test to use a truly unknown field name
instead of max_budget_usd (which was dropped in #259).
Copilot AI review requested due to automatic review settings May 13, 2026 12:28
@coderabbitai

coderabbitai Bot commented May 13, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The PR extends the API key schema in crates/aisix-core/src/models/schema.rs to support optional team and owner identifiers with associated rate limits. The schema now accepts team_id, team_rate_limit, owner_id, and owner_rate_limit fields, with both rate limit fields validated against the shared rate_limit definition. Test coverage includes validation of payloads with these new fields and enforcement of the schema's unknown field rejection.

Changes

API Key Schema Extension

Layer / File(s) Summary
API key schema extended with team and owner rate limits
crates/aisix-core/src/models/schema.rs
apikey_schema properties extended to include optional team_id, team_rate_limit, owner_id, and owner_rate_limit fields. Added test apikey_with_team_and_owner_fields_passes to validate the new fields, and updated apikey_unknown_field_rejected to verify proper schema field enforcement.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • api7/ai-gateway-stash#64: Modifies API key JSON schema validation tests around rate_limit and apikey payload field acceptance.
🚥 Pre-merge checks | ✅ 4
✅ 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 accurately summarizes the main change: adding team/owner rate-limit fields to the apikey JSON schema, which is the primary purpose of this PR.
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.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

🧹 Nitpick comments (1)
crates/aisix-core/src/models/schema.rs (1)

201-204: ⚡ Quick win

Consider adding minLength: 1 validation to ID fields.

The team_id and owner_id fields accept any string, including empty strings. Other identifier fields in this schema (e.g., key_hash at line 195) and across other schemas (e.g., display_name, model_name, provider_key_id at lines 111-114) enforce minLength: 1 to prevent empty values. Empty identifiers are semantically invalid and could cause issues downstream.

📏 Proposed validation addition
-            "team_id": { "type": "string" },
+            "team_id": { "type": "string", "minLength": 1 },
             "team_rate_limit": { "$ref": "#/$defs/rate_limit" },
-            "owner_id": { "type": "string" },
+            "owner_id": { "type": "string", "minLength": 1 },
             "owner_rate_limit": { "$ref": "#/$defs/rate_limit" }
🤖 Prompt for 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.

In `@crates/aisix-core/src/models/schema.rs` around lines 201 - 204, Add non-empty
string validation to the ID fields by adding minLength: 1 to the "team_id" and
"owner_id" entries in the schema so they cannot be empty; locate the definitions
for "team_id" and "owner_id" in the struct/schema (the entries currently are
"team_id": { "type": "string" } and "owner_id": { "type": "string" }) and change
each to include "minLength": 1 consistent with other identifiers like "key_hash"
and "display_name".
🤖 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.

Nitpick comments:
In `@crates/aisix-core/src/models/schema.rs`:
- Around line 201-204: Add non-empty string validation to the ID fields by
adding minLength: 1 to the "team_id" and "owner_id" entries in the schema so
they cannot be empty; locate the definitions for "team_id" and "owner_id" in the
struct/schema (the entries currently are "team_id": { "type": "string" } and
"owner_id": { "type": "string" }) and change each to include "minLength": 1
consistent with other identifiers like "key_hash" and "display_name".

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d672ddb2-ff5c-4b31-a4ad-ba04d11ff37d

📥 Commits

Reviewing files that changed from the base of the PR and between bd9617c and 070f6c0.

📒 Files selected for processing (1)
  • crates/aisix-core/src/models/schema.rs

Copilot AI 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.

Pull request overview

Fixes ApiKey JSON schema validation in aisix-core to accept the team_id/owner_id and corresponding *_rate_limit fields introduced in #267. This is necessary because the apikey schema uses additionalProperties: false, so previously-valid payloads were being rejected before serde deserialization.

Changes:

  • Extended the apikey JSON schema to include team_id, team_rate_limit, owner_id, and owner_rate_limit.
  • Added a unit test asserting apikey payloads with the new fields validate successfully.
  • Updated the unknown-field schema test to use bogus_field instead of max_budget_usd (removed in #259).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/aisix-core/src/models/schema.rs Outdated
Comment thread crates/aisix-core/src/models/schema.rs
Prevents empty-string IDs from creating shared limiter buckets.

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

🧹 Nitpick comments (1)
crates/aisix-core/src/models/schema.rs (1)

512-523: ⚡ Quick win

Add explicit regression tests for empty team_id / owner_id.

You added minLength: 1 constraints, but there’s no test that proves empty strings are rejected. Add one to lock in the limiter-bucket safety guarantee.

✅ Suggested test addition
+    #[test]
+    fn apikey_empty_team_or_owner_id_rejected() {
+        let with_empty_team = json!({
+            "key_hash":"9df37f5e7cbc3c391d872742b5f286c242e733a09add9eeaa4d26a599bd90b20",
+            "allowed_models":["gpt-4o"],
+            "team_id": "",
+            "owner_id": "member-uuid-1"
+        });
+        assert!(validate_apikey(&with_empty_team).is_err());
+
+        let with_empty_owner = json!({
+            "key_hash":"9df37f5e7cbc3c391d872742b5f286c242e733a09add9eeaa4d26a599bd90b20",
+            "allowed_models":["gpt-4o"],
+            "team_id": "team-uuid-1",
+            "owner_id": ""
+        });
+        assert!(validate_apikey(&with_empty_owner).is_err());
+    }
🤖 Prompt for 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.

In `@crates/aisix-core/src/models/schema.rs` around lines 512 - 523, Add
regression tests that assert empty "team_id" and empty "owner_id" are rejected
by the schema: create two new tests (e.g., apikey_with_empty_team_id_fails and
apikey_with_empty_owner_id_fails) that build the same JSON shape used in
apikey_with_team_and_owner_fields_passes but set "team_id": "" and "owner_id":
"" respectively, then call validate_apikey(&v) and assert it returns an error
(use unwrap_err() or assert!(validate_apikey(&v).is_err())). This ensures the
minLength: 1 constraint enforced by validate_apikey fails for empty strings.
🤖 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.

Nitpick comments:
In `@crates/aisix-core/src/models/schema.rs`:
- Around line 512-523: Add regression tests that assert empty "team_id" and
empty "owner_id" are rejected by the schema: create two new tests (e.g.,
apikey_with_empty_team_id_fails and apikey_with_empty_owner_id_fails) that build
the same JSON shape used in apikey_with_team_and_owner_fields_passes but set
"team_id": "" and "owner_id": "" respectively, then call validate_apikey(&v) and
assert it returns an error (use unwrap_err() or
assert!(validate_apikey(&v).is_err())). This ensures the minLength: 1 constraint
enforced by validate_apikey fails for empty strings.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 966a762f-7166-42e4-b292-4d52be9c12a7

📥 Commits

Reviewing files that changed from the base of the PR and between 070f6c0 and 8b7428a.

📒 Files selected for processing (1)
  • crates/aisix-core/src/models/schema.rs

@nic-6443
nic-6443 merged commit a757fa9 into main May 13, 2026
7 checks passed
@nic-6443
nic-6443 deleted the fix/apikey-schema-missing-fields branch May 13, 2026 13:40
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.

3 participants