-
Notifications
You must be signed in to change notification settings - Fork 1
Fix metadata loss in AgentCard legacy capability unmarshaling #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package a2a | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/techbuzzz/agent-shaker/internal/a2a/models" | ||
| ) | ||
|
|
||
| func TestAgentCardUnmarshal_MetadataPreservation(t *testing.T) { | ||
| // Test that legacy capabilities are preserved in metadata after struct assignment | ||
| jsonData := `{ | ||
| "name": "Test Agent", | ||
| "version": "1.0.0", | ||
| "capabilities": [ | ||
| { | ||
| "type": "task", | ||
| "description": "Task execution" | ||
| }, | ||
| { | ||
| "type": "streaming", | ||
| "description": "SSE streaming" | ||
| } | ||
| ], | ||
| "endpoints": [], | ||
| "metadata": { | ||
| "existingKey": "existingValue" | ||
| } | ||
| }` | ||
|
|
||
| var card models.AgentCard | ||
| err := json.Unmarshal([]byte(jsonData), &card) | ||
| if err != nil { | ||
| t.Fatalf("Failed to unmarshal: %v", err) | ||
| } | ||
|
|
||
| // Check if metadata exists | ||
| if card.Metadata == nil { | ||
| t.Fatal("Metadata is nil") | ||
| } | ||
|
|
||
| // Check if legacy capabilities are preserved | ||
| legacyCaps, ok := card.Metadata["legacyCapabilities"] | ||
| if !ok { | ||
| t.Fatal("legacyCapabilities not found in metadata - the fix failed!") | ||
| } | ||
|
|
||
| caps, ok := legacyCaps.([]models.Capability) | ||
| if !ok { | ||
| t.Fatalf("legacyCapabilities is not []Capability, got: %T", legacyCaps) | ||
| } | ||
|
|
||
| if len(caps) != 2 { | ||
| t.Fatalf("Expected 2 legacy capabilities, got %d", len(caps)) | ||
| } | ||
|
|
||
| if caps[0].Type != "task" || caps[0].Description != "Task execution" { | ||
| t.Errorf("First capability incorrect: %+v", caps[0]) | ||
| } | ||
|
|
||
| if caps[1].Type != "streaming" || caps[1].Description != "SSE streaming" { | ||
| t.Errorf("Second capability incorrect: %+v", caps[1]) | ||
| } | ||
|
|
||
| // Check if existing metadata is also preserved | ||
| if existingVal, ok := card.Metadata["existingKey"].(string); !ok || existingVal != "existingValue" { | ||
| t.Errorf("Existing metadata not preserved correctly: %v", card.Metadata["existingKey"]) | ||
| } | ||
|
|
||
| t.Log("✓ Legacy capabilities correctly preserved in metadata after struct assignment") | ||
| } | ||
|
|
||
| func TestAgentCardUnmarshal_ObjectFormat_MetadataPreservation(t *testing.T) { | ||
| // Test that legacy object format capabilities are also preserved | ||
| jsonData := `{ | ||
| "name": "Test Agent", | ||
| "version": "1.0.0", | ||
| "capabilities": { | ||
| "task": "Task execution", | ||
| "streaming": "SSE streaming", | ||
| "artifacts": "Artifact sharing" | ||
| }, | ||
| "endpoints": [], | ||
| "metadata": { | ||
| "customField": "customValue" | ||
| } | ||
| }` | ||
|
|
||
| var card models.AgentCard | ||
| err := json.Unmarshal([]byte(jsonData), &card) | ||
| if err != nil { | ||
| t.Fatalf("Failed to unmarshal: %v", err) | ||
| } | ||
|
|
||
| // Check if metadata exists | ||
| if card.Metadata == nil { | ||
| t.Fatal("Metadata is nil") | ||
| } | ||
|
|
||
| // Check if legacy capabilities are preserved | ||
| legacyCaps, ok := card.Metadata["legacyCapabilities"] | ||
| if !ok { | ||
| t.Fatal("legacyCapabilities not found in metadata - the fix failed!") | ||
| } | ||
|
|
||
| caps, ok := legacyCaps.([]models.Capability) | ||
| if !ok { | ||
| t.Fatalf("legacyCapabilities is not []Capability, got: %T", legacyCaps) | ||
| } | ||
|
|
||
| if len(caps) != 3 { | ||
| t.Fatalf("Expected 3 legacy capabilities, got %d", len(caps)) | ||
| } | ||
|
|
||
| // Check if existing metadata is also preserved | ||
| if customVal, ok := card.Metadata["customField"].(string); !ok || customVal != "customValue" { | ||
| t.Errorf("Existing metadata not preserved correctly: %v", card.Metadata["customField"]) | ||
| } | ||
|
|
||
| t.Log("✓ Legacy object format capabilities correctly preserved in metadata") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restoring
legacyCapabilitiesafter*a = AgentCard(*aux)will overwrite anymetadata.legacyCapabilitiesvalue that was already present in the incoming JSON (previously it would have been preserved because the struct assignment overwrote your pre-populated metadata). Consider only setting this key when it’s not already present, or using a different key/merge strategy to avoid clobbering user-provided metadata.