Skip to content

Commit

Permalink
Merge pull request #233 from kzys/swag-file
Browse files Browse the repository at this point in the history
Break dependency cycles
  • Loading branch information
youyuanwu committed Feb 5, 2022
2 parents 53db7ce + beeaff4 commit 6bf3659
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 186 deletions.
18 changes: 2 additions & 16 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,6 @@

package runtime

import "mime/multipart"
import "github.com/go-openapi/swag"

// File represents an uploaded file.
type File struct {
Data multipart.File
Header *multipart.FileHeader
}

// Read bytes from the file
func (f *File) Read(p []byte) (n int, err error) {
return f.Data.Read(p)
}

// Close the file
func (f *File) Close() error {
return f.Data.Close()
}
type File = swag.File
28 changes: 18 additions & 10 deletions file_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package runtime

import (
"io"
"testing"

"github.com/go-openapi/spec"
"github.com/go-openapi/validate"
"github.com/stretchr/testify/assert"
)

func TestFileImplementsIOReader(t *testing.T) {
var file interface{} = &File{}
expected := "that File implements io.Reader"
assert.Implements(t, new(io.Reader), file, expected)
}
func TestValidateFile(t *testing.T) {
fileParam := spec.FileParam("f")
validator := validate.NewParamValidator(fileParam, nil)

result := validator.Validate("str")
assert.Equal(t, 1, len(result.Errors))
assert.Equal(
t,
`f in formData must be of type file: "string"`,
result.Errors[0].Error(),
)

result = validator.Validate(&File{})
assert.True(t, result.IsValid())

func TestFileImplementsIOReadCloser(t *testing.T) {
var file interface{} = &File{}
expected := "that File implements io.ReadCloser"
assert.Implements(t, new(io.ReadCloser), file, expected)
result = validator.Validate(File{})
assert.True(t, result.IsValid())
}
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ module github.com/go-openapi/runtime

require (
github.com/docker/go-units v0.4.0
github.com/go-openapi/analysis v0.20.1
github.com/go-openapi/analysis v0.21.2
github.com/go-openapi/errors v0.20.1
github.com/go-openapi/loads v0.21.0
github.com/go-openapi/loads v0.21.1
github.com/go-openapi/spec v0.20.4
github.com/go-openapi/strfmt v0.21.0
github.com/go-openapi/swag v0.19.15
github.com/go-openapi/validate v0.20.3
github.com/go-openapi/strfmt v0.21.1
github.com/go-openapi/swag v0.21.1
github.com/go-openapi/validate v0.20.4-0.20220203034227-bad0d9503970
github.com/opentracing/opentracing-go v1.2.0
github.com/stretchr/testify v1.7.0
gopkg.in/yaml.v2 v2.4.0
Expand Down
Loading

0 comments on commit 6bf3659

Please sign in to comment.