Skip to content

Commit 3d6572c

Browse files
authored
Merge pull request #135 from jeffreydwalter/master
Issue: #1917 in github.com/go-swagger/go-swagger: Don't attempt to unmarshal empty response bodies to prevent panic.
2 parents 1097371 + 65735b4 commit 3d6572c

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

text.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ func TextConsumer() Consumer {
3939
}
4040
b := buf.Bytes()
4141

42+
// If the buffer is empty, no need to unmarshal it, which causes a panic.
43+
if len(b) == 0 {
44+
data = ""
45+
return nil
46+
}
47+
4248
if tu, ok := data.(encoding.TextUnmarshaler); ok {
4349
err := tu.UnmarshalText(b)
4450
if err != nil {

text_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestTextConsumer(t *testing.T) {
4343
assert.Equal(t, consProdText, tu.str)
4444

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

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

0 commit comments

Comments
 (0)