Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debug(multipart): use slices in multipart form parts #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ go 1.14
require (
github.com/ghodss/yaml v1.0.0
github.com/go-openapi/jsonpointer v0.19.5
github.com/google/go-cmp v0.5.6 // indirect
github.com/gorilla/mux v1.8.0
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.5.1
gopkg.in/yaml.v2 v2.3.0
gotest.tools v2.2.0+incompatible
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUe
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/swag v0.19.5 h1:lTz6Ys4CmqqCQmZPBlbQENR1/GucA2bzYTE12Pw4tFY=
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand All @@ -17,15 +19,21 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e h1:hB2xlXdHp/pmPZq0y3QnmWAArdw9PqbmotexnWx/FU8=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
57 changes: 56 additions & 1 deletion openapi3filter/req_resp_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/getkin/kin-openapi/openapi3"
legacyrouter "github.com/getkin/kin-openapi/routers/legacy"
"github.com/stretchr/testify/require"
"gotest.tools/assert"
)

func TestDecodeParameter(t *testing.T) {
Expand Down Expand Up @@ -978,6 +979,34 @@ func TestDecodeBody(t *testing.T) {
})
require.NoError(t, err)

jsonSlice, err := json.Marshal([]map[string]interface{}{{"d1": "d1"}})
require.NoError(t, err)
multipartFormWithJSONSlice, multipartFormWithJSONSliceMime, err := newTestMultipartForm([]*testFormPart{
{name: "a", contentType: "text/plain", data: strings.NewReader("a1")},
{name: "b", contentType: "application/json", data: strings.NewReader("10")},
{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(jsonSlice)},
{name: "f", contentType: "application/octet-stream", data: strings.NewReader("foo"), filename: "f1"},
{name: "g", data: strings.NewReader("g1")},
})
require.NoError(t, err)

structSlice, err := json.Marshal([]struct {
Name string `json:"name"`
}{{Name: "Foo"}, {Name: "Bar"}})
require.NoError(t, err)
multipartFormWithStructSlice, multipartFormWithStructSliceMime, err := newTestMultipartForm([]*testFormPart{
{name: "a", contentType: "text/plain", data: strings.NewReader("a1")},
{name: "b", contentType: "application/json", data: strings.NewReader("10")},
{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(structSlice)},
{name: "f", contentType: "application/octet-stream", data: strings.NewReader("foo"), filename: "f1"},
{name: "g", data: strings.NewReader("g1")},
})
require.NoError(t, err)

multipartFormExtraPart, multipartFormMimeExtraPart, err := newTestMultipartForm([]*testFormPart{
{name: "a", contentType: "text/plain", data: strings.NewReader("a1")},
{name: "x", contentType: "text/plain", data: strings.NewReader("x1")},
Expand Down Expand Up @@ -1096,6 +1125,32 @@ func TestDecodeBody(t *testing.T) {
WithProperty("g", openapi3.NewStringSchema()),
want: map[string]interface{}{"a": "a1", "b": float64(10), "c": []interface{}{"c1", "c2"}, "d": map[string]interface{}{"d1": "d1"}, "f": "foo", "g": "g1"},
},
{
name: "multipart",
mime: multipartFormWithJSONSliceMime,
body: multipartFormWithJSONSlice,
schema: openapi3.NewObjectSchema().
WithProperty("a", openapi3.NewStringSchema()).
WithProperty("b", openapi3.NewIntegerSchema()).
WithProperty("c", openapi3.NewArraySchema().WithItems(openapi3.NewStringSchema())).
WithProperty("d", openapi3.NewObjectSchema().WithProperty("d1", openapi3.NewStringSchema())).
WithProperty("f", openapi3.NewStringSchema().WithFormat("binary")).
WithProperty("g", openapi3.NewStringSchema()),
want: map[string]interface{}{"a": "a1", "b": float64(10), "c": []interface{}{"c1", "c2"}, "d": []map[string]interface{}{{"d1": "d1"}}, "f": "foo", "g": "g1"},
},
{
name: "multipart",
mime: multipartFormWithStructSliceMime,
body: multipartFormWithStructSlice,
schema: openapi3.NewObjectSchema().
WithProperty("a", openapi3.NewStringSchema()).
WithProperty("b", openapi3.NewIntegerSchema()).
WithProperty("c", openapi3.NewArraySchema().WithItems(openapi3.NewStringSchema())).
WithProperty("d", openapi3.NewObjectSchema().WithProperty("d1", openapi3.NewStringSchema())).
WithProperty("f", openapi3.NewStringSchema().WithFormat("binary")).
WithProperty("g", openapi3.NewStringSchema()),
want: map[string]interface{}{"a": "a1", "b": float64(10), "c": []interface{}{"c1", "c2"}, "d": []map[string]interface{}{{"name": "Foo"}, {"name": "Bar"}}, "f": "foo", "g": "g1"},
},
{
name: "multipartExtraPart",
mime: multipartFormMimeExtraPart,
Expand Down Expand Up @@ -1165,7 +1220,7 @@ func TestDecodeBody(t *testing.T) {
}

require.NoError(t, err)
require.Truef(t, reflect.DeepEqual(got, tc.want), "got %v, want %v", got, tc.want)
assert.DeepEqual(t, tc.want, got)
})
}
}
Expand Down