Skip to content

feat: validate IM idempotency key length#1797

Merged
YangJunzhou-01 merged 1 commit into
larksuite:mainfrom
91-enjoy:feat/add_message_send_param_validate
Jul 13, 2026
Merged

feat: validate IM idempotency key length#1797
YangJunzhou-01 merged 1 commit into
larksuite:mainfrom
91-enjoy:feat/add_message_send_param_validate

Conversation

@91-enjoy

@91-enjoy 91-enjoy commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #1774 by adding client-side validation for IM --idempotency-key.

Previously, keys longer than the OpenAPI uuid limit were sent to the server and returned a generic field validation failed error. This change rejects overlong keys locally with a typed validation error that identifies
--idempotency-key and the 50-character limit.

Changes

  • Validate --idempotency-key length for:
    • im +messages-send
    • im +messages-reply
  • Return validation/invalid_argument with param: "--idempotency-key" when the key exceeds 50 characters.
  • Update help text and Lark IM reference docs to mention the 50-character limit.

Test Plan

  • go test ./shortcuts/im
  • Verified im +messages-send --dry-run rejects a 51-character --idempotency-key.
  • Verified im +messages-reply --dry-run rejects a 51-character --idempotency-key.
  • Verified --help shows idempotency key, max 50 characters.

Related Issues

close #1774

Summary by CodeRabbit

  • Bug Fixes
    • Added stricter validation for message send and reply idempotency keys.
    • Requests now fail fast when the idempotency key exceeds 50 characters.
  • Documentation
    • Updated command help text and reference docs to explicitly state a max 50-character limit for idempotency keys.
    • Kept the existing guidance about duplicate-prevention behavior within 1 hour.
  • Tests
    • Expanded validation coverage for empty, exactly 50 characters (including multi-byte), and over-maximum idempotency keys, including send and reply.

@github-actions github-actions Bot added domain/im PR touches the im domain size/M Single-domain feat or fix with limited business impact labels Jul 8, 2026
@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2c54cfbf-c97c-4146-8ab3-2a533d45d96a

📥 Commits

Reviewing files that changed from the base of the PR and between 9edad3e and 030b3be.

📒 Files selected for processing (5)
  • shortcuts/im/builders_test.go
  • shortcuts/im/im_messages_reply.go
  • shortcuts/im/im_messages_send.go
  • skills/lark-im/references/lark-im-messages-reply.md
  • skills/lark-im/references/lark-im-messages-send.md
🚧 Files skipped from review as they are similar to previous changes (5)
  • skills/lark-im/references/lark-im-messages-reply.md
  • skills/lark-im/references/lark-im-messages-send.md
  • shortcuts/im/builders_test.go
  • shortcuts/im/im_messages_send.go
  • shortcuts/im/im_messages_reply.go

📝 Walkthrough

Walkthrough

Adds 50-character validation for --idempotency-key in im messages-send and im messages-reply, returns field-specific validation errors, updates flag/help text and reference docs, and adds test coverage for valid and over-limit keys.

Changes

Idempotency Key Length Validation

Layer / File(s) Summary
messages-send validation helper and wiring
shortcuts/im/im_messages_send.go, skills/lark-im/references/lark-im-messages-send.md
Adds maxIdempotencyKeyChars and validateIdempotencyKey, reads idempotency-key in Validate, returns an invalid-argument error when the key is too long, and updates the send flag description and reference doc to state the 50-character limit.
messages-reply validation wiring
shortcuts/im/im_messages_reply.go, skills/lark-im/references/lark-im-messages-reply.md
Reads idempotency-key in Validate, calls validateIdempotencyKey, returns validation errors early, and updates the reply flag description and reference doc to state the 50-character limit.
validation coverage
shortcuts/im/builders_test.go
Adds tests for empty, boundary, and over-limit idempotency keys, plus send/reply validation cases that accept valid keys and reject keys longer than 50 characters.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested labels: enhancement

Suggested reviewers: yangjunzhou-01, haozhenghua-code, sammi-bytedance

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately describes the main change: IM idempotency key length validation.
Description check ✅ Passed The description matches the template with Summary, Changes, Test Plan, and Related Issues sections filled in.
Linked Issues check ✅ Passed The PR adds client-side validation for overlong IM idempotency keys and returns a clear field-specific error as requested in #1774.
Out of Scope Changes check ✅ Passed The code changes, tests, and docs all directly support the idempotency-key validation fix and do not appear unrelated.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@91-enjoy
91-enjoy force-pushed the feat/add_message_send_param_validate branch from d3c14e7 to 69ec416 Compare July 8, 2026 06:59

@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: 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 `@shortcuts/im/im_messages_send.go`:
- Around line 218-226: Add tests to cover the idempotency-key validation logic
in validateIdempotencyKey and the Validate methods for ImMessagesSend and
ImMessagesReply. Include boundary cases at exactly 50 characters and 51
characters, verify multi-byte runes are counted by characters rather than bytes,
and assert that Validate() rejects invalid idempotency keys in both message
builders.
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2ba27a9a-93e0-4b2e-b64c-1604bbb16bdd

📥 Commits

