oauthex: use internal JSON library for decoding. (#866)

This commit is contained in:
Maciej Kisiel 2026-03-30 14:52:15 +02:00 • committed by GitHub
commit 05277f6e3c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 55 additions and 4 deletions

View file

@ -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)
}

View file

@ -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"`