Skip to content

Commit

Permalink
refs getkin#696, Support csv doby decoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
k2tzumi committed Jan 7, 2023
1 parent 586f897 commit 96bc8b8
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions openapi3filter/req_resp_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package openapi3filter
import (
"archive/zip"
"bytes"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}

0 comments on commit 96bc8b8

Please sign in to comment.