diff --git a/internal/json/json.go b/internal/json/json.go index 1148770..b4c897f 100644 --- a/internal/json/json.go +++ b/internal/json/json.go @@ -8,12 +8,25 @@ package json import ( "bytes" + "io" "github.com/segmentio/encoding/json" ) -func Unmarshal(data []byte, v any) error { - dec := json.NewDecoder(bytes.NewReader(data)) - dec.DontMatchCaseInsensitiveStructFields() - return dec.Decode(v) +type Decoder struct { + dec *json.Decoder +} + +func NewDecoder(r io.Reader) *Decoder { + dec := json.NewDecoder(r) + dec.DontMatchCaseInsensitiveStructFields() + return &Decoder{dec: dec} +} + +func (d *Decoder) Decode(v any) error { + return d.dec.Decode(v) +} + +func Unmarshal(data []byte, v any) error { + return NewDecoder(bytes.NewReader(data)).Decode(v) } diff --git a/internal/json/json_test.go b/internal/json/json_test.go index 34d0226..d037e88 100644 --- a/internal/json/json_test.go +++ b/internal/json/json_test.go @@ -8,6 +8,7 @@ import ( "bytes" "encoding/json" "fmt" + "strings" "testing" "github.com/google/go-cmp/cmp" @@ -65,6 +66,43 @@ func TestUnmarshalCaseSensitivity(t *testing.T) { } } +func TestNewDecoderCaseSensitivity(t *testing.T) { + type Target struct { + Field string `json:"field"` + TaggedField string `json:"custom_tag"` + } + + tests := []struct { + name string + json string + want Target + }{ + { + name: "exact match", + json: `{"field": "value", "custom_tag": "tagged"}`, + want: Target{Field: "value", TaggedField: "tagged"}, + }, + { + name: "case mismatch", + json: `{"Field": "value", "Custom_tag": "tagged"}`, + want: Target{}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var got Target + dec := NewDecoder(strings.NewReader(tt.json)) + if err := dec.Decode(&got); err != nil { + t.Fatalf("Decode failed: %v", err) + } + if diff := cmp.Diff(tt.want, got); diff != "" { + t.Errorf("Decode mismatch (-want +got):\n%s", diff) + } + }) + } +} + func TestUnmarshalNullCharacter(t *testing.T) { type Target struct { Field string `json:"field"` diff --git a/oauthex/oauth2.go b/oauthex/oauth2.go index d8aeb3c..82c1c28 100644 --- a/oauthex/oauth2.go +++ b/oauthex/oauth2.go @@ -10,7 +10,6 @@ package oauthex import ( "context" - "encoding/json" "fmt" "io" "mime" @@ -18,6 +17,7 @@ import ( "net/url" "strings" + "github.com/modelcontextprotocol/go-sdk/internal/json" "github.com/modelcontextprotocol/go-sdk/internal/util" )