Skip to content

Commit

Permalink
Merge pull request #135 from jeffreydwalter/master
Browse files Browse the repository at this point in the history
Issue: #1917 in github.com/go-swagger/go-swagger: Don't attempt to unmarshal empty response bodies to prevent panic.
  • Loading branch information
casualjim authored Apr 11, 2019
2 parents 1097371 + 65735b4 commit 3d6572c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions text.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func TextConsumer() Consumer {
}
b := buf.Bytes()

// If the buffer is empty, no need to unmarshal it, which causes a panic.
if len(b) == 0 {
data = ""
return nil
}

if tu, ok := data.(encoding.TextUnmarshaler); ok {
err := tu.UnmarshalText(b)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestTextConsumer(t *testing.T) {
assert.Equal(t, consProdText, tu.str)

// text unmarshal objects can return an error as well, this will be propagated
assert.Error(t, cons.Consume(bytes.NewBuffer(nil), &tu))
assert.NoError(t, cons.Consume(bytes.NewBuffer(nil), &tu))

// when readers can't be read, those errors will be propogated as well
assert.Error(t, cons.Consume(new(nopReader), &tu))
Expand Down

0 comments on commit 3d6572c

Please sign in to comment.