Skip to content

Commit

Permalink
unmarshal: fix when second doc is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
santhosh-tekuri committed Apr 23, 2024
1 parent f7367d6 commit 8b8cf44
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ func UnmarshalJSON(r io.Reader) (any, error) {
if err := decoder.Decode(&doc); err != nil {
return nil, err
}
if t, _ := decoder.Token(); t != nil {
return nil, fmt.Errorf("invalid character %v after top-level value", t)
if _, err := decoder.Token(); err == nil || err != io.EOF {
return nil, fmt.Errorf("invalid character after top-level value")
}
return doc, nil
}
28 changes: 28 additions & 0 deletions loader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package jsonschema

import (
"strings"
"testing"
)

func TestUnmarshalJSON(t *testing.T) {
tests := []struct {
input string
valid bool
}{
{"{", false},
{"{}", true},
{"{}A", false},
{"{}{}", false},
}

for _, test := range tests {
_, err := UnmarshalJSON(strings.NewReader(test.input))
if valid := err == nil; valid != test.valid {
if err != nil {
t.Log(err)
t.Errorf("UnmarshalJSON(%q) valid: got %v, want %v", test.input, valid, test.valid)
}
}
}
}

0 comments on commit 8b8cf44

Please sign in to comment.