Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.0.52 #74

Merged
merged 4 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/pb33f/libopenapi-validator
go 1.21

require (
github.com/pb33f/libopenapi v0.15.3
github.com/pb33f/libopenapi v0.16.3
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
github.com/vmware-labs/yaml-jsonpath v0.3.2
gopkg.in/yaml.v3 v3.0.1
)
Expand All @@ -18,6 +18,6 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect
golang.org/x/sync v0.6.0 // indirect
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/pb33f/libopenapi v0.15.3 h1:P3D4Vnuvct3PD9ctwGxmmvniBUrEs1A3GjQcSfxjQrg=
github.com/pb33f/libopenapi v0.15.3/go.mod h1:m+4Pwri31UvcnZjuP8M7TlbR906DXJmMvYsbis234xg=
github.com/pb33f/libopenapi v0.16.3 h1:ozo0vYdeP6r+qAXb+Kg1MOy6QrTxSgNpoD3nKuw/HA8=
github.com/pb33f/libopenapi v0.16.3/go.mod h1:PEXNwvtT4KNdjrwudp5OYnD1ryqK6uJ68aMNyWvoMuc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
Expand All @@ -65,8 +65,8 @@ github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNX
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/vmware-labs/yaml-jsonpath v0.3.2 h1:/5QKeCBGdsInyDCyVNLbXyilb61MXGi9NP674f9Hobk=
github.com/vmware-labs/yaml-jsonpath v0.3.2/go.mod h1:U6whw1z03QyqgWdgXxvVnQ90zN1BWz5V+51Ewf8k+rQ=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
Expand All @@ -75,8 +75,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a h1:HinSgX1tJRX3KsL//Gxynpw5CTOAIPhgL4W8PNiIpVE=
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand Down
254 changes: 128 additions & 126 deletions parameters/path_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,163 +108,165 @@ func (v *paramValidator) ValidatePathParams(request *http.Request) (bool, []*err
}

