Skip to content
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

Fix JSON returned for partial documents when readLimit=0 #199

Merged
merged 2 commits into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions internal/magic/magic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package magic

import "testing"

var magicTests = []struct {
raw string
limit uint32
res bool
detector Detector
}{
{`["an incomplete JSON array`, 0, false, JSON},
}

func TestMagic(t *testing.T) {
for i, tt := range magicTests {
if got := tt.detector([]byte(tt.raw), tt.limit); got != tt.res {
t.Errorf("Detector %d error: expected: %t; got: %t", i, tt.res, got)
}
}
}
6 changes: 4 additions & 2 deletions internal/magic/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,14 @@ func Php(raw []byte, limit uint32) bool {
// JSON matches a JavaScript Object Notation file.
func JSON(raw []byte, limit uint32) bool {
raw = trimLWS(raw)
if len(raw) == 0 || (raw[0] != '[' && raw[0] != '{') {
// #175 A single JSON string, number or bool is not considered JSON.
// JSON objects and arrays are reported as JSON.
if len(raw) < 2 || (raw[0] != '[' && raw[0] != '{') {
return false
}
parsed, err := json.Scan(raw)
// If the full file content was provided, check there is no error.
if len(raw) < int(limit) {
if limit == 0 || len(raw) < int(limit) {
return err == nil
}

Expand Down