Skip to content

Commit f45b674

Browse files
committed
parameter binding rewrite
Reorganized the code a bit, so that runtime requirements are in the package "runtime". Parameters in swagger are freakishly complicated, and this is a pretty robust implementation that parses most of the supported types. We limit ourselves to form styled query parameters, since Content parameters can describe anything else.
1 parent d85d50d commit f45b674

2 files changed

Lines changed: 14 additions & 18 deletions

File tree

oapi_validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func OapiValidatorFromYamlFile(path string) (echo.MiddlewareFunc, error) {
3535
return nil, fmt.Errorf("error reading %s: %s", path, err)
3636
}
3737

38-
swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromYAMLData(data)
38+
swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromData(data)
3939
if err != nil {
4040
return nil, fmt.Errorf("error parsing %s as Swagger YAML: %s",
4141
path, err)

oapi_validate_test.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func doPost(t *testing.T, e *echo.Echo, url string, jsonBody interface{}) *httpt
7979
}
8080

8181
func TestOapiRequestValidator(t *testing.T) {
82-
swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromYAMLData([]byte(testSchema))
82+
swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromData([]byte(testSchema))
8383
assert.NoError(t, err, "Error initializing swagger")
8484

8585
// Create a new echo router
@@ -112,24 +112,20 @@ func TestOapiRequestValidator(t *testing.T) {
112112
}
113113

114114
// Send an out-of-spec parameter
115-
// TODO(marcin): limit checking for numbers is not yet working in
116-
// kin-openapi, they only check the string type. I either need to do
117-
// this myself, or patch their code.
118-
//{
119-
// rec := doGet(t, e, "http://deepmap.ai/resource?id=500")
120-
// assert.Equal(t, http.StatusBadRequest, rec.Code)
121-
// assert.False(t, called, "Handler should not have been called")
122-
// called = false
123-
//}
115+
{
116+
rec := doGet(t, e, "http://deepmap.ai/resource?id=500")
117+
assert.Equal(t, http.StatusBadRequest, rec.Code)
118+
assert.False(t, called, "Handler should not have been called")
119+
called = false
120+
}
124121

125122
// Send a bad parameter type
126-
// TODO(marcin): type checking is currently not yet working in kin-openapi
127-
//{
128-
// rec := doGet(t, e, "http://deepmap.ai/resource?id=foo")
129-
// assert.Equal(t, http.StatusBadRequest, rec.Code)
130-
// assert.False(t, called, "Handler should not have been called")
131-
// called = false
132-
//}
123+
{
124+
rec := doGet(t, e, "http://deepmap.ai/resource?id=foo")
125+
assert.Equal(t, http.StatusBadRequest, rec.Code)
126+
assert.False(t, called, "Handler should not have been called")
127+
called = false
128+
}
133129

134130
// Add a handler for the POST message
135131
e.POST("/resource", func(c echo.Context) error {

0 commit comments

Comments
 (0)