Skip to content

Commit

Permalink
Cleaning up some coverage issues
Browse files Browse the repository at this point in the history
  • Loading branch information
daveshanley committed Sep 30, 2024
1 parent 1e8709f commit 3dc6133
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
4 changes: 4 additions & 0 deletions helpers/ignore_regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ package helpers
import "regexp"

var IgnorePattern = `^\b(anyOf|allOf|oneOf|validation) failed\b`
var IgnorePolyPattern = `^\b(anyOf|allOf|oneOf) failed\b`

// IgnoreRegex is a regular expression that matches the IgnorePattern
var IgnoreRegex = regexp.MustCompile(IgnorePattern)

// IgnorePolyRegex is a regular expression that matches the IgnorePattern
var IgnorePolyRegex = regexp.MustCompile(IgnorePolyPattern)
40 changes: 40 additions & 0 deletions parameters/query_parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2708,3 +2708,43 @@ components:
assert.Equal(t, "The query parameter 'objParam' is defined as an object,"+
" however it failed to pass a schema validation", errors[0].Reason)
}

func TestNewValidator_ValidateRawMap(t *testing.T) {
spec := `openapi: 3.1.0
paths:
/a/fishy/on/a/dishy:
get:
parameters:
- name: fishy
in: query
required: true
style: deepObject
schema:
type: object
properties:
ocean:
type: string
salt:
type: boolean
required: [ocean, salt]
operationId: locateFishy`

doc, _ := libopenapi.NewDocument([]byte(spec))
m, _ := doc.BuildV3Model()

sch := m.Model.Paths.PathItems.GetOrZero("/a/fishy/on/a/dishy").Get.Parameters[0].Schema

s := sch.Schema()

// this is not compatible.
rawObject := map[int]int{
1: 2,
}

errs := ValidateParameterSchema(s, rawObject, "cake", "burger", "lemons",
"pizza", "rice", "herbs")

assert.Len(t, errs, 1)
assert.Equal(t, "lemons 'pizza' is defined as an object, "+
"however it failed to be decoded as an object", errs[0].Reason)
}
2 changes: 1 addition & 1 deletion schema_validation/validate_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func ValidateOpenAPIDocument(doc libopenapi.Document) (bool, []*liberrors.Valida
er := schFlatErrs[q]

errMsg := er.Error.Kind.LocalizedString(message.NewPrinter(language.Tag{}))
if er.KeywordLocation == "" && helpers.IgnoreRegex.MatchString(errMsg) {
if er.KeywordLocation == "" || helpers.IgnorePolyRegex.MatchString(errMsg) {
continue // ignore this error, it's useless tbh, utter noise.
}
if errMsg != "" {
Expand Down
16 changes: 16 additions & 0 deletions schema_validation/validate_document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,19 @@ components:
assert.Len(t, errors, 1)
assert.Len(t, errors[0].SchemaValidationErrors, 1)
}

func TestValidateSchema_GeneratePointlessValidation(t *testing.T) {
spec := `openapi: 3.1.0
info:
version: 1
`

doc, _ := libopenapi.NewDocument([]byte(spec))

// validate!
valid, errors := ValidateOpenAPIDocument(doc)

assert.False(t, valid)
assert.Len(t, errors, 1)
assert.Len(t, errors[0].SchemaValidationErrors, 6)
}

0 comments on commit 3dc6133

Please sign in to comment.