Repository: getkin/kin-openapi
Pull request #86 (2019-04-17 03:54:17)

Fix: MIME type for plain text should be text/plain
The title should be self-explanatory


Patch:
diff --git a/README.md b/README.md
index 6d6dbc5..6d2ea2b 100644
--- a/README.md
+++ b/README.md
@@ -122,7 +122,7 @@ func main() {
 ## Custom content type for body of HTTP request/response
 
 By default, the library parses a body of HTTP request and response
-if it has one of the next content types: `"plain/text"` or `"application/json"`.
+if it has one of the next content types: `"text/plain"` or `"application/json"`.
 To support other content types you must register decoders for them:
 
 ```go
diff --git a/openapi3filter/req_resp_decoder.go b/openapi3filter/req_resp_decoder.go
index dee4f23..6bc1f99 100644
--- a/openapi3filter/req_resp_decoder.go
+++ b/openapi3filter/req_resp_decoder.go
@@ -662,7 +662,7 @@ func decodeBody(body io.Reader, header http.Header, schema *openapi3.SchemaRef,
 }
 
 func init() {
-	RegisterBodyDecoder("plain/text", plainBodyDecoder)
+	RegisterBodyDecoder("text/plain", plainBodyDecoder)
 	RegisterBodyDecoder("application/json", jsonBodyDecoder)
 	RegisterBodyDecoder("application/x-www-form-urlencoded", urlencodedBodyDecoder)
 	RegisterBodyDecoder("multipart/form-data", multipartBodyDecoder)


Test patch:
diff --git a/openapi3filter/req_resp_decoder_test.go b/openapi3filter/req_resp_decoder_test.go
index 86fefe0..02627fb 100644
--- a/openapi3filter/req_resp_decoder_test.go
+++ b/openapi3filter/req_resp_decoder_test.go
@@ -892,10 +892,10 @@ func TestDecodeBody(t *testing.T) {
 	d, err := json.Marshal(map[string]interface{}{"d1": "d1"})
 	require.NoError(t, err)
 	multipartForm, multipartFormMime, err := newTestMultipartForm([]*testFormPart{
-		{name: "a", contentType: "plain/text", data: strings.NewReader("a1")},
+		{name: "a", contentType: "text/plain", data: strings.NewReader("a1")},
 		{name: "b", contentType: "application/json", data: strings.NewReader("10")},
-		{name: "c", contentType: "plain/text", data: strings.NewReader("c1")},
-		{name: "c", contentType: "plain/text", data: strings.NewReader("c2")},
+		{name: "c", contentType: "text/plain", data: strings.NewReader("c1")},
+		{name: "c", contentType: "text/plain", data: strings.NewReader("c2")},
 		{name: "d", contentType: "application/json", data: bytes.NewReader(d)},
 		{name: "f", contentType: "application/octet-stream", data: strings.NewReader("foo"), filename: "f1"},
 	})
@@ -923,7 +923,7 @@ func TestDecodeBody(t *testing.T) {
 		},
 		{
 			name: "plain text",
-			mime: "plain/text",
+			mime: "text/plain",
 			body: strings.NewReader("text"),
 			want: "text",
 		},
diff --git a/openapi3filter/validation_test.go b/openapi3filter/validation_test.go
index 88bf415..7ffd33c 100644
--- a/openapi3filter/validation_test.go
+++ b/openapi3filter/validation_test.go
@@ -182,7 +182,7 @@ func TestValidateRequestBody(t *testing.T) {
 		WithRequired(true)
 
 	plainTextContent := openapi3.NewContent()
-	plainTextContent["plain/text"] = openapi3.NewMediaType().WithSchema(openapi3.NewStringSchema())
+	plainTextContent["text/plain"] = openapi3.NewMediaType().WithSchema(openapi3.NewStringSchema())
 
 	testCases := []struct {
 		name    string
@@ -217,7 +217,7 @@ func TestValidateRequestBody(t *testing.T) {
 		{
 			name: "not JSON data",
 			body: openapi3.NewRequestBody().WithContent(plainTextContent).WithRequired(true),
-			mime: "plain/text",
+			mime: "text/plain",
 			data: strings.NewReader("foo"),
 		},
 		{