Skip to content

fix update commands to preserve full PUT payloads#20

Open
guoqqqi wants to merge 2 commits intomasterfrom
fix-partial-update-put-payloads
Open

fix update commands to preserve full PUT payloads#20
guoqqqi wants to merge 2 commits intomasterfrom
fix-partial-update-put-payloads

Conversation

@guoqqqi
Copy link
Copy Markdown
Contributor

@guoqqqi guoqqqi commented May 9, 2026

Summary

Fixes #18.

This PR changes flag-based update commands for resources whose API endpoints require full PUT payloads:

  • a7 credential update now reads the existing credential and preserves fields such as name and plugins unless the corresponding flags are provided.
  • a7 proto update now preserves existing content unless --content is provided.
  • a7 gateway-group update now preserves required fields such as name when only description, prefix, or labels are updated.

File-based update mode remains unchanged and continues to send the provided file payload directly.

Validation

  • go test ./pkg/cmd/credential/update ./pkg/cmd/proto/update ./pkg/cmd/gateway-group/update
  • go test ./...
  • Built the CLI and validated against a real API7 EE environment:
    • credential update --desc preserves existing plugins.
    • proto update --desc preserves existing content.
    • gateway-group update --description preserves existing name.

Follow-up

API capability/version boundary findings from the same live validation are tracked separately in #19.

Summary by CodeRabbit

  • Bug Fixes

    • Update commands for credentials, gateway groups, and protobufs now only change fields explicitly provided via CLI flags, preserving unspecified fields and allowing explicit clearing when a flag is set.
  • Tests

    • Enhanced unit tests to mock fetch-and-update flows and assert that updates preserve required/existing fields and apply only flagged changes.

Copilot AI review requested due to automatic review settings May 9, 2026 00:39
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 9, 2026

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 647e49d3-6957-40b7-a2de-c10aaab8523c

📥 Commits

Reviewing files that changed from the base of the PR and between e2794da and 0d77de2.

📒 Files selected for processing (2)
  • pkg/cmd/gateway-group/update/update.go
  • pkg/cmd/gateway-group/update/update_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/cmd/gateway-group/update/update_test.go

📝 Walkthrough

Walkthrough

Four update commands—credential, gateway-group, and proto—are refactored to track which CLI flags were explicitly provided, fetch current resource state, conditionally parse and merge only provided changes, and send full PUT payloads that preserve unmodified fields required by the API schema.

Changes

Partial Update Flag Mode Fix

Layer / File(s) Summary
Flag State Tracking
pkg/cmd/credential/update/update.go, pkg/cmd/gateway-group/update/update.go, pkg/cmd/proto/update/update.go
Options structs gain boolean fields (DescSet, PluginsSet/LabelsSet, NameSet, DescriptionSet, PrefixSet, ContentSet) to record whether each field-related CLI flag was explicitly provided.
Flag Detection Initialization
pkg/cmd/credential/update/update.go, pkg/cmd/gateway-group/update/update.go, pkg/cmd/proto/update/update.go
NewCmd sets boolean state indicators using Cobra's Flags().Changed(...) for each tracked field, enabling conditional logic downstream.
Core Update Flow
pkg/cmd/credential/update/update.go, pkg/cmd/gateway-group/update/update.go, pkg/cmd/proto/update/update.go
Each command now parses labels/plugins only when their flags were set, fetches current resource state via GET, merges only provided changes into the existing payload, and sends the full merged payload via PUT.
Tests / Validation
pkg/cmd/credential/update/update_test.go, pkg/cmd/gateway-group/update/update_test.go, pkg/cmd/proto/update/update_test.go
Tests register GET mocks for current state and PUT responders that read/unmarshal the request body to assert unchanged required fields are preserved while provided fields are updated. A new gateway-group test validates required-field preservation.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes


Important

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
E2e Test Quality Review ⚠️ Warning Tests use HTTP mocks, not E2E tests. Custom check requires full business flow (API→Service→DB). Only unit tests provided. Insufficient scenario coverage for credential/proto. Add real E2E tests against API7 EE. Test boundary cases: empty values, invalid JSON. Add error scenarios: malformed JSON, invalid label format. Cover all flag combinations.
Security Check ❓ Inconclusive No result was produced after verification. Marking as INCONCLUSIVE. Re-run the check or adjust instructions to produce a final result.
✅ 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 'fix update commands to preserve full PUT payloads' directly describes the main change: modifying update commands to read existing resources and preserve full payloads in PUT requests.
Linked Issues check ✅ Passed The PR fully addresses issue #18 by implementing partial update behavior for credential, proto, and gateway-group commands, with unit tests validating preserved fields.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the three affected update commands (credential, proto, gateway-group) to preserve existing fields and required fields in PUT payloads.
✨ 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 fix-partial-update-put-payloads

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 and usage tips.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes flag-based update commands that previously sent partial PUT payloads to API endpoints that require full resource replacement, by first reading the current resource and then merging only the explicitly-provided flags before issuing the PUT.

Changes:

  • proto update, credential update, and gateway-group update (flag mode) now GET the current resource and preserve unspecified fields when building the PUT payload.
  • Introduces *Set booleans (based on cobra.Flags().Changed(...)) to distinguish “flag not provided” vs “provided empty value”.
  • Adds/updates unit tests to assert preservation of required/existing fields in the generated PUT payload.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
pkg/cmd/proto/update/update.go Fetches current proto and merges only changed flag fields before PUT to preserve required/existing fields.
pkg/cmd/proto/update/update_test.go Updates test to validate PUT payload preserves content unless --content is explicitly set.
pkg/cmd/gateway-group/update/update.go Fetches current gateway group and merges only changed flag fields before PUT to preserve required name.
pkg/cmd/gateway-group/update/update_test.go New test ensuring required fields (e.g., name) are preserved when updating description/labels.
pkg/cmd/credential/update/update.go Fetches current credential and merges only changed flag fields before PUT to preserve name/plugins.
pkg/cmd/credential/update/update_test.go Updates tests to validate PUT payload preserves existing name/plugins and adds required GET mock for error-path test.

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

Comment thread pkg/cmd/gateway-group/update/update.go Outdated
Comment on lines +111 to +115
var request api.GatewayGroup
if err := json.Unmarshal(currentBody, &request); err != nil {
return fmt.Errorf("failed to decode current gateway group: %w", err)
}
request.ID = opts.ID
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.

Fix partial update flag mode for resources that require full PUT payloads

2 participants