gin-gonic / gin

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
https://gin-gonic.com/
MIT License
78.83k stars 8.02k forks source link

msgpack incorrectly encoding unsigned integers #4043

Closed anpez closed 2 months ago

anpez commented 2 months ago

Description

Native msgpack encodes unsigned integers using positive_fixint instead of the correct uintXX. Encoding with the external vmihailenco/msgpack gives the correct encoding.

How to reproduce

package main

import (
    "github.com/gin-gonic/gin"
    "github.com/vmihailenco/msgpack/v5"
)

func main() {
    var ret struct {
        T uint64 `msgpack:"t"`
    }

    g := gin.Default()
    g.GET("/testmsg", func(c *gin.Context) {
        c.Render(200, render.MsgPack{Data: ret})
    })
    g.GET("/testgin", func(c *gin.Context) {
        bytes, _ := msgpack.Marshal(ret)
        c.Writer.Write(bytes)
    })
    g.Run(":9000")
}

Expectations

$ curl -s localhost:9000/testmsg | fq -d msgpack '. | tovalue'
{
  "length": 1,
  "pairs": [
    {
      "key": {
        "length": 1,
        "type": "fixstr",
        "value": "t"
      },
      "value": {
        "type": "uint64",
        "value": 0
      }
    }
  ],
  "type": "fixmap"
}

Actual result

$ curl -s localhost:9000/testgin | fq -d msgpack '. | tovalue'
{
  "length": 1,
  "pairs": [
    {
      "key": {
        "length": 1,
        "type": "fixstr",
        "value": "T"
      },
      "value": {
        "type": "positive_fixint",
        "value": 0
      }
    }
  ],
  "type": "fixmap"
}

Environment

JimChenWYU commented 2 months ago

gin use github.com/ugorji/go/codec as msgpack encoder, can you help to test whether its behavior is consistent with github.com/vmihailenco/msgpack/v5 ?

anpez commented 2 months ago

gin use github.com/ugorji/go/codec as msgpack encoder, can you help to test whether its behavior is consistent with github.com/vmihailenco/msgpack/v5 ?

I've been reading through ugorji/go and it looks like it's not a bug, but a feature. A long explaning issue is on their github here. So I guess that's it for gin then. Thanks anyway for your time