diff --git a/openapi3filter/req_resp_decoder.go b/openapi3filter/req_resp_decoder.go index 9f2cbe134..44abacb0f 100644 --- a/openapi3filter/req_resp_decoder.go +++ b/openapi3filter/req_resp_decoder.go @@ -3,6 +3,7 @@ package openapi3filter import ( "archive/zip" "bytes" + "encoding/csv" "encoding/json" "errors" "fmt" @@ -1015,6 +1016,7 @@ func init() { RegisterBodyDecoder("application/yaml", yamlBodyDecoder) RegisterBodyDecoder("application/zip", zipFileBodyDecoder) RegisterBodyDecoder("multipart/form-data", multipartBodyDecoder) + RegisterBodyDecoder("text/csv", csvBodyDecoder) RegisterBodyDecoder("text/plain", plainBodyDecoder) } @@ -1271,3 +1273,23 @@ func zipFileBodyDecoder(body io.Reader, header http.Header, schema *openapi3.Sch return string(content), nil } + +// csvBodyDecoder is a body decoder that decodes a csv body to a string. +func csvBodyDecoder(body io.Reader, header http.Header, schema *openapi3.SchemaRef, encFn EncodingFn) (interface{}, error) { + r := csv.NewReader(body) + + var content string + for { + record, err := r.Read() + if err == io.EOF { + break + } + if err != nil { + return nil, err + } + + content += strings.Join(record, ",") + "\n" + } + + return content, nil +}