Skip to content

Commit 2175b2a

Browse files
authored
openapi3filter: RegisterBodyDecoder for application/zip (#730)
1 parent 2ed340d commit 2175b2a

2 files changed

Lines changed: 174 additions & 4 deletions

File tree

openapi3filter/req_resp_decoder.go

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package openapi3filter
22

33
import (
4+
"archive/zip"
5+
"bytes"
46
"encoding/json"
57
"errors"
68
"fmt"
@@ -1004,15 +1006,16 @@ func decodeBody(body io.Reader, header http.Header, schema *openapi3.SchemaRef,
10041006
}
10051007

10061008
func init() {
1007-
RegisterBodyDecoder("text/plain", plainBodyDecoder)
10081009
RegisterBodyDecoder("application/json", jsonBodyDecoder)
10091010
RegisterBodyDecoder("application/json-patch+json", jsonBodyDecoder)
1010-
RegisterBodyDecoder("application/x-yaml", yamlBodyDecoder)
1011-
RegisterBodyDecoder("application/yaml", yamlBodyDecoder)
1011+
RegisterBodyDecoder("application/octet-stream", FileBodyDecoder)
10121012
RegisterBodyDecoder("application/problem+json", jsonBodyDecoder)
10131013
RegisterBodyDecoder("application/x-www-form-urlencoded", urlencodedBodyDecoder)
1014+
RegisterBodyDecoder("application/x-yaml", yamlBodyDecoder)
1015+
RegisterBodyDecoder("application/yaml", yamlBodyDecoder)
1016+
RegisterBodyDecoder("application/zip", ZipFileBodyDecoder)
10141017
RegisterBodyDecoder("multipart/form-data", multipartBodyDecoder)
1015-
RegisterBodyDecoder("application/octet-stream", FileBodyDecoder)
1018+
RegisterBodyDecoder("text/plain", plainBodyDecoder)
10161019
}
10171020

10181021
func plainBodyDecoder(body io.Reader, header http.Header, schema *openapi3.SchemaRef, encFn EncodingFn) (interface{}, error) {
@@ -1217,3 +1220,54 @@ func FileBodyDecoder(body io.Reader, header http.Header, schema *openapi3.Schema
12171220
}
12181221
return string(data), nil
12191222
}
1223+
1224+
// ZipFileBodyDecoder is a body decoder that decodes a zip file body to a string.
1225+
func ZipFileBodyDecoder(body io.Reader, header http.Header, schema *openapi3.SchemaRef, encFn EncodingFn) (interface{}, error) {
1226+
buff := bytes.NewBuffer([]byte{})
1227+
size, err := io.Copy(buff, body)
1228+
if err != nil {
1229+
return nil, err
1230+
}
1231+
1232+
zr, err := zip.NewReader(bytes.NewReader(buff.Bytes()), size)
1233+
if err != nil {
1234+
return nil, err
1235+
}
1236+
1237+
const bufferSize = 256
1238+
content := make([]byte, 0, bufferSize*len(zr.File))
1239+
buffer := make([]byte, bufferSize)
1240+
1241+
for _, f := range zr.File {
1242+
err := func() error {
1243+
rc, err := f.Open()
1244+
if err != nil {
1245+
return err
1246+
}
1247+
defer func() {
1248+
_ = rc.Close()
1249+
}()
1250+
1251+
for {
1252+
n, err := rc.Read(buffer)
1253+
if 0 < n {
1254+
content = append(content, buffer...)
1255+
}
1256+
if err == io.EOF {
1257+
break
1258+
}
1259+
if err != nil {
1260+
return err
1261+
}
1262+
}
1263+
1264+
return nil
1265+
}()
1266+
1267+
if err != nil {
1268+
return nil, err
1269+
}
1270+
}
1271+
1272+
return string(content), nil
1273+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package openapi3filter_test
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"io"
7+
"mime/multipart"
8+
"net/http"
9+
"net/textproto"
10+
"testing"
11+
12+
"github.com/stretchr/testify/require"
13+
14+
"github.com/getkin/kin-openapi/openapi3"
15+
"github.com/getkin/kin-openapi/openapi3filter"
16+
"github.com/getkin/kin-openapi/routers/gorillamux"
17+
)
18+
19+
func TestValidateZipFileUpload(t *testing.T) {
20+
const spec = `
21+
openapi: 3.0.0
22+
info:
23+
title: 'Validator'
24+
version: 0.0.1
25+
paths:
26+
/test:
27+
post:
28+
requestBody:
29+
required: true
30+
content:
31+
multipart/form-data:
32+
schema:
33+
type: object
34+
required:
35+
- file
36+
properties:
37+
file:
38+
type: string
39+
format: binary
40+
responses:
41+
'200':
42+
description: Created
43+
`
44+
45+
loader := openapi3.NewLoader()
46+
doc, err := loader.LoadFromData([]byte(spec))
47+
require.NoError(t, err)
48+
49+
err = doc.Validate(loader.Context)
50+
require.NoError(t, err)
51+
52+
router, err := gorillamux.NewRouter(doc)
53+
require.NoError(t, err)
54+
55+
tests := []struct {
56+
zipData []byte
57+
wantErr bool
58+
}{
59+
{
60+
[]byte{
61+
0x50, 0x4b, 0x03, 0x04, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x7d, 0x23, 0x56, 0xcd, 0xfd, 0x67, 0xf8, 0x07, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x09, 0x00, 0x1c, 0x00, 0x65, 0x6e, 0x74, 0x72, 0x79, 0x2e, 0x74, 0x78, 0x74, 0x55, 0x54, 0x09, 0x00, 0x03, 0xac, 0xce, 0xb3, 0x63, 0xaf, 0xce, 0xb3, 0x63, 0x75, 0x78, 0x0b, 0x00, 0x01, 0x04, 0xf7, 0x01, 0x00, 0x00, 0x04, 0x14, 0x00, 0x00, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x0a, 0x50, 0x4b, 0x01, 0x02, 0x1e, 0x03, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x7d, 0x23, 0x56, 0xcd, 0xfd, 0x67, 0xf8, 0x07, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x09, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xa4, 0x81, 0x00, 0x00, 0x00, 0x00, 0x65, 0x6e, 0x74, 0x72, 0x79, 0x2e, 0x74, 0x78, 0x74, 0x55, 0x54, 0x05, 0x00, 0x03, 0xac, 0xce, 0xb3, 0x63, 0x75, 0x78, 0x0b, 0x00, 0x01, 0x04, 0xf7, 0x01, 0x00, 0x00, 0x04, 0x14, 0x00, 0x00, 0x00, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x4f, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00, 0x00,
62+
},
63+
false,
64+
},
65+
{
66+
[]byte{
67+
0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68+
}, // No entry
69+
true,
70+
},
71+
}
72+
for _, tt := range tests {
73+
body := &bytes.Buffer{}
74+
writer := multipart.NewWriter(body)
75+
76+
{ // Add file data
77+
h := make(textproto.MIMEHeader)
78+
h.Set("Content-Disposition", `form-data; name="file"; filename="hello.zip"`)
79+
h.Set("Content-Type", "application/zip")
80+
81+
fw, err := writer.CreatePart(h)
82+
require.NoError(t, err)
83+
_, err = io.Copy(fw, bytes.NewReader(tt.zipData))
84+
85+
require.NoError(t, err)
86+
}
87+
88+
writer.Close()
89+
90+
req, err := http.NewRequest(http.MethodPost, "/test", bytes.NewReader(body.Bytes()))
91+
require.NoError(t, err)
92+
93+
req.Header.Set("Content-Type", writer.FormDataContentType())
94+
95+
route, pathParams, err := router.FindRoute(req)
96+
require.NoError(t, err)
97+
98+
if err = openapi3filter.ValidateRequestBody(
99+
context.Background(),
100+
&openapi3filter.RequestValidationInput{
101+
Request: req,
102+
PathParams: pathParams,
103+
Route: route,
104+
},
105+
route.Operation.RequestBody.Value,
106+
); err != nil {
107+
if !tt.wantErr {
108+
t.Errorf("got %v", err)
109+
}
110+
continue
111+
}
112+
if tt.wantErr {
113+
t.Errorf("want err")
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)