From a2254d99982d94a6774ef4cbcdf1ccb03d56bcff Mon Sep 17 00:00:00 2001 From: Pierre Fenoll Date: Fri, 3 Dec 2021 09:49:46 +0000 Subject: [PATCH] nitpicking: use type openapi3.Schemas (#456) --- openapi3/loader_issue212_test.go | 2 +- openapi3/loader_test.go | 10 +++++----- openapi3/schema.go | 6 +++--- openapi3/schema_test.go | 8 ++++---- openapi3filter/req_resp_decoder.go | 3 +-- openapi3filter/validation_error_test.go | 3 ++- routers/legacy/router.go | 5 ++++- 7 files changed, 20 insertions(+), 17 deletions(-) diff --git a/openapi3/loader_issue212_test.go b/openapi3/loader_issue212_test.go index 507b37522..252d0d224 100644 --- a/openapi3/loader_issue212_test.go +++ b/openapi3/loader_issue212_test.go @@ -81,7 +81,7 @@ components: expected, err := json.Marshal(&Schema{ Type: "object", Required: []string{"id", "uri"}, - Properties: map[string]*SchemaRef{ + Properties: Schemas{ "id": {Value: &Schema{Type: "string"}}, "uri": {Value: &Schema{Type: "string"}}, }, diff --git a/openapi3/loader_test.go b/openapi3/loader_test.go index 4bc4ce432..eb293148d 100644 --- a/openapi3/loader_test.go +++ b/openapi3/loader_test.go @@ -227,18 +227,18 @@ paths: require.NotNil(t, doc.Paths["/"].Post.RequestBody.Value.Content.Get("application/json").Examples["test"]) } -func createTestServer(handler http.Handler) *httptest.Server { +func createTestServer(t *testing.T, handler http.Handler) *httptest.Server { ts := httptest.NewUnstartedServer(handler) - l, _ := net.Listen("tcp", addr) + l, err := net.Listen("tcp", addr) + require.NoError(t, err) ts.Listener.Close() ts.Listener = l return ts } func TestLoadFromRemoteURL(t *testing.T) { - fs := http.FileServer(http.Dir("testdata")) - ts := createTestServer(fs) + ts := createTestServer(t, fs) ts.Start() defer ts.Close() @@ -351,7 +351,7 @@ func TestLoadFromDataWithExternalRequestResponseHeaderRemoteRef(t *testing.T) { }`) fs := http.FileServer(http.Dir("testdata")) - ts := createTestServer(fs) + ts := createTestServer(t, fs) ts.Start() defer ts.Close() diff --git a/openapi3/schema.go b/openapi3/schema.go index a0796989a..a15677c66 100644 --- a/openapi3/schema.go +++ b/openapi3/schema.go @@ -372,7 +372,7 @@ func NewArraySchema() *Schema { func NewObjectSchema() *Schema { return &Schema{ Type: TypeObject, - Properties: make(map[string]*SchemaRef), + Properties: make(Schemas), } } @@ -493,7 +493,7 @@ func (schema *Schema) WithProperty(name string, propertySchema *Schema) *Schema func (schema *Schema) WithPropertyRef(name string, ref *SchemaRef) *Schema { properties := schema.Properties if properties == nil { - properties = make(map[string]*SchemaRef) + properties = make(Schemas) schema.Properties = properties } properties[name] = ref @@ -501,7 +501,7 @@ func (schema *Schema) WithPropertyRef(name string, ref *SchemaRef) *Schema { } func (schema *Schema) WithProperties(properties map[string]*Schema) *Schema { - result := make(map[string]*SchemaRef, len(properties)) + result := make(Schemas, len(properties)) for k, v := range properties { result[k] = &SchemaRef{ Value: v, diff --git a/openapi3/schema_test.go b/openapi3/schema_test.go index f724f08e2..fdbd5e7bf 100644 --- a/openapi3/schema_test.go +++ b/openapi3/schema_test.go @@ -389,7 +389,7 @@ var schemaExamples = []schemaExample{ UniqueItems: true, Items: (&Schema{ Type: "object", - Properties: map[string]*SchemaRef{ + Properties: Schemas{ "key1": NewFloat64Schema().NewRef(), }, }).NewRef(), @@ -446,7 +446,7 @@ var schemaExamples = []schemaExample{ UniqueItems: true, Items: (&Schema{ Type: "object", - Properties: map[string]*SchemaRef{ + Properties: Schemas{ "key1": (&Schema{ Type: "array", UniqueItems: true, @@ -579,7 +579,7 @@ var schemaExamples = []schemaExample{ UniqueItems: true, Items: (&Schema{ Type: "object", - Properties: map[string]*SchemaRef{ + Properties: Schemas{ "key1": NewFloat64Schema().NewRef(), }, }).NewRef(), @@ -678,7 +678,7 @@ var schemaExamples = []schemaExample{ Schema: &Schema{ Type: "object", MaxProps: Uint64Ptr(2), - Properties: map[string]*SchemaRef{ + Properties: Schemas{ "numberProperty": NewFloat64Schema().NewRef(), }, }, diff --git a/openapi3filter/req_resp_decoder.go b/openapi3filter/req_resp_decoder.go index cb58b62b6..12b368384 100644 --- a/openapi3filter/req_resp_decoder.go +++ b/openapi3filter/req_resp_decoder.go @@ -244,8 +244,6 @@ func decodeStyledParameter(param *openapi3.Parameter, input *RequestValidationIn } func decodeValue(dec valueDecoder, param string, sm *openapi3.SerializationMethod, schema *openapi3.SchemaRef, required bool) (interface{}, error) { - var decodeFn func(param string, sm *openapi3.SerializationMethod, schema *openapi3.SchemaRef) (interface{}, error) - if len(schema.Value.AllOf) > 0 { var value interface{} var err error @@ -298,6 +296,7 @@ func decodeValue(dec valueDecoder, param string, sm *openapi3.SerializationMetho } if schema.Value.Type != "" { + var decodeFn func(param string, sm *openapi3.SerializationMethod, schema *openapi3.SchemaRef) (interface{}, error) switch schema.Value.Type { case "array": decodeFn = func(param string, sm *openapi3.SerializationMethod, schema *openapi3.SchemaRef) (interface{}, error) { diff --git a/openapi3filter/validation_error_test.go b/openapi3filter/validation_error_test.go index 6eadbd06c..bdf544210 100644 --- a/openapi3filter/validation_error_test.go +++ b/openapi3filter/validation_error_test.go @@ -56,7 +56,8 @@ type validationTest struct { } func getValidationTests(t *testing.T) []*validationTest { - badHost, _ := http.NewRequest(http.MethodGet, "http://unknown-host.com/v2/pet", nil) + badHost, err := http.NewRequest(http.MethodGet, "http://unknown-host.com/v2/pet", nil) + require.NoError(t, err) badPath := newPetstoreRequest(t, http.MethodGet, "/watdis", nil) badMethod := newPetstoreRequest(t, http.MethodTrace, "/pet", nil) diff --git a/routers/legacy/router.go b/routers/legacy/router.go index ecaae1348..f1f47d9ed 100644 --- a/routers/legacy/router.go +++ b/routers/legacy/router.go @@ -125,7 +125,10 @@ func (router *Router) FindRoute(req *http.Request) (*routers.Route, map[string]s } } pathParams = make(map[string]string, 8) - paramNames, _ := server.ParameterNames() + paramNames, err := server.ParameterNames() + if err != nil { + return nil, nil, err + } for i, value := range paramValues { name := paramNames[i] pathParams[name] = value