From f7c80fecb8472b694244fb1119e0be9806fa0dee Mon Sep 17 00:00:00 2001 From: Nick Ufer Date: Thu, 2 Dec 2021 16:25:49 +0100 Subject: [PATCH] fix: duplicate error reason when parameter is required but not present (#453) --- openapi3filter/validate_request.go | 2 +- openapi3filter/validation_error_encoder.go | 2 +- openapi3filter/validation_error_test.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/openapi3filter/validate_request.go b/openapi3filter/validate_request.go index 2f9a5f14c..990b299ef 100644 --- a/openapi3filter/validate_request.go +++ b/openapi3filter/validate_request.go @@ -140,7 +140,7 @@ func ValidateParameter(ctx context.Context, input *RequestValidationInput, param // Validate a parameter's value. if value == nil { if parameter.Required { - return &RequestError{Input: input, Parameter: parameter, Reason: ErrInvalidRequired.Error(), Err: ErrInvalidRequired} + return &RequestError{Input: input, Parameter: parameter, Err: ErrInvalidRequired} } return nil } diff --git a/openapi3filter/validation_error_encoder.go b/openapi3filter/validation_error_encoder.go index 707b22d4a..205186960 100644 --- a/openapi3filter/validation_error_encoder.go +++ b/openapi3filter/validation_error_encoder.go @@ -75,7 +75,7 @@ func convertBasicRequestError(e *RequestError) *ValidationError { } func convertErrInvalidRequired(e *RequestError) *ValidationError { - if e.Reason == ErrInvalidRequired.Error() && e.Parameter != nil { + if e.Err == ErrInvalidRequired && e.Parameter != nil { return &ValidationError{ Status: http.StatusBadRequest, Title: fmt.Sprintf("parameter %q in %s is required", e.Parameter.Name, e.Parameter.In), diff --git a/openapi3filter/validation_error_test.go b/openapi3filter/validation_error_test.go index d539c3f7e..6eadbd06c 100644 --- a/openapi3filter/validation_error_test.go +++ b/openapi3filter/validation_error_test.go @@ -180,7 +180,7 @@ func getValidationTests(t *testing.T) []*validationTest { }, wantErrParam: "status", wantErrParamIn: "query", - wantErrReason: ErrInvalidRequired.Error(), + wantErrBody: `parameter "status" in query has an error: value is required but missing`, wantErrResponse: &ValidationError{Status: http.StatusBadRequest, Title: `parameter "status" in query is required`}, }, @@ -424,7 +424,7 @@ func getValidationTests(t *testing.T) []*validationTest { }, wantErrParam: "petId", wantErrParamIn: "path", - wantErrReason: ErrInvalidRequired.Error(), + wantErrBody: `parameter "petId" in path has an error: value is required but missing`, wantErrResponse: &ValidationError{Status: http.StatusBadRequest, Title: `parameter "petId" in path is required`}, },