From d8cf255c801f0df81870b82eb2527963d5ba46c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Thu, 28 Nov 2024 12:06:57 +0100 Subject: [PATCH] v0.0.546 Fix ginext json-parse error when the bufferedReader was read beforehand --- dataext/bufferedReadCloser.go | 17 ++++++++++++++++- ginext/preContext.go | 11 +++++++++++ goextVersion.go | 4 ++-- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/dataext/bufferedReadCloser.go b/dataext/bufferedReadCloser.go index 376f1aa..dbd678d 100644 --- a/dataext/bufferedReadCloser.go +++ b/dataext/bufferedReadCloser.go @@ -115,6 +115,9 @@ func (b *bufferedReadCloser) BufferedAll() ([]byte, error) { return nil, err } } + if err := b.Reset(); err != nil { + return nil, err + } return b.buffer, nil case modeSourceFinished: @@ -131,10 +134,22 @@ func (b *bufferedReadCloser) BufferedAll() ([]byte, error) { } } +// Reset resets the buffer to the beginning of the buffer. +// If the original source is partially read, we will finish reading it and fill our buffer func (b *bufferedReadCloser) Reset() error { switch b.mode { case modeSourceReading: - fallthrough + if b.off == 0 { + return nil // nobody has read anything yet + } + err := b.Close() + if err != nil { + return err + } + b.mode = modeBufferReading + b.off = 0 + return nil + case modeSourceFinished: err := b.Close() if err != nil { diff --git a/ginext/preContext.go b/ginext/preContext.go index 2075d1e..65ebd59 100644 --- a/ginext/preContext.go +++ b/ginext/preContext.go @@ -100,6 +100,17 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) { if pctx.body != nil { if pctx.ginCtx.ContentType() == "application/json" { + if brc, ok := pctx.body.(dataext.BufferedReadCloser); ok { + // Ensures a fully reset (offset=0) buffer before parsing + err := brc.Reset() + if err != nil { + err = exerr.Wrap(err, "Failed to read (brc.reset) json-body"). + WithType(exerr.TypeBindFailJSON). + Str("struct_type", fmt.Sprintf("%T", pctx.body)). + Build() + return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "JSON", err)) + } + } if err := pctx.ginCtx.ShouldBindJSON(pctx.body); err != nil { err = exerr.Wrap(err, "Failed to read json-body"). WithType(exerr.TypeBindFailJSON). diff --git a/goextVersion.go b/goextVersion.go index f302470..8187c69 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.545" +const GoextVersion = "0.0.546" -const GoextVersionTimestamp = "2024-11-27T13:21:45+0100" +const GoextVersionTimestamp = "2024-11-28T12:06:57+0100"