@@ -90,8 +90,23 @@ func ValidateRequest(ctx context.Context, input *RequestValidationInput) error {
9090
9191 // RequestBody
9292 requestBody := operation .RequestBody
93- if requestBody != nil && ! options .ExcludeRequestBody {
94- if err := ValidateRequestBody (ctx , input , requestBody .Value ); err != nil {
93+ if ! options .ExcludeRequestBody {
94+ // Validate specification request body if present
95+ if requestBody != nil {
96+ if err := ValidateRequestBody (ctx , input , requestBody .Value ); err != nil {
97+ if ! options .MultiError {
98+ return err
99+ }
100+ me = append (me , err )
101+ }
102+ }
103+
104+ // Reject if specification request body if not present (not wanted) but is present in the HTTP request
105+ if options .RejectWhenRequestBodyNotSpecified && input .Request .ContentLength > 0 {
106+ err := & RequestError {
107+ Input : input ,
108+ Err : fmt .Errorf ("request body not allowed for this request" ),
109+ }
95110 if ! options .MultiError {
96111 return err
97112 }
@@ -193,22 +208,34 @@ func ValidateParameter(ctx context.Context, input *RequestValidationInput, param
193208 case openapi3 .ParameterInHeader :
194209 req .Header .Add (parameter .Name , fmt .Sprint (value ))
195210 case openapi3 .ParameterInCookie :
196- req .AddCookie (& http.Cookie {
197- Name : parameter .Name ,
198- Value : fmt .Sprint (value ),
199- })
211+ req .AddCookie (
212+ & http.Cookie {
213+ Name : parameter .Name ,
214+ Value : fmt .Sprint (value ),
215+ },
216+ )
200217 }
201218 }
202219 }
203220
204221 // Validate a parameter's value and presence.
205222 if parameter .Required && ! found {
206- return & RequestError {Input : input , Parameter : parameter , Reason : ErrInvalidRequired .Error (), Err : ErrInvalidRequired }
223+ return & RequestError {
224+ Input : input ,
225+ Parameter : parameter ,
226+ Reason : ErrInvalidRequired .Error (),
227+ Err : ErrInvalidRequired ,
228+ }
207229 }
208230
209231 if isNilValue (value ) {
210232 if ! parameter .AllowEmptyValue && found {
211- return & RequestError {Input : input , Parameter : parameter , Reason : ErrInvalidEmptyValue .Error (), Err : ErrInvalidEmptyValue }
233+ return & RequestError {
234+ Input : input ,
235+ Parameter : parameter ,
236+ Reason : ErrInvalidEmptyValue .Error (),
237+ Err : ErrInvalidEmptyValue ,
238+ }
212239 }
213240 return nil
214241 }
@@ -372,7 +399,11 @@ func ValidateRequestBody(ctx context.Context, input *RequestValidationInput, req
372399// ValidateSecurityRequirements goes through multiple OpenAPI 3 security
373400// requirements in order and returns nil on the first valid requirement.
374401// If no requirement is met, errors are returned in order.
375- func ValidateSecurityRequirements (ctx context.Context , input * RequestValidationInput , srs openapi3.SecurityRequirements ) error {
402+ func ValidateSecurityRequirements (
403+ ctx context.Context ,
404+ input * RequestValidationInput ,
405+ srs openapi3.SecurityRequirements ,
406+ ) error {
376407 if len (srs ) == 0 {
377408 return nil
378409 }
@@ -394,7 +425,11 @@ func ValidateSecurityRequirements(ctx context.Context, input *RequestValidationI
394425}
395426
396427// validateSecurityRequirement validates a single OpenAPI 3 security requirement
397- func validateSecurityRequirement (ctx context.Context , input * RequestValidationInput , securityRequirement openapi3.SecurityRequirement ) error {
428+ func validateSecurityRequirement (
429+ ctx context.Context ,
430+ input * RequestValidationInput ,
431+ securityRequirement openapi3.SecurityRequirement ,
432+ ) error {
398433 names := make ([]string , 0 , len (securityRequirement ))
399434 for name := range securityRequirement {
400435 names = append (names , name )
@@ -467,12 +502,14 @@ func validateSecurityRequirement(ctx context.Context, input *RequestValidationIn
467502 }
468503 }
469504
470- if err := f (ctx , & AuthenticationInput {
471- RequestValidationInput : input ,
472- SecuritySchemeName : name ,
473- SecurityScheme : securityScheme ,
474- Scopes : scopes ,
475- }); err != nil {
505+ if err := f (
506+ ctx , & AuthenticationInput {
507+ RequestValidationInput : input ,
508+ SecuritySchemeName : name ,
509+ SecurityScheme : securityScheme ,
510+ Scopes : scopes ,
511+ },
512+ ); err != nil {
476513 return err
477514 }
478515 }
0 commit comments