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.

Home Page:https://gin-gonic.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

YAML BindBody should not parse JSON content

RedCrazyGhost opened this issue · comments

Description

When I was writing the ShouldBindBodyWith shortcut, I tested that YAML would parse JSON BODY content, which should not be expected

How to reproduce

func TestYAMLBindingBindBodyJSON(t *testing.T) {
	var s struct {
		Foo string `yaml:"foo"`
	}
	err := yamlBinding{}.BindBody([]byte(`{"foo":"FOO"}`), &s)
	require.Error(t, err)
	assert.Equal(t, "", s.Foo)
}

Expectations

=== RUN   TestYAMLBindingBindBodyJSON
--- PASS: TestYAMLBindingBindBodyJSON (0.00s)
PASS

Actual result

=== RUN   TestYAMLBindingBindBodyJSON
    yaml_test.go:27: 
                Error Trace:    /Users/wenxingzhan/GolandProjects/gin/binding/yaml_test.go:27
                Error:          An error is expected but got nil.
                Test:           TestYAMLBindingBindBodyJSON
--- FAIL: TestYAMLBindingBindBodyJSON (0.00s)
FAIL

Environment

  • go version: go1.21.3 darwin/amd64
  • gin version (or commit ref): ecdbbbe
  • operating system: mac os 14.3.1 (23D60)

I looked it up, and Red Hat says that YAML is a superset of JSON, and all the data can be parsed in JSON format.