Skip to content

Commit 9c61295

Browse files
chore(header): Add support for RFC 9512: application/yaml (#3851)
* fix(binding): support application/yaml RFC 9512 defines application/yaml as the official YAML MIME type. application/x-yaml is deprecated. In this commit, we ensure it is recognized correctly in Content-Type. * fix(render): use application/yaml when rendering YAML As per RFC 9512, application/x-yaml is now deprecated and applications should use application/yaml. This commit fix the Content-Type header when rendering YAML.
1 parent ae15646 commit 9c61295

File tree

6 files changed

+12
-8
lines changed

6 files changed

+12
-8
lines changed

binding/binding.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const (
2121
MIMEMSGPACK = "application/x-msgpack"
2222
MIMEMSGPACK2 = "application/msgpack"
2323
MIMEYAML = "application/x-yaml"
24+
MIMEYAML2 = "application/yaml"
2425
MIMETOML = "application/toml"
2526
)
2627

@@ -102,7 +103,7 @@ func Default(method, contentType string) Binding {
102103
return ProtoBuf
103104
case MIMEMSGPACK, MIMEMSGPACK2:
104105
return MsgPack
105-
case MIMEYAML:
106+
case MIMEYAML, MIMEYAML2:
106107
return YAML
107108
case MIMETOML:
108109
return TOML

binding/binding_nomsgpack.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const (
1919
MIMEMultipartPOSTForm = "multipart/form-data"
2020
MIMEPROTOBUF = "application/x-protobuf"
2121
MIMEYAML = "application/x-yaml"
22+
MIMEYAML2 = "application/yaml"
2223
MIMETOML = "application/toml"
2324
)
2425

@@ -96,7 +97,7 @@ func Default(method, contentType string) Binding {
9697
return XML
9798
case MIMEPROTOBUF:
9899
return ProtoBuf
99-
case MIMEYAML:
100+
case MIMEYAML, MIMEYAML2:
100101
return YAML
101102
case MIMEMultipartPOSTForm:
102103
return FormMultipart

binding/binding_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ func TestBindingDefault(t *testing.T) {
164164

165165
assert.Equal(t, YAML, Default("POST", MIMEYAML))
166166
assert.Equal(t, YAML, Default("PUT", MIMEYAML))
167+
assert.Equal(t, YAML, Default("POST", MIMEYAML2))
168+
assert.Equal(t, YAML, Default("PUT", MIMEYAML2))
167169

168170
assert.Equal(t, TOML, Default("POST", MIMETOML))
169171
assert.Equal(t, TOML, Default("PUT", MIMETOML))

context_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ func TestContextRenderUTF8Attachment(t *testing.T) {
10601060
}
10611061

10621062
// TestContextRenderYAML tests that the response is serialized as YAML
1063-
// and Content-Type is set to application/x-yaml
1063+
// and Content-Type is set to application/yaml
10641064
func TestContextRenderYAML(t *testing.T) {
10651065
w := httptest.NewRecorder()
10661066
c, _ := CreateTestContext(w)
@@ -1069,7 +1069,7 @@ func TestContextRenderYAML(t *testing.T) {
10691069

10701070
assert.Equal(t, http.StatusCreated, w.Code)
10711071
assert.Equal(t, "foo: bar\n", w.Body.String())
1072-
assert.Equal(t, "application/x-yaml; charset=utf-8", w.Header().Get("Content-Type"))
1072+
assert.Equal(t, "application/yaml; charset=utf-8", w.Header().Get("Content-Type"))
10731073
}
10741074

10751075
// TestContextRenderTOML tests that the response is serialized as TOML
@@ -1217,7 +1217,7 @@ func TestContextNegotiationWithYAML(t *testing.T) {
12171217

12181218
assert.Equal(t, http.StatusOK, w.Code)
12191219
assert.Equal(t, "foo: bar\n", w.Body.String())
1220-
assert.Equal(t, "application/x-yaml; charset=utf-8", w.Header().Get("Content-Type"))
1220+
assert.Equal(t, "application/yaml; charset=utf-8", w.Header().Get("Content-Type"))
12211221
}
12221222

12231223
func TestContextNegotiationWithTOML(t *testing.T) {

render/render_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,12 @@ b:
280280
d: [3, 4]
281281
`
282282
(YAML{data}).WriteContentType(w)
283-
assert.Equal(t, "application/x-yaml; charset=utf-8", w.Header().Get("Content-Type"))
283+
assert.Equal(t, "application/yaml; charset=utf-8", w.Header().Get("Content-Type"))
284284

285285
err := (YAML{data}).Render(w)
286286
assert.NoError(t, err)
287287
assert.Equal(t, "|4-\n a : Easy!\n b:\n \tc: 2\n \td: [3, 4]\n \t\n", w.Body.String())
288-
assert.Equal(t, "application/x-yaml; charset=utf-8", w.Header().Get("Content-Type"))
288+
assert.Equal(t, "application/yaml; charset=utf-8", w.Header().Get("Content-Type"))
289289
}
290290

291291
type fail struct{}

render/yaml.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type YAML struct {
1515
Data any
1616
}
1717

18-
var yamlContentType = []string{"application/x-yaml; charset=utf-8"}
18+
var yamlContentType = []string{"application/yaml; charset=utf-8"}
1919

2020
// Render (YAML) marshals the given interface object and writes data with custom ContentType.
2121
func (r YAML) Render(w http.ResponseWriter) error {

0 commit comments

Comments
 (0)