go-jose / go-jose

An implementation of JOSE standards (JWE, JWS, JWT) in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Optimization in json.encodeByteSlice causes cannot use type *json.encodeState as type io.Writer

agilezebra opened this issue · comments

When using this package in a traefik plugin, we get the following error:
[snip]import \"github.com/go-jose/go-jose/v3\" error: github.com/go-jose/go-jose/v3/asymmetric.go:33:2: import \"github.com/go-jose/go-jose/v3/json\" error: jwt-middleware/vendor/github.com/go-jose/go-jose/v3/json/encode.go:650:48: cannot use type *json.encodeState as type io.Writer

If I remove the optimization for longer buffers and have all flows use the small buffer code, everything works as expected. E.g.

func encodeByteSlice(e *encodeState, v reflect.Value, _ bool) {
	if v.IsNil() {
		e.WriteString("null")
		return
	}
	s := v.Bytes()
	e.WriteByte('"')
	dst := make([]byte, base64.StdEncoding.EncodedLen(len(s)))
	base64.StdEncoding.Encode(dst, s)
	e.Write(dst)
	e.WriteByte('"')
}

encodeByteSlice.patch

I note that the current version of json/encode.go also does not have this optimization (although this as moved to append-like operations):
https://github.com/golang/go/blob/9ac6b00e79312e5ad4665acc063ac7b77becddf8/src/encoding/json/encode.go#L779-L791