Skip to content

Conversation

@MrGossett
Copy link
Contributor

Check that UnmarshalJSON methods for Content don't panic on nil.

  • Introduces a new test file content_nil_test.go which verifies that UnmarshalJSON methods for various Content types do not panic when unmarshaling onto nil pointers.
  • Adds a nil check in contentFromWire function to guard against a nil wire.Content parameter.
  • Tests cover different scenarios, including valid and invalid content types, as well as cases with empty or missing content fields.

For modelcontextprotocol#205

- Introduces a new test file `content_nil_test.go` which verifies that `UnmarshalJSON` methods for various `Content` types do not panic when unmarshaling onto `nil` pointers.
- Adds a `nil` check in `contentFromWire` function to guard against a `nil` `wire.Content` parameter.
- Tests cover different scenarios, including valid and invalid content types, as well as cases with empty or missing content fields.

For [modelcontextprotocol#205](modelcontextprotocol#205)
@MrGossett MrGossett marked this pull request as ready for review August 13, 2025 16:02
@findleyr findleyr self-requested a review August 13, 2025 17:43
Copy link
Contributor

@findleyr findleyr left a comment

Choose a reason for hiding this comment

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

Thanks! Appreciate the tests (and fix?).

Just think we should update this to use cmp.Diff. I can merge and then do that myself if you prefer.

}

// Verify that the Content field was properly populated
switch v := tt.content.(type) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can simplify this significantly using cmp.Diff: just compare the expected and actual value.
https://pkg.go.dev/github.com/google/go-cmp/cmp is already required by this module.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in a56a3cc

}

func contentFromWire(wire *wireContent, allow map[string]bool) (Content, error) {
if wire == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

The test failed without this change, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's correct. One of the tests that I added failed without this change.

The test is unmarshaling {"model":"test","role":"user"} (missing "content"), which causes the *wireContent parameter here to be nil.

Below is the test output without this change:

$ go test ./mcp   
--- FAIL: TestContentUnmarshalNilWithEmptyContent (0.00s)
    --- FAIL: TestContentUnmarshalNilWithEmptyContent/Missing_Content_field (0.00s)
        content_nil_test.go:170: UnmarshalJSON panicked: runtime error: invalid memory address or nil pointer dereference
FAIL
FAIL    github.com/modelcontextprotocol/go-sdk/mcp      7.532s
FAIL

- Added expected output values for various `Content` types in `TestContentUnmarshalNil`.
- Changed to use the `cmp` package to simplify assertions.
Copy link
Contributor

@findleyr findleyr left a comment

Choose a reason for hiding this comment

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

Thanks!

@findleyr findleyr merged commit cfa5c1d into modelcontextprotocol:main Aug 14, 2025
5 checks passed
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.

2 participants