Skip to content

Commit 46638b9

Browse files
committed
Fixed borked validation types after upgrading libopenapi
Signed-off-by: Dave Shanley <[email protected]>
1 parent 7703d58 commit 46638b9

16 files changed

+1489
-1463
lines changed

parameters/cookie_parameters.go

Lines changed: 110 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -4,124 +4,125 @@
44
package parameters
55

66
import (
7-
"github.com/pb33f/libopenapi-validator/errors"
8-
"github.com/pb33f/libopenapi-validator/helpers"
9-
"github.com/pb33f/libopenapi-validator/paths"
10-
"github.com/pb33f/libopenapi/datamodel/high/base"
11-
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
12-
"net/http"
13-
"strconv"
14-
"strings"
7+
"fmt"
8+
"github.com/pb33f/libopenapi-validator/errors"
9+
"github.com/pb33f/libopenapi-validator/helpers"
10+
"github.com/pb33f/libopenapi-validator/paths"
11+
"github.com/pb33f/libopenapi/datamodel/high/base"
12+
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
13+
"net/http"
14+
"strconv"
15+
"strings"
1516
)
1617

1718
func (v *paramValidator) ValidateCookieParams(request *http.Request) (bool, []*errors.ValidationError) {
1819

19-
// find path
20-
var pathItem *v3.PathItem
21-
var errs []*errors.ValidationError
22-
if v.pathItem == nil {
23-
pathItem, errs, _ = paths.FindPath(request, v.document)
24-
if pathItem == nil || errs != nil {
25-
v.errors = errs
26-
return false, errs
27-
}
28-
} else {
29-
pathItem = v.pathItem
30-
}
20+
// find path
21+
var pathItem *v3.PathItem
22+
var errs []*errors.ValidationError
23+
if v.pathItem == nil {
24+
pathItem, errs, _ = paths.FindPath(request, v.document)
25+
if pathItem == nil || errs != nil {
26+
v.errors = errs
27+
return false, errs
28+
}
29+
} else {
30+
pathItem = v.pathItem
31+
}
3132

32-
// extract params for the operation
33-
var params = helpers.ExtractParamsForOperation(request, pathItem)
34-
var validationErrors []*errors.ValidationError
35-
for _, p := range params {
36-
if p.In == helpers.Cookie {
37-
for _, cookie := range request.Cookies() {
38-
if cookie.Name == p.Name { // cookies are case-sensitive, an exact match is required
33+
// extract params for the operation
34+
var params = helpers.ExtractParamsForOperation(request, pathItem)
35+
var validationErrors []*errors.ValidationError
36+
for _, p := range params {
37+
if p.In == helpers.Cookie {
38+
for _, cookie := range request.Cookies() {
39+
if cookie.Name == p.Name { // cookies are case-sensitive, an exact match is required
3940

40-
var sch *base.Schema
41-
if p.Schema != nil {
42-
sch = p.Schema.Schema()
43-
}
44-
pType := sch.Type
41+
var sch *base.Schema
42+
if p.Schema != nil {
43+
sch = p.Schema.Schema()
44+
}
45+
pType := sch.Type
4546

46-
for _, ty := range pType {
47-
switch ty {
48-
case helpers.Integer, helpers.Number:
49-
if _, err := strconv.ParseFloat(cookie.Value, 64); err != nil {
50-
validationErrors = append(validationErrors,
51-
errors.InvalidCookieParamNumber(p, strings.ToLower(cookie.Value), sch))
52-
break
53-
}
54-
// check if enum is in range
55-
if sch.Enum != nil {
56-
matchFound := false
57-
for _, enumVal := range sch.Enum {
58-
if strings.TrimSpace(cookie.Value) == enumVal {
59-
matchFound = true
60-
break
61-
}
62-
}
63-
if !matchFound {
64-
validationErrors = append(validationErrors,
65-
errors.IncorrectCookieParamEnum(p, strings.ToLower(cookie.Value), sch))
66-
}
67-
}
68-
case helpers.Boolean:
69-
if _, err := strconv.ParseBool(cookie.Value); err != nil {
70-
validationErrors = append(validationErrors,
71-
errors.IncorrectCookieParamBool(p, strings.ToLower(cookie.Value), sch))
72-
}
73-
case helpers.Object:
74-
if !p.IsExploded() {
75-
encodedObj := helpers.ConstructMapFromCSV(cookie.Value)
47+
for _, ty := range pType {
48+
switch ty {
49+
case helpers.Integer, helpers.Number:
50+
if _, err := strconv.ParseFloat(cookie.Value, 64); err != nil {
51+
validationErrors = append(validationErrors,
52+
errors.InvalidCookieParamNumber(p, strings.ToLower(cookie.Value), sch))
53+
break
54+
}
55+
// check if enum is in range
56+
if sch.Enum != nil {
57+
matchFound := false
58+
for _, enumVal := range sch.Enum {
59+
if strings.TrimSpace(cookie.Value) == fmt.Sprint(enumVal) {
60+
matchFound = true
61+
break
62+
}
63+
}
64+
if !matchFound {
65+
validationErrors = append(validationErrors,
66+
errors.IncorrectCookieParamEnum(p, strings.ToLower(cookie.Value), sch))
67+
}
68+
}
69+
case helpers.Boolean:
70+
if _, err := strconv.ParseBool(cookie.Value); err != nil {
71+
validationErrors = append(validationErrors,
72+
errors.IncorrectCookieParamBool(p, strings.ToLower(cookie.Value), sch))
73+
}
74+
case helpers.Object:
75+
if !p.IsExploded() {
76+
encodedObj := helpers.ConstructMapFromCSV(cookie.Value)
7677

77-
// if a schema was extracted
78-
if sch != nil {
79-
validationErrors = append(validationErrors,
80-
ValidateParameterSchema(sch, encodedObj, "",
81-
"Cookie parameter",
82-
"The cookie parameter",
83-
p.Name,
84-
helpers.ParameterValidation,
85-
helpers.ParameterValidationQuery)...)
86-
}
87-
}
88-
case helpers.Array:
78+
// if a schema was extracted
79+
if sch != nil {
80+
validationErrors = append(validationErrors,
81+
ValidateParameterSchema(sch, encodedObj, "",
82+
"Cookie parameter",
83+
"The cookie parameter",
84+
p.Name,
85+
helpers.ParameterValidation,
86+
helpers.ParameterValidationQuery)...)
87+
}
88+
}
89+
case helpers.Array:
8990

90-
if !p.IsExploded() {
91-
// well we're already in an array, so we need to check the items schema
92-
// to ensure this array items matches the type
93-
// only check if items is a schema, not a boolean
94-
if sch.Items.IsA() {
95-
validationErrors = append(validationErrors,
96-
ValidateCookieArray(sch, p, cookie.Value)...)
97-
}
98-
}
91+
if !p.IsExploded() {
92+
// well we're already in an array, so we need to check the items schema
93+
// to ensure this array items matches the type
94+
// only check if items is a schema, not a boolean
95+
if sch.Items.IsA() {
96+
validationErrors = append(validationErrors,
97+
ValidateCookieArray(sch, p, cookie.Value)...)
98+
}
99+
}
99100

100-
case helpers.String:
101+
case helpers.String:
101102

102-
// check if the schema has an enum, and if so, match the value against one of
103-
// the defined enum values.
104-
if sch.Enum != nil {
105-
matchFound := false
106-
for _, enumVal := range sch.Enum {
107-
if strings.TrimSpace(cookie.Value) == enumVal {
108-
matchFound = true
109-
break
110-
}
111-
}
112-
if !matchFound {
113-
validationErrors = append(validationErrors,
114-
errors.IncorrectCookieParamEnum(p, strings.ToLower(cookie.Value), sch))
115-
}
116-
}
117-
}
118-
}
119-
}
120-
}
121-
}
122-
}
123-
if len(validationErrors) > 0 {
124-
return false, validationErrors
125-
}
126-
return true, nil
103+
// check if the schema has an enum, and if so, match the value against one of
104+
// the defined enum values.
105+
if sch.Enum != nil {
106+
matchFound := false
107+
for _, enumVal := range sch.Enum {
108+
if strings.TrimSpace(cookie.Value) == fmt.Sprint(enumVal) {
109+
matchFound = true
110+
break
111+
}
112+
}
113+
if !matchFound {
114+
validationErrors = append(validationErrors,
115+
errors.IncorrectCookieParamEnum(p, strings.ToLower(cookie.Value), sch))
116+
}
117+
}
118+
}
119+
}
120+
}
121+
}
122+
}
123+
}
124+
if len(validationErrors) > 0 {
125+
return false, validationErrors
126+
}
127+
return true, nil
127128
}

0 commit comments

Comments
 (0)