// for each type, check the value.
for typ := range sch.Type {
if sch != nil && sch.Type != nil {
for typ := range sch.Type {

switch sch.Type[typ] {
case helpers.String:
switch sch.Type[typ] {
case helpers.String:

// TODO: label and matrix style validation
// TODO: label and matrix style validation

// check if the param is within the enum
if sch.Enum != nil {
enumCheck(paramValue)
break
}
validationErrors = append(validationErrors,
ValidateSingleParameterSchema(
// check if the param is within the enum
if sch.Enum != nil {
enumCheck(paramValue)
break
}
validationErrors = append(validationErrors,
ValidateSingleParameterSchema(
sch,
paramValue,
"Path parameter",
"The path parameter",
p.Name,
helpers.ParameterValidation,
helpers.ParameterValidationPath,
)...)

case helpers.Integer, helpers.Number:
// simple use case is already handled in find param.
rawParamValue, paramValueParsed, err := v.resolveNumber(sch, p, isLabel, isMatrix, paramValue)
if err != nil {
validationErrors = append(validationErrors, err...)
break
}
// check if the param is within the enum
if sch.Enum != nil {
enumCheck(rawParamValue)
break
}
validationErrors = append(validationErrors, ValidateSingleParameterSchema(
sch,
paramValue,
paramValueParsed,
"Path parameter",
"The path parameter",
p.Name,
helpers.ParameterValidation,
helpers.ParameterValidationPath,
)...)

case helpers.Integer, helpers.Number:
// simple use case is already handled in find param.
rawParamValue, paramValueParsed, err := v.resolveNumber(sch, p, isLabel, isMatrix, paramValue)
if err != nil {
validationErrors = append(validationErrors, err...)
break
}
// check if the param is within the enum
if sch.Enum != nil {
enumCheck(rawParamValue)
break
}
validationErrors = append(validationErrors, ValidateSingleParameterSchema(
sch,
paramValueParsed,
"Path parameter",
"The path parameter",
p.Name,
helpers.ParameterValidation,
helpers.ParameterValidationPath,
)...)

case helpers.Boolean:
if isLabel && p.Style == helpers.LabelStyle {
if _, err := strconv.ParseFloat(paramValue[1:], 64); err != nil {
validationErrors = append(validationErrors,
errors.IncorrectPathParamBool(p, paramValue[1:], sch))
}
}
if isSimple {
if _, err := strconv.ParseBool(paramValue); err != nil {
validationErrors = append(validationErrors,
errors.IncorrectPathParamBool(p, paramValue, sch))
}
}
if isMatrix && p.Style == helpers.MatrixStyle {
// strip off the colon and the parameter name
paramValue = strings.Replace(paramValue[1:], fmt.Sprintf("%s=", p.Name), "", 1)
if _, err := strconv.ParseBool(paramValue); err != nil {
validationErrors = append(validationErrors,
errors.IncorrectPathParamBool(p, paramValue, sch))
}
}
case helpers.Object:
var encodedObject interface{}

if p.IsDefaultPathEncoding() {
encodedObject = helpers.ConstructMapFromCSV(paramValue)
} else {
switch p.Style {
case helpers.LabelStyle:
if !p.IsExploded() {
encodedObject = helpers.ConstructMapFromCSV(paramValue[1:])
} else {
encodedObject = helpers.ConstructKVFromLabelEncoding(paramValue)
case helpers.Boolean:
if isLabel && p.Style == helpers.LabelStyle {
if _, err := strconv.ParseFloat(paramValue[1:], 64); err != nil {
validationErrors = append(validationErrors,
errors.IncorrectPathParamBool(p, paramValue[1:], sch))
}
case helpers.MatrixStyle:
if !p.IsExploded() {
paramValue = strings.Replace(paramValue[1:], fmt.Sprintf("%s=", p.Name), "", 1)
encodedObject = helpers.ConstructMapFromCSV(paramValue)
} else {
paramValue = strings.Replace(paramValue[1:], fmt.Sprintf("%s=", p.Name), "", 1)
encodedObject = helpers.ConstructKVFromMatrixCSV(paramValue)
}
if isSimple {
if _, err := strconv.ParseBool(paramValue); err != nil {
validationErrors = append(validationErrors,
errors.IncorrectPathParamBool(p, paramValue, sch))
}
default:
if p.IsExploded() {
encodedObject = helpers.ConstructKVFromCSV(paramValue)
}
if isMatrix && p.Style == helpers.MatrixStyle {
// strip off the colon and the parameter name
paramValue = strings.Replace(paramValue[1:], fmt.Sprintf("%s=", p.Name), "", 1)
if _, err := strconv.ParseBool(paramValue); err != nil {
validationErrors = append(validationErrors,
errors.IncorrectPathParamBool(p, paramValue, sch))
}
}
}
// if a schema was extracted
if sch != nil {
validationErrors = append(validationErrors,
ValidateParameterSchema(sch,
encodedObject,
"",
"Path parameter",
"The path parameter",
p.Name,
helpers.ParameterValidation,
helpers.ParameterValidationPath)...)
}
case helpers.Object:
var encodedObject interface{}

case helpers.Array:

// extract the items schema in order to validate the array items.
if sch.Items != nil && sch.Items.IsA() {
iSch := sch.Items.A.Schema()
for n := range iSch.Type {
// determine how to explode the array
var arrayValues []string
if isSimple {
arrayValues = strings.Split(paramValue, helpers.Comma)
}
if isLabel {
if p.IsDefaultPathEncoding() {
encodedObject = helpers.ConstructMapFromCSV(paramValue)
} else {
switch p.Style {
case helpers.LabelStyle:
if !p.IsExploded() {
arrayValues = strings.Split(paramValue[1:], helpers.Comma)
encodedObject = helpers.ConstructMapFromCSV(paramValue[1:])
} else {
arrayValues = strings.Split(paramValue[1:], helpers.Period)
encodedObject = helpers.ConstructKVFromLabelEncoding(paramValue)
}
}
if isMatrix {
case helpers.MatrixStyle:
if !p.IsExploded() {
paramValue = strings.Replace(paramValue[1:], fmt.Sprintf("%s=", p.Name), "", 1)
arrayValues = strings.Split(paramValue, helpers.Comma)
encodedObject = helpers.ConstructMapFromCSV(paramValue)
} else {
paramValue = strings.ReplaceAll(paramValue[1:], fmt.Sprintf("%s=", p.Name), "")
arrayValues = strings.Split(paramValue, helpers.SemiColon)
paramValue = strings.Replace(paramValue[1:], fmt.Sprintf("%s=", p.Name), "", 1)
encodedObject = helpers.ConstructKVFromMatrixCSV(paramValue)
}
default:
if p.IsExploded() {
encodedObject = helpers.ConstructKVFromCSV(paramValue)
}
}
switch iSch.Type[n] {
case helpers.Integer, helpers.Number:
for pv := range arrayValues {
if _, err := strconv.ParseFloat(arrayValues[pv], 64); err != nil {
validationErrors = append(validationErrors,
errors.IncorrectPathParamArrayNumber(p, arrayValues[pv], sch, iSch))
}
// if a schema was extracted
if sch != nil {
validationErrors = append(validationErrors,
ValidateParameterSchema(sch,
encodedObject,
"",
"Path parameter",
"The path parameter",
p.Name,
helpers.ParameterValidation,
helpers.ParameterValidationPath)...)
}

case helpers.Array:

// extract the items schema in order to validate the array items.
if sch.Items != nil && sch.Items.IsA() {
iSch := sch.Items.A.Schema()
for n := range iSch.Type {
// determine how to explode the array
var arrayValues []string
if isSimple {
arrayValues = strings.Split(paramValue, helpers.Comma)
}
if isLabel {
if !p.IsExploded() {
arrayValues = strings.Split(paramValue[1:], helpers.Comma)
} else {
arrayValues = strings.Split(paramValue[1:], helpers.Period)
}
}
if isMatrix {
if !p.IsExploded() {
paramValue = strings.Replace(paramValue[1:], fmt.Sprintf("%s=", p.Name), "", 1)
arrayValues = strings.Split(paramValue, helpers.Comma)
} else {
paramValue = strings.ReplaceAll(paramValue[1:], fmt.Sprintf("%s=", p.Name), "")
arrayValues = strings.Split(paramValue, helpers.SemiColon)
}
}
case helpers.Boolean:
for pv := range arrayValues {
bc := len(validationErrors)
if _, err := strconv.ParseBool(arrayValues[pv]); err != nil {
validationErrors = append(validationErrors,
errors.IncorrectPathParamArrayBoolean(p, arrayValues[pv], sch, iSch))
continue
switch iSch.Type[n] {
case helpers.Integer, helpers.Number:
for pv := range arrayValues {
if _, err := strconv.ParseFloat(arrayValues[pv], 64); err != nil {
validationErrors = append(validationErrors,
errors.IncorrectPathParamArrayNumber(p, arrayValues[pv], sch, iSch))
}
}
if len(validationErrors) == bc {
// ParseBool will parse 0 or 1 as false/true to we
// need to catch this edge case.
if arrayValues[pv] == "0" || arrayValues[pv] == "1" {
case helpers.Boolean:
for pv := range arrayValues {
bc := len(validationErrors)
if _, err := strconv.ParseBool(arrayValues[pv]); err != nil {
validationErrors = append(validationErrors,
errors.IncorrectPathParamArrayBoolean(p, arrayValues[pv], sch, iSch))
continue
}
if len(validationErrors) == bc {
// ParseBool will parse 0 or 1 as false/true to we
// need to catch this edge case.
if arrayValues[pv] == "0" || arrayValues[pv] == "1" {
validationErrors = append(validationErrors,
errors.IncorrectPathParamArrayBoolean(p, arrayValues[pv], sch, iSch))
continue
}
}
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions parameters/validate_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,19 @@ func formatJsonSchemaValidationError(schema *base.Schema, scErrs *jsonschema.Val
if er.KeywordLocation == "" || strings.HasPrefix(er.Error, "doesn't validate with") {
continue // ignore this error, it's not useful
}
schemaValidationErrors = append(schemaValidationErrors, &errors.SchemaValidationFailure{

fail := &errors.SchemaValidationFailure{
Reason: er.Error,
Location: er.KeywordLocation,
OriginalError: scErrs,
})
}
if schema != nil {
rendered, err := schema.RenderInline()
if err == nil && rendered != nil {
fail.ReferenceSchema = fmt.Sprintf("%s", rendered)
}
}
schemaValidationErrors = append(schemaValidationErrors, fail)
}
schemaType := "undefined"
if len(schema.Type) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion requests/validate_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (v *requestBodyValidator) ValidateRequestBody(request *http.Request) (bool,
required = *operation.RequestBody.Required
}
if contentType == "" {
if !required{
if !required {
// request body is not required, the validation stop there.
return true, nil
}
Expand Down
Loading