Reviewing files that changed from the base of the PR and between 1a9f637 and 69ec416.

📒 Files selected for processing (4)
  • shortcuts/im/im_messages_reply.go
  • shortcuts/im/im_messages_send.go
  • skills/lark-im/references/lark-im-messages-reply.md
  • skills/lark-im/references/lark-im-messages-send.md

Comment thread shortcuts/im/im_messages_send.go
@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown

🚀 PR Preview Install Guide

🧰 CLI update

npm i -g https://pkg.pr.new/larksuite/cli/@larksuite/cli@030b3be4a40486ec4314e6b45bf9fe523eeb327d

🧩 Skill update

npx skills add 91-enjoy/cli#feat/add_message_send_param_validate -y -g

@91-enjoy
91-enjoy force-pushed the feat/add_message_send_param_validate branch from 69ec416 to 9edad3e Compare July 8, 2026 07:53

@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)
shortcuts/im/builders_test.go (1)

519-524: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Assert typed error metadata in error-path tests, not just message substrings.

The new error-path test cases (validateIdempotencyKey 51 chars fails, validateIdempotencyKey 51 Chinese chars fails, ImMessagesSend idempotency key too long, ImMessagesReply idempotency key too long) only check strings.Contains(err.Error(), ...). Per coding guidelines, error-path tests must assert typed metadata via errs.ProblemOf (category / subtype) and Param, not message substrings alone.

Since errs.ProblemOf does not expose Param, use errors.As(err, &ve) with a *errs.ValidationError to assert ve.Param == "--idempotency-key". Based on learnings, errs.ProblemOf(err) returns (*errs.Problem, bool) containing only problem-level fields (Code, Hint, Subtype, Category, Retryable) — it does not have Param.

♻️ Proposed refactor for typed metadata assertions
 	t.Run("validateIdempotencyKey 51 chars fails", func(t *testing.T) {
 		err := validateIdempotencyKey(strings.Repeat("a", 51))
 		if err == nil || !strings.Contains(err.Error(), "--idempotency-key exceeds the maximum of 50 characters") {
 			t.Fatalf("validateIdempotencyKey() error = %v", err)
 		}
+		problem, ok := errs.ProblemOf(err)
+		if !ok || problem.Category != errs.CategoryValidation || problem.Subtype != errs.SubtypeInvalidArgument {
+			t.Fatalf("validateIdempotencyKey() expected validation/invalid_argument, got %+v", problem)
+		}
+		var ve *errs.ValidationError
+		if !errors.As(err, &ve) || ve.Param != "--idempotency-key" {
+			t.Fatalf("validateIdempotencyKey() expected param --idempotency-key, got %v", ve)
+		}
 	})

As per coding guidelines: "Error-path tests must assert typed metadata via errs.ProblemOf (category / subtype / param) and cause preservation, not message substrings alone."

Also applies to: 532-537, 539-549, 562-572

🤖 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 `@shortcuts/im/builders_test.go` around lines 519 - 524, The idempotency-key
error-path tests only assert message substrings, so update the affected tests
around validateIdempotencyKey, ImMessagesSend, and ImMessagesReply to assert
typed metadata instead. Use errs.ProblemOf to check the problem
category/subtype, and use errors.As with errs.ValidationError to verify the
Param is --idempotency-key; keep the existing failure cases but replace
substring-only checks with typed assertions and cause preservation.

Sources: Coding guidelines, Learnings

🤖 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 `@shortcuts/im/builders_test.go`:
- Around line 519-524: The idempotency-key error-path tests only assert message
substrings, so update the affected tests around validateIdempotencyKey,
ImMessagesSend, and ImMessagesReply to assert typed metadata instead. Use
errs.ProblemOf to check the problem category/subtype, and use errors.As with
errs.ValidationError to verify the Param is --idempotency-key; keep the existing
failure cases but replace substring-only checks with typed assertions and cause
preservation.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9d7126cc-5d61-4be3-8be3-941eb57697d5

📥 Commits

Reviewing files that changed from the base of the PR and between 69ec416 and 9edad3e.

📒 Files selected for processing (5)
  • shortcuts/im/builders_test.go
  • shortcuts/im/im_messages_reply.go
  • shortcuts/im/im_messages_send.go
  • skills/lark-im/references/lark-im-messages-reply.md
  • skills/lark-im/references/lark-im-messages-send.md
✅ Files skipped from review due to trivial changes (2)
  • skills/lark-im/references/lark-im-messages-send.md
  • skills/lark-im/references/lark-im-messages-reply.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • shortcuts/im/im_messages_reply.go
  • shortcuts/im/im_messages_send.go

@91-enjoy
91-enjoy force-pushed the feat/add_message_send_param_validate branch from 9edad3e to 030b3be Compare July 13, 2026 12:11
@YangJunzhou-01
YangJunzhou-01 merged commit 21bfa84 into larksuite:main Jul 13, 2026
33 checks passed
@liangshuo-1 liangshuo-1 mentioned this pull request Jul 13, 2026
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

domain/im PR touches the im domain size/M Single-domain feat or fix with limited business impact

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--idempotency-key 超 50 字符时错误信息不包含字段名和原因

2 participants