From 65735b426e2bd0edf527412bbaa8563282abc1a3 Mon Sep 17 00:00:00 2001 From: jwalter1-quest Date: Wed, 10 Apr 2019 22:38:54 -0500 Subject: [PATCH] Issue: #1917 in github.com/go-swagger/go-swagger: Don't attempt to unmarshal empty response bodies to prevent panic. Signed-off-by: jwalter1-quest --- text.go | 6 ++++++ text_test.go | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/text.go b/text.go index 77099fed..c7fd04c3 100644 --- a/text.go +++ b/text.go @@ -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 { diff --git a/text_test.go b/text_test.go index 86f8f568..1c6a944b 100644 --- a/text_test.go +++ b/text_test.go @@ -